• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // 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  *****************************************************************************/
btu_init_core(void)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  *****************************************************************************/
btu_free_core(void)88 void btu_free_core(void) {
89   /* Free the mandatory core stack components */
90   l2c_free();
91 
92   sdp_free();
93 
94   gatt_free();
95 }
96 
97 /*****************************************************************************
98  *
99  * Function         BTU_StartUp
100  *
101  * Description      Initializes the BTU control block.
102  *
103  *                  NOTE: Must be called before creating any tasks
104  *                      (RPC, BTU, HCIT, APPL, etc.)
105  *
106  * Returns          void
107  *
108  *****************************************************************************/
BTU_StartUp(void)109 void BTU_StartUp(void) {
110   btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
111 
112   bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
113   if (bt_workqueue_thread == NULL) goto error_exit;
114 
115   thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
116 
117   // Continue startup on bt workqueue thread.
118   thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
119   return;
120 
121 error_exit:;
122   LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue",
123             __func__);
124   BTU_ShutDown();
125 }
126 
BTU_ShutDown(void)127 void BTU_ShutDown(void) {
128   btu_task_shut_down(NULL);
129 
130 
131   thread_free(bt_workqueue_thread);
132 
133   bt_workqueue_thread = NULL;
134 }
135