1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bt_headless"
18
19 #include "test/headless/headless.h"
20
21 #include <dlfcn.h> // dlopen
22
23 #include <algorithm>
24 #include <iostream>
25 #include <map>
26
27 #include "base/logging.h" // LOG() stdout and android log
28 #include "include/hardware/bluetooth.h"
29 #include "osi/include/log.h" // android log only
30 #include "test/headless/get_options.h"
31 #include "test/headless/interface.h"
32 #include "types/raw_address.h"
33
34 extern bt_interface_t bluetoothInterface;
35
36 using namespace bluetooth::test::headless;
37
38 std::map<const std::string, std::list<callback_function_t>>
39 interface_api_callback_map_;
40
headless_add_callback(const std::string interface_name,callback_function_t function)41 void headless_add_callback(const std::string interface_name,
42 callback_function_t function) {
43 if (interface_api_callback_map_.find(interface_name) ==
44 interface_api_callback_map_.end()) {
45 interface_api_callback_map_.emplace(interface_name,
46 std::list<callback_function_t>());
47 }
48 interface_api_callback_map_[interface_name].push_back(function);
49 }
50
headless_remove_callback(const std::string interface_name,callback_function_t function)51 void headless_remove_callback(const std::string interface_name,
52 callback_function_t function) {
53 if (interface_api_callback_map_.find(interface_name) ==
54 interface_api_callback_map_.end()) {
55 ASSERT_LOG(false, "No callbacks registered for interface:%s",
56 interface_name.c_str());
57 }
58 interface_api_callback_map_[interface_name].remove(function);
59 }
60
61 namespace {
62 std::mutex adapter_state_mutex_;
63 std::condition_variable adapter_state_cv_;
64 bt_state_t bt_state_{BT_STATE_OFF};
65
adapter_state_changed(bt_state_t state)66 void adapter_state_changed(bt_state_t state) {
67 std::unique_lock<std::mutex> lck(adapter_state_mutex_);
68 bt_state_ = state;
69 adapter_state_cv_.notify_all();
70 }
adapter_properties(bt_status_t status,int num_properties,bt_property_t * properties)71 void adapter_properties(bt_status_t status, int num_properties,
72 bt_property_t* properties) {
73 LOG_INFO("%s", __func__);
74 }
75
remote_device_properties(bt_status_t status,RawAddress * bd_addr,int num_properties,bt_property_t * properties)76 void remote_device_properties(bt_status_t status, RawAddress* bd_addr,
77 int num_properties, bt_property_t* properties) {
78 LOG_INFO("%s", __func__);
79 }
80
device_found(int num_properties,bt_property_t * properties)81 void device_found(int num_properties, bt_property_t* properties) {
82 LOG_INFO("%s", __func__);
83 }
84
discovery_state_changed(bt_discovery_state_t state)85 void discovery_state_changed(bt_discovery_state_t state) {
86 LOG_INFO("%s", __func__);
87 }
88
89 /** Bluetooth Legacy PinKey Request callback */
pin_request(RawAddress * remote_bd_addr,bt_bdname_t * bd_name,uint32_t cod,bool min_16_digit)90 void pin_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
91 bool min_16_digit) {
92 LOG_INFO("%s", __func__);
93 }
94
ssp_request(RawAddress * remote_bd_addr,bt_bdname_t * bd_name,uint32_t cod,bt_ssp_variant_t pairing_variant,uint32_t pass_key)95 void ssp_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
96 bt_ssp_variant_t pairing_variant, uint32_t pass_key) {
97 LOG_INFO("%s", __func__);
98 }
99
100 /** Bluetooth Bond state changed callback */
101 /* Invoked in response to create_bond, cancel_bond or remove_bond */
bond_state_changed(bt_status_t status,RawAddress * remote_bd_addr,bt_bond_state_t state,int fail_reason)102 void bond_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
103 bt_bond_state_t state, int fail_reason) {
104 LOG_INFO("%s", __func__);
105 }
106
address_consolidate(RawAddress * main_bd_addr,RawAddress * secondary_bd_addr)107 void address_consolidate(RawAddress* main_bd_addr,
108 RawAddress* secondary_bd_addr) {
109 LOG_INFO("%s", __func__);
110 }
111
le_address_associate(RawAddress * main_bd_addr,RawAddress * secondary_bd_addr)112 void le_address_associate(RawAddress* main_bd_addr,
113 RawAddress* secondary_bd_addr) {
114 LOG_INFO("%s", __func__);
115 }
116
117 /** Bluetooth ACL connection state changed callback */
acl_state_changed(bt_status_t status,RawAddress * remote_bd_addr,bt_acl_state_t state,int transport_link_type,bt_hci_error_code_t hci_reason)118 void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
119 bt_acl_state_t state, int transport_link_type,
120 bt_hci_error_code_t hci_reason) {
121 auto callback_list = interface_api_callback_map_.at(__func__);
122 for (auto callback : callback_list) {
123 interface_data_t params{
124 .name = __func__,
125 .params.acl_state_changed.status = status,
126 .params.acl_state_changed.remote_bd_addr = remote_bd_addr,
127 .params.acl_state_changed.state = state,
128 .params.acl_state_changed.hci_reason = hci_reason,
129 };
130 (callback)(params);
131 }
132 LOG_INFO("%s status:%s device:%s state:%s", __func__,
133 bt_status_text(status).c_str(), remote_bd_addr->ToString().c_str(),
134 (state) ? "disconnected" : "connected");
135 }
136
137 /** Bluetooth Link Quality Report callback */
link_quality_report(uint64_t timestamp,int report_id,int rssi,int snr,int retransmission_count,int packets_not_receive_count,int negative_acknowledgement_count)138 void link_quality_report(uint64_t timestamp, int report_id, int rssi, int snr,
139 int retransmission_count, int packets_not_receive_count,
140 int negative_acknowledgement_count) {
141 LOG_INFO("%s", __func__);
142 }
143
144 /** Switch buffer size callback */
switch_buffer_size(bool is_low_latency_buffer_size)145 void switch_buffer_size(bool is_low_latency_buffer_size) {
146 LOG_INFO("%s", __func__);
147 }
148
149 /** Switch codec callback */
switch_codec(bool is_low_latency_buffer_size)150 void switch_codec(bool is_low_latency_buffer_size) { LOG_INFO("%s", __func__); }
151
thread_event(bt_cb_thread_evt evt)152 void thread_event(bt_cb_thread_evt evt) { LOG_INFO("%s", __func__); }
153
dut_mode_recv(uint16_t opcode,uint8_t * buf,uint8_t len)154 void dut_mode_recv(uint16_t opcode, uint8_t* buf, uint8_t len) {
155 LOG_INFO("%s", __func__);
156 }
157
le_test_mode(bt_status_t status,uint16_t num_packets)158 void le_test_mode(bt_status_t status, uint16_t num_packets) {
159 LOG_INFO("%s", __func__);
160 }
161
energy_info(bt_activity_energy_info * energy_info,bt_uid_traffic_t * uid_data)162 void energy_info(bt_activity_energy_info* energy_info,
163 bt_uid_traffic_t* uid_data) {
164 LOG_INFO("%s", __func__);
165 }
166
167 bt_callbacks_t bt_callbacks{
168 /** set to sizeof(bt_callbacks_t) */
169 .size = sizeof(bt_callbacks_t),
170 .adapter_state_changed_cb = adapter_state_changed,
171 .adapter_properties_cb = adapter_properties,
172 .remote_device_properties_cb = remote_device_properties,
173 .device_found_cb = device_found,
174 .discovery_state_changed_cb = discovery_state_changed,
175 .pin_request_cb = pin_request,
176 .ssp_request_cb = ssp_request,
177 .bond_state_changed_cb = bond_state_changed,
178 .address_consolidate_cb = address_consolidate,
179 .le_address_associate_cb = le_address_associate,
180 .acl_state_changed_cb = acl_state_changed,
181 .thread_evt_cb = thread_event,
182 .dut_mode_recv_cb = dut_mode_recv,
183 .le_test_mode_cb = le_test_mode,
184 .energy_info_cb = energy_info,
185 .link_quality_report_cb = link_quality_report,
186 .switch_buffer_size_cb = switch_buffer_size,
187 .switch_codec_cb = switch_codec,
188 };
189 // HAL HARDWARE CALLBACKS
190
191 // OS CALLOUTS
set_wake_alarm_co(uint64_t delay_millis,bool should_wake,alarm_cb cb,void * data)192 bool set_wake_alarm_co(uint64_t delay_millis, bool should_wake, alarm_cb cb,
193 void* data) {
194 LOG_INFO("%s", __func__);
195 return true;
196 }
acquire_wake_lock_co(const char * lock_name)197 int acquire_wake_lock_co(const char* lock_name) {
198 LOG_INFO("%s", __func__);
199 return 1;
200 }
201
release_wake_lock_co(const char * lock_name)202 int release_wake_lock_co(const char* lock_name) {
203 LOG_INFO("%s", __func__);
204 return 0;
205 }
206
207 bt_os_callouts_t bt_os_callouts{
208 .size = sizeof(bt_os_callouts_t),
209 .set_wake_alarm = set_wake_alarm_co,
210 .acquire_wake_lock = acquire_wake_lock_co,
211 .release_wake_lock = release_wake_lock_co,
212 };
213 } // namespace
214
SetUp()215 void HeadlessStack::SetUp() {
216 LOG(INFO) << __func__ << " Entry";
217
218 const bool start_restricted = false;
219 const bool is_common_criteria_mode = false;
220 const int config_compare_result = 0;
221 const bool is_atv = false;
222 int status = bluetoothInterface.init(
223 &bt_callbacks, start_restricted, is_common_criteria_mode,
224 config_compare_result, StackInitFlags(), is_atv, nullptr);
225
226 (status == BT_STATUS_SUCCESS)
227 ? LOG(INFO) << __func__ << " Initialized bluetooth callbacks"
228 : LOG(FATAL) << "Failed to initialize Bluetooth stack";
229
230 status = bluetoothInterface.set_os_callouts(&bt_os_callouts);
231 (status == BT_STATUS_SUCCESS)
232 ? LOG(INFO) << __func__ << " Initialized os callouts"
233 : LOG(ERROR) << "Failed to set up Bluetooth OS callouts";
234
235 bluetoothInterface.enable();
236 LOG_INFO("%s HeadlessStack stack has enabled", __func__);
237
238 std::unique_lock<std::mutex> lck(adapter_state_mutex_);
239 while (bt_state_ != BT_STATE_ON) adapter_state_cv_.wait(lck);
240 LOG_INFO("%s HeadlessStack stack is operational", __func__);
241 }
242
TearDown()243 void HeadlessStack::TearDown() {
244 LOG_INFO("Stack has disabled");
245 int status = bluetoothInterface.disable();
246
247 LOG(INFO) << __func__ << " Interface has been disabled status:" << status;
248
249 bluetoothInterface.cleanup();
250 LOG(INFO) << __func__ << " Cleaned up hal bluetooth library";
251
252 std::unique_lock<std::mutex> lck(adapter_state_mutex_);
253 while (bt_state_ != BT_STATE_OFF) adapter_state_cv_.wait(lck);
254 LOG_INFO("%s HeadlessStack stack has exited", __func__);
255 }
256