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