1 /******************************************************************************
2 *
3 * Copyright (C) 1999-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_btu_task"
20
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "bt_common.h"
27 #include "bt_target.h"
28 #include "bt_trace.h"
29 #include "bt_types.h"
30 #include "bt_utils.h"
31 #include "btcore/include/module.h"
32 #include "bte.h"
33 #include "btif_common.h"
34 #include "btm_api.h"
35 #include "btm_int.h"
36 #include "btu.h"
37 #include "gap_int.h"
38 #include "hcimsgs.h"
39 #include "l2c_int.h"
40 #include "osi/include/alarm.h"
41 #include "osi/include/fixed_queue.h"
42 #include "osi/include/future.h"
43 #include "osi/include/log.h"
44 #include "osi/include/osi.h"
45 #include "osi/include/thread.h"
46 #include "port_api.h"
47 #include "port_ext.h"
48 #include "sdpint.h"
49
50 #if (BNEP_INCLUDED == TRUE)
51 #include "bnep_int.h"
52 #endif
53
54 #if (PAN_INCLUDED == TRUE)
55 #include "pan_int.h"
56 #endif
57
58 #if (HID_HOST_INCLUDED == TRUE)
59 #include "hidh_int.h"
60 #endif
61
62 #if (AVDT_INCLUDED == TRUE)
63 #include "avdt_int.h"
64 #else
65 extern void avdt_rcv_sync_info(BT_HDR* p_buf); /* this is for hci_test */
66 #endif
67
68 #if (MCA_INCLUDED == TRUE)
69 #include "mca_api.h"
70 #include "mca_defs.h"
71 #include "mca_int.h"
72 #endif
73
74 #include "bta_sys.h"
75 #include "btm_ble_int.h"
76 #include "gatt_int.h"
77 #include "smp_int.h"
78
79 /* Define BTU storage area
80 */
81 uint8_t btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
82
83 // Communication queue between btu_task and bta.
84 extern fixed_queue_t* btu_bta_msg_queue;
85
86 // Communication queue between btu_task and hci.
87 extern fixed_queue_t* btu_hci_msg_queue;
88
89 // General timer queue.
90 extern fixed_queue_t* btu_general_alarm_queue;
91
92 extern fixed_queue_t* event_queue;
93 extern fixed_queue_t* btif_msg_queue;
94
95 extern thread_t* bt_workqueue_thread;
96
97 static void btu_hci_msg_process(BT_HDR* p_msg);
98
btu_hci_msg_ready(fixed_queue_t * queue,UNUSED_ATTR void * context)99 void btu_hci_msg_ready(fixed_queue_t* queue, UNUSED_ATTR void* context) {
100 BT_HDR* p_msg = (BT_HDR*)fixed_queue_dequeue(queue);
101 btu_hci_msg_process(p_msg);
102 }
103
btu_bta_msg_ready(fixed_queue_t * queue,UNUSED_ATTR void * context)104 void btu_bta_msg_ready(fixed_queue_t* queue, UNUSED_ATTR void* context) {
105 BT_HDR* p_msg = (BT_HDR*)fixed_queue_dequeue(queue);
106 bta_sys_event(p_msg);
107 }
108
btu_hci_msg_process(BT_HDR * p_msg)109 static void btu_hci_msg_process(BT_HDR* p_msg) {
110 /* Determine the input message type. */
111 switch (p_msg->event & BT_EVT_MASK) {
112 case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove
113 // this
114 ((post_to_task_hack_t*)(&p_msg->data[0]))->callback(p_msg);
115 break;
116 case BT_EVT_TO_BTU_HCI_ACL:
117 /* All Acl Data goes to L2CAP */
118 l2c_rcv_acl_data(p_msg);
119 break;
120
121 case BT_EVT_TO_BTU_L2C_SEG_XMIT:
122 /* L2CAP segment transmit complete */
123 l2c_link_segments_xmitted(p_msg);
124 break;
125
126 case BT_EVT_TO_BTU_HCI_SCO:
127 #if (BTM_SCO_INCLUDED == TRUE)
128 btm_route_sco_data(p_msg);
129 break;
130 #endif
131
132 case BT_EVT_TO_BTU_HCI_EVT:
133 btu_hcif_process_event((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
134 osi_free(p_msg);
135 break;
136
137 case BT_EVT_TO_BTU_HCI_CMD:
138 btu_hcif_send_cmd((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
139 break;
140
141 default:
142 osi_free(p_msg);
143 break;
144 }
145 }
146
btu_task_start_up(UNUSED_ATTR void * context)147 void btu_task_start_up(UNUSED_ATTR void* context) {
148 BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
149 "btu_task pending for preload complete event");
150
151 LOG_INFO(LOG_TAG, "Bluetooth chip preload is complete");
152
153 BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
154 "btu_task received preload complete event");
155
156 /* Initialize the mandatory core stack control blocks
157 (BTU, BTM, L2CAP, and SDP)
158 */
159 btu_init_core();
160
161 /* Initialize any optional stack components */
162 BTE_InitStack();
163
164 bta_sys_init();
165
166 /* Initialise platform trace levels at this point as BTE_InitStack() and
167 * bta_sys_init()
168 * reset the control blocks and preset the trace level with
169 * XXX_INITIAL_TRACE_LEVEL
170 */
171 module_init(get_module(BTE_LOGMSG_MODULE));
172
173 // Inform the bt jni thread initialization is ok.
174 btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
175
176 fixed_queue_register_dequeue(btu_bta_msg_queue,
177 thread_get_reactor(bt_workqueue_thread),
178 btu_bta_msg_ready, NULL);
179
180 fixed_queue_register_dequeue(btu_hci_msg_queue,
181 thread_get_reactor(bt_workqueue_thread),
182 btu_hci_msg_ready, NULL);
183
184 alarm_register_processing_queue(btu_general_alarm_queue, bt_workqueue_thread);
185 }
186
btu_task_shut_down(UNUSED_ATTR void * context)187 void btu_task_shut_down(UNUSED_ATTR void* context) {
188 fixed_queue_unregister_dequeue(btu_bta_msg_queue);
189 fixed_queue_unregister_dequeue(btu_hci_msg_queue);
190 alarm_unregister_processing_queue(btu_general_alarm_queue);
191
192 module_clean_up(get_module(BTE_LOGMSG_MODULE));
193
194 bta_sys_free();
195 btu_free_core();
196 }
197