1 /****************************************************************************** 2 * 3 * Copyright 2000-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 #define LOG_TAG "bt_task" 20 21 #include <base/logging.h> 22 #include <pthread.h> 23 #include <string.h> 24 25 #include "bt_target.h" 26 #include "btm_int.h" 27 #include "btu.h" 28 #include "device/include/controller.h" 29 #include "gatt_api.h" 30 #include "gatt_int.h" 31 #include "l2c_int.h" 32 #include "osi/include/alarm.h" 33 #include "osi/include/fixed_queue.h" 34 #include "osi/include/log.h" 35 #include "osi/include/thread.h" 36 #include "sdpint.h" 37 #include "smp_int.h" 38 39 // RT priority for audio-related tasks 40 #define BTU_TASK_RT_PRIORITY 1 41 42 // Communication queue from hci thread to bt_workqueue. 43 extern fixed_queue_t* btu_hci_msg_queue; 44 45 thread_t* bt_workqueue_thread; 46 static const char* BT_WORKQUEUE_NAME = "bt_workqueue"; 47 48 extern void PLATFORM_DisableHciTransport(uint8_t bDisable); 49 50 void btu_task_start_up(void* context); 51 void btu_task_shut_down(void* context); 52 53 /***************************************************************************** 54 * 55 * Function btu_init_core 56 * 57 * Description Initialize control block memory for each core component. 58 * 59 * 60 * Returns void 61 * 62 *****************************************************************************/ 63 void btu_init_core(void) { 64 /* Initialize the mandatory core stack components */ 65 btm_init(); 66 67 l2c_init(); 68 69 sdp_init(); 70 71 gatt_init(); 72 73 SMP_Init(); 74 75 btm_ble_init(); 76 } 77 78 /***************************************************************************** 79 * 80 * Function btu_free_core 81 * 82 * Description Releases control block memory for each core component. 83 * 84 * 85 * Returns void 86 * 87 *****************************************************************************/ 88 void btu_free_core(void) { 89 /* Free the mandatory core stack components */ 90 gatt_free(); 91 92 l2c_free(); 93 94 sdp_free(); 95 96 btm_free(); 97 } 98 99 /***************************************************************************** 100 * 101 * Function BTU_StartUp 102 * 103 * Description Initializes the BTU control block. 104 * 105 * NOTE: Must be called before creating any tasks 106 * (RPC, BTU, HCIT, APPL, etc.) 107 * 108 * Returns void 109 * 110 *****************************************************************************/ 111 void BTU_StartUp(void) { 112 btu_trace_level = HCI_INITIAL_TRACE_LEVEL; 113 114 bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME); 115 if (bt_workqueue_thread == NULL) goto error_exit; 116 117 thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY); 118 119 // Continue startup on bt workqueue thread. 120 thread_post(bt_workqueue_thread, btu_task_start_up, NULL); 121 return; 122 123 error_exit:; 124 LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue", 125 __func__); 126 BTU_ShutDown(); 127 } 128 129 void BTU_ShutDown(void) { 130 btu_task_shut_down(NULL); 131 132 133 thread_free(bt_workqueue_thread); 134 135 bt_workqueue_thread = NULL; 136 } 137