stack_manager.cc revision 8675d884479deca722ef5d7776a165241457dd75
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" 251924dac3811575d594c11b76874b24ee0ad24e71Zach Johnson#include "btif_common.h" 268df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "bt_utils.h" 2772f308ee6d3983ae2c0d67be3de2451f2dd72dcbZach Johnson#include "module.h" 288df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "osi.h" 298df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "semaphore.h" 308df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "stack_manager.h" 318df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "thread.h" 328df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 338df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic thread_t *management_thread; 348df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 358df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// If initialized, any of the bluetooth API functions can be called. 368df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// (e.g. turning logging on and off, enabling/disabling the stack, etc) 378df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic bool stack_is_initialized; 388df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// If running, the stack is fully up and able to bluetooth. 398df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic bool stack_is_running; 408df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 418df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_init_stack(void *context); 428df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_start_up_stack(void *context); 438df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_shut_down_stack(void *context); 448df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_clean_up_stack(void *context); 458df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 46efea7821780cdea2b30e944b08e418d1f2781878Zach Johnsonstatic void event_signal_stack_up(void *context); 47efea7821780cdea2b30e944b08e418d1f2781878Zach Johnsonstatic void event_signal_stack_down(void *context); 48efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson 49f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson// Unvetted includes/imports, etc which should be removed or vetted in the future 508ea8188a481504641fc6a72087704eb808d0bd87Zach Johnsonstatic future_t *hack_future; 51f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnsonvoid bte_main_enable(); 52efea7821780cdea2b30e944b08e418d1f2781878Zach Johnsonvoid btif_thread_post(thread_fn func, void *context); 53f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson// End unvetted section 548ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 558df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Interface functions 568df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 578df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void init_stack(void) { 588df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson // This is a synchronous process. Post it to the thread though, so 598df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson // state modification only happens there. 608df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson semaphore_t *semaphore = semaphore_new(0); 618df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson thread_post(management_thread, event_init_stack, semaphore); 628df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson semaphore_wait(semaphore); 63f5153bf62b6cee3f3c740504e94360130c3a9186Sharvil Nanavati semaphore_free(semaphore); 648df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 658df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 668df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void start_up_stack_async(void) { 678df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson thread_post(management_thread, event_start_up_stack, NULL); 688df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 698df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 708df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void shut_down_stack_async(void) { 718df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson thread_post(management_thread, event_shut_down_stack, NULL); 728df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 738df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 748df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void clean_up_stack_async(void) { 758df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson thread_post(management_thread, event_clean_up_stack, NULL); 768df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 778df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 78f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnsonstatic bool get_stack_is_running(void) { 79f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson return stack_is_running; 80f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson} 81f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson 828df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Internal functions 838df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 848df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to initialize the stack 858df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_init_stack(void *context) { 868df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson semaphore_t *semaphore = (semaphore_t *)context; 878df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 888df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (!stack_is_initialized) { 8972f308ee6d3983ae2c0d67be3de2451f2dd72dcbZach Johnson module_management_start(); 9072f308ee6d3983ae2c0d67be3de2451f2dd72dcbZach Johnson 918df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson bt_utils_init(); 928df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson btif_init_bluetooth(); 938df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 948df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson // stack init is synchronous, so no waiting necessary here 958df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson stack_is_initialized = true; 968df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson } 978df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 988df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (semaphore) 998df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson semaphore_post(semaphore); 1008df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 1018df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1028df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void ensure_stack_is_initialized(void) { 1038df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (!stack_is_initialized) { 1048df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGW("%s found the stack was uninitialized. Initializing now.", __func__); 1058df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson // No semaphore needed since we are calling it directly 1068df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson event_init_stack(NULL); 1078df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson } 1088df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 1098df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1108df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to start up the stack 1118df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_start_up_stack(UNUSED_ATTR void *context) { 1128df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (stack_is_running) { 1138df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s stack already brought up.", __func__); 1148df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson return; 1158df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson } 1168df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1178df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ensure_stack_is_initialized(); 1188df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1198df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s is bringing up the stack.", __func__); 1208ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson hack_future = future_new(); 1218ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 122f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson bte_main_enable(); 1238ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 1241924dac3811575d594c11b76874b24ee0ad24e71Zach Johnson if (future_await(hack_future) != FUTURE_SUCCESS) { 125efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson stack_is_running = true; // So stack shutdown actually happens 1261924dac3811575d594c11b76874b24ee0ad24e71Zach Johnson event_shut_down_stack(NULL); 1271924dac3811575d594c11b76874b24ee0ad24e71Zach Johnson return; 1281924dac3811575d594c11b76874b24ee0ad24e71Zach Johnson } 1291924dac3811575d594c11b76874b24ee0ad24e71Zach Johnson 1301924dac3811575d594c11b76874b24ee0ad24e71Zach Johnson stack_is_running = true; 1318df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s finished", __func__); 132efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson btif_thread_post(event_signal_stack_up, NULL); 1338df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 1348df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1358df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to shut down the stack 1368df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_shut_down_stack(UNUSED_ATTR void *context) { 1378df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (!stack_is_running) { 1388df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s stack is already brought down.", __func__); 1398df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson return; 1408df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson } 1418df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1428df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s is bringing down the stack.", __func__); 1438ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson hack_future = future_new(); 1448df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson stack_is_running = false; 1458ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 1468ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson btif_disable_bluetooth(); 1478ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 1488ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson future_await(hack_future); 1498df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s finished.", __func__); 150efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson btif_thread_post(event_signal_stack_down, NULL); 1518df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 1528df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1538df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void ensure_stack_is_not_running(void) { 1548df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (stack_is_running) { 1558df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGW("%s found the stack was still running. Bringing it down now.", __func__); 1568df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson event_shut_down_stack(NULL); 1578df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson } 1588df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 1598df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1608df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to clean up the stack 1618df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_clean_up_stack(UNUSED_ATTR void *context) { 1628df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (!stack_is_initialized) { 1638df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s found the stack already in a clean state.", __func__); 1648df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson return; 1658df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson } 1668df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1678df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ensure_stack_is_not_running(); 1688df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1698ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson ALOGD("%s is cleaning up the stack.", __func__); 1708ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson hack_future = future_new(); 1718df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson stack_is_initialized = false; 1728ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 1738ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson btif_shutdown_bluetooth(); 1748ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 1758ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson future_await(hack_future); 1768675d884479deca722ef5d7776a165241457dd75Zach Johnson module_management_stop(); 1778df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGD("%s finished.", __func__); 1788df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 1798df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 180efea7821780cdea2b30e944b08e418d1f2781878Zach Johnsonstatic void event_signal_stack_up(UNUSED_ATTR void *context) { 181efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_ON); 182efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson} 183efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson 184efea7821780cdea2b30e944b08e418d1f2781878Zach Johnsonstatic void event_signal_stack_down(UNUSED_ATTR void *context) { 185efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF); 186efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson} 187efea7821780cdea2b30e944b08e418d1f2781878Zach Johnson 1888df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void ensure_manager_initialized(void) { 1898df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (management_thread) 1908df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson return; 1918df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1928df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson management_thread = thread_new("stack_manager"); 1938df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson if (!management_thread) { 1948df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ALOGE("%s unable to create stack management thread.", __func__); 1958df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson return; 1968df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson } 1978df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 1988df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 1998df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic const stack_manager_t interface = { 2008df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson init_stack, 2018df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson start_up_stack_async, 2028df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson shut_down_stack_async, 203f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson clean_up_stack_async, 204f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson 205f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson get_stack_is_running 2068df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}; 2078df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson 2088df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonconst stack_manager_t *stack_manager_get_interface() { 2098df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ensure_manager_initialized(); 2108df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson return &interface; 2118df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson} 2128ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson 2138ea8188a481504641fc6a72087704eb808d0bd87Zach Johnsonfuture_t *stack_manager_get_hack_future() { 2148ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson return hack_future; 2158ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson} 216