1 /******************************************************************************
2  *
3  *  Copyright (C) 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 extern fixed_queue_t* btif_msg_queue;
43 
44 // Communication queue from bta thread to bt_workqueue.
45 fixed_queue_t* btu_bta_msg_queue;
46 
47 // Communication queue from hci thread to bt_workqueue.
48 extern fixed_queue_t* btu_hci_msg_queue;
49 
50 // General timer queue.
51 fixed_queue_t* btu_general_alarm_queue;
52 
53 thread_t* bt_workqueue_thread;
54 static const char* BT_WORKQUEUE_NAME = "bt_workqueue";
55 
56 extern void PLATFORM_DisableHciTransport(uint8_t bDisable);
57 /*****************************************************************************
58  *                          V A R I A B L E S                                *
59  *****************************************************************************/
60 // TODO(cmanton) Move this out of this file
61 const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
62 
63 void btu_task_start_up(void* context);
64 void btu_task_shut_down(void* context);
65 
66 /*****************************************************************************
67  *
68  * Function         btu_init_core
69  *
70  * Description      Initialize control block memory for each core component.
71  *
72  *
73  * Returns          void
74  *
75  *****************************************************************************/
btu_init_core(void)76 void btu_init_core(void) {
77   /* Initialize the mandatory core stack components */
78   btm_init();
79 
80   l2c_init();
81 
82   sdp_init();
83 
84   gatt_init();
85 
86   SMP_Init();
87 
88   btm_ble_init();
89 }
90 
91 /*****************************************************************************
92  *
93  * Function         btu_free_core
94  *
95  * Description      Releases control block memory for each core component.
96  *
97  *
98  * Returns          void
99  *
100  *****************************************************************************/
btu_free_core(void)101 void btu_free_core(void) {
102   /* Free the mandatory core stack components */
103   l2c_free();
104 
105   gatt_free();
106 }
107 
108 /*****************************************************************************
109  *
110  * Function         BTU_StartUp
111  *
112  * Description      Initializes the BTU control block.
113  *
114  *                  NOTE: Must be called before creating any tasks
115  *                      (RPC, BTU, HCIT, APPL, etc.)
116  *
117  * Returns          void
118  *
119  *****************************************************************************/
BTU_StartUp(void)120 void BTU_StartUp(void) {
121   btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
122 
123   btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
124   if (btu_bta_msg_queue == NULL) goto error_exit;
125 
126   btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
127   if (btu_general_alarm_queue == NULL) goto error_exit;
128 
129   bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
130   if (bt_workqueue_thread == NULL) goto error_exit;
131 
132   thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
133 
134   // Continue startup on bt workqueue thread.
135   thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
136   return;
137 
138 error_exit:;
139   LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue",
140             __func__);
141   BTU_ShutDown();
142 }
143 
BTU_ShutDown(void)144 void BTU_ShutDown(void) {
145   btu_task_shut_down(NULL);
146 
147   fixed_queue_free(btu_bta_msg_queue, NULL);
148   btu_bta_msg_queue = NULL;
149 
150   fixed_queue_free(btu_general_alarm_queue, NULL);
151   btu_general_alarm_queue = NULL;
152 
153   thread_free(bt_workqueue_thread);
154 
155   bt_workqueue_thread = NULL;
156 }
157