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 <dlfcn.h> // dlopen
20
21 #include "base/logging.h" // LOG() stdout and android log
22 #include "include/hardware/bluetooth.h"
23 #include "osi/include/log.h" // android log only
24 #include "test/headless/get_options.h"
25 #include "test/headless/headless.h"
26
27 extern bt_interface_t bluetoothInterface;
28
29 using namespace bluetooth::test::headless;
30
31 namespace {
32 std::mutex adapter_state_mutex_;
33 std::condition_variable adapter_state_cv_;
34 bt_state_t bt_state_{BT_STATE_OFF};
35
adapter_state_changed(bt_state_t state)36 void adapter_state_changed(bt_state_t state) {
37 std::unique_lock<std::mutex> lck(adapter_state_mutex_);
38 bt_state_ = state;
39 adapter_state_cv_.notify_all();
40 }
adapter_properties(bt_status_t status,int num_properties,bt_property_t * properties)41 void adapter_properties(bt_status_t status, int num_properties,
42 bt_property_t* properties) {
43 LOG_INFO("%s", __func__);
44 }
45
remote_device_properties(bt_status_t status,RawAddress * bd_addr,int num_properties,bt_property_t * properties)46 void remote_device_properties(bt_status_t status, RawAddress* bd_addr,
47 int num_properties, bt_property_t* properties) {
48 LOG_INFO("%s", __func__);
49 }
50
device_found(int num_properties,bt_property_t * properties)51 void device_found(int num_properties, bt_property_t* properties) {
52 LOG_INFO("%s", __func__);
53 }
54
discovery_state_changed(bt_discovery_state_t state)55 void discovery_state_changed(bt_discovery_state_t state) {
56 LOG_INFO("%s", __func__);
57 }
58
59 /** Bluetooth Legacy PinKey Request callback */
pin_request(RawAddress * remote_bd_addr,bt_bdname_t * bd_name,uint32_t cod,bool min_16_digit)60 void pin_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
61 bool min_16_digit) {
62 LOG_INFO("%s", __func__);
63 }
64
ssp_request(RawAddress * remote_bd_addr,bt_bdname_t * bd_name,uint32_t cod,bt_ssp_variant_t pairing_variant,uint32_t pass_key)65 void ssp_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
66 bt_ssp_variant_t pairing_variant, uint32_t pass_key) {
67 LOG_INFO("%s", __func__);
68 }
69
70 /** Bluetooth Bond state changed callback */
71 /* 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)72 void bond_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
73 bt_bond_state_t state) {
74 LOG_INFO("%s", __func__);
75 }
76
77 /** Bluetooth ACL connection state changed callback */
acl_state_changed(bt_status_t status,RawAddress * remote_bd_addr,bt_acl_state_t state)78 void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
79 bt_acl_state_t state) {
80 LOG_INFO("%s", __func__);
81 }
82
thread_event(bt_cb_thread_evt evt)83 void thread_event(bt_cb_thread_evt evt) { LOG_INFO("%s", __func__); }
84
dut_mode_recv(uint16_t opcode,uint8_t * buf,uint8_t len)85 void dut_mode_recv(uint16_t opcode, uint8_t* buf, uint8_t len) {
86 LOG_INFO("%s", __func__);
87 }
88
le_test_mode(bt_status_t status,uint16_t num_packets)89 void le_test_mode(bt_status_t status, uint16_t num_packets) {
90 LOG_INFO("%s", __func__);
91 }
92
energy_info(bt_activity_energy_info * energy_info,bt_uid_traffic_t * uid_data)93 void energy_info(bt_activity_energy_info* energy_info,
94 bt_uid_traffic_t* uid_data) {
95 LOG_INFO("%s", __func__);
96 }
97
98 bt_callbacks_t bt_callbacks{
99 /** set to sizeof(bt_callbacks_t) */
100 .size = sizeof(bt_callbacks_t),
101 .adapter_state_changed_cb = adapter_state_changed,
102 .adapter_properties_cb = adapter_properties,
103 .remote_device_properties_cb = remote_device_properties,
104 .device_found_cb = device_found,
105 .discovery_state_changed_cb = discovery_state_changed,
106 .pin_request_cb = pin_request,
107 .ssp_request_cb = ssp_request,
108 .bond_state_changed_cb = bond_state_changed,
109 .acl_state_changed_cb = acl_state_changed,
110 .thread_evt_cb = thread_event,
111 .dut_mode_recv_cb = dut_mode_recv,
112 .le_test_mode_cb = le_test_mode,
113 .energy_info_cb = energy_info,
114 };
115 // HAL HARDWARE CALLBACKS
116
117 // OS CALLOUTS
set_wake_alarm_co(uint64_t delay_millis,bool should_wake,alarm_cb cb,void * data)118 bool set_wake_alarm_co(uint64_t delay_millis, bool should_wake, alarm_cb cb,
119 void* data) {
120 LOG_INFO("%s", __func__);
121 return true;
122 }
acquire_wake_lock_co(const char * lock_name)123 int acquire_wake_lock_co(const char* lock_name) {
124 LOG_INFO("%s", __func__);
125 return 1;
126 }
127
release_wake_lock_co(const char * lock_name)128 int release_wake_lock_co(const char* lock_name) {
129 LOG_INFO("%s", __func__);
130 return 0;
131 }
132
133 bt_os_callouts_t bt_os_callouts{
134 .size = sizeof(bt_os_callouts_t),
135 .set_wake_alarm = set_wake_alarm_co,
136 .acquire_wake_lock = acquire_wake_lock_co,
137 .release_wake_lock = release_wake_lock_co,
138 };
139 } // namespace
140
SetUp()141 void HeadlessStack::SetUp() {
142 LOG(INFO) << __func__ << " Entry";
143
144 int status = bluetoothInterface.init(&bt_callbacks, false, false, 0, nullptr);
145 (status == BT_STATUS_SUCCESS)
146 ? LOG(INFO) << __func__ << " Initialized bluetooth callbacks"
147 : LOG(FATAL) << "Failed to initialize Bluetooth stack";
148
149 status = bluetoothInterface.set_os_callouts(&bt_os_callouts);
150 (status == BT_STATUS_SUCCESS)
151 ? LOG(INFO) << __func__ << " Initialized os callouts"
152 : LOG(ERROR) << "Failed to set up Bluetooth OS callouts";
153
154 bluetoothInterface.enable();
155 LOG_INFO("%s HeadlessStack stack has enabled", __func__);
156
157 std::unique_lock<std::mutex> lck(adapter_state_mutex_);
158 while (bt_state_ != BT_STATE_ON) adapter_state_cv_.wait(lck);
159 LOG_INFO("%s HeadlessStack stack is operational", __func__);
160 }
161
TearDown()162 void HeadlessStack::TearDown() {
163 LOG_INFO("Stack has disabled");
164 int status = bluetoothInterface.disable();
165
166 LOG(INFO) << __func__ << " Interface has been disabled status:" << status;
167
168 bluetoothInterface.cleanup();
169 LOG(INFO) << __func__ << " Cleaned up hal bluetooth library";
170
171 std::unique_lock<std::mutex> lck(adapter_state_mutex_);
172 while (bt_state_ != BT_STATE_OFF) adapter_state_cv_.wait(lck);
173 LOG_INFO("%s HeadlessStack stack has exited", __func__);
174 }
175