blob: 9083e9c429b7024b1ada6880ee51e8d177884cea [file] [log] [blame]
Chris Mantona4020612020-01-14 16:34:08 -08001/*
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
Chris Manton25cc8282020-02-18 07:56:15 -080024#include "test/headless/get_options.h"
Chris Mantona4020612020-01-14 16:34:08 -080025#include "test/headless/headless.h"
26
27extern bt_interface_t bluetoothInterface;
28
29using namespace bluetooth::test::headless;
30
31namespace {
32std::mutex adapter_state_mutex_;
33std::condition_variable adapter_state_cv_;
34bt_state_t bt_state_{BT_STATE_OFF};
35
36void 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}
41void adapter_properties(bt_status_t status, int num_properties,
42 bt_property_t* properties) {
43 LOG_INFO(LOG_TAG, "%s", __func__);
44}
45
46void remote_device_properties(bt_status_t status, RawAddress* bd_addr,
47 int num_properties, bt_property_t* properties) {
48 LOG_INFO(LOG_TAG, "%s", __func__);
49}
50
51void device_found(int num_properties, bt_property_t* properties) {
52 LOG_INFO(LOG_TAG, "%s", __func__);
53}
54
55void discovery_state_changed(bt_discovery_state_t state) {
56 LOG_INFO(LOG_TAG, "%s", __func__);
57}
58
59/** Bluetooth Legacy PinKey Request callback */
60void pin_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
61 bool min_16_digit) {
62 LOG_INFO(LOG_TAG, "%s", __func__);
63}
64
65void 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(LOG_TAG, "%s", __func__);
68}
69
70/** Bluetooth Bond state changed callback */
71/* Invoked in response to create_bond, cancel_bond or remove_bond */
72void bond_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
73 bt_bond_state_t state) {
74 LOG_INFO(LOG_TAG, "%s", __func__);
75}
76
77/** Bluetooth ACL connection state changed callback */
78void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
79 bt_acl_state_t state) {
80 LOG_INFO(LOG_TAG, "%s", __func__);
81}
82
83void thread_event(bt_cb_thread_evt evt) { LOG_INFO(LOG_TAG, "%s", __func__); }
84
85void dut_mode_recv(uint16_t opcode, uint8_t* buf, uint8_t len) {
86 LOG_INFO(LOG_TAG, "%s", __func__);
87}
88
89void le_test_mode(bt_status_t status, uint16_t num_packets) {
90 LOG_INFO(LOG_TAG, "%s", __func__);
91}
92
93void energy_info(bt_activity_energy_info* energy_info,
94 bt_uid_traffic_t* uid_data) {
95 LOG_INFO(LOG_TAG, "%s", __func__);
96}
97
98bt_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
118bool set_wake_alarm_co(uint64_t delay_millis, bool should_wake, alarm_cb cb,
119 void* data) {
120 LOG_INFO(LOG_TAG, "%s", __func__);
121 return true;
122}
123int acquire_wake_lock_co(const char* lock_name) {
124 LOG_INFO(LOG_TAG, "%s", __func__);
125 return 1;
126}
127
128int release_wake_lock_co(const char* lock_name) {
129 LOG_INFO(LOG_TAG, "%s", __func__);
130 return 0;
131}
132
133bt_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
Chris Manton25cc8282020-02-18 07:56:15 -0800141void HeadlessStack::SetUp() {
Chris Mantona4020612020-01-14 16:34:08 -0800142 LOG(INFO) << __func__ << " Entry";
143
Rahul Sabnise198eb92020-07-31 19:44:27 -0700144 int status = bluetoothInterface.init(&bt_callbacks, false, false, 0, false);
Chris Mantona4020612020-01-14 16:34:08 -0800145 (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();
Chris Manton25cc8282020-02-18 07:56:15 -0800155 LOG_INFO(LOG_TAG, "%s HeadlessStack stack has enabled", __func__);
Chris Mantona4020612020-01-14 16:34:08 -0800156
157 std::unique_lock<std::mutex> lck(adapter_state_mutex_);
158 while (bt_state_ != BT_STATE_ON) adapter_state_cv_.wait(lck);
Chris Manton25cc8282020-02-18 07:56:15 -0800159 LOG_INFO(LOG_TAG, "%s HeadlessStack stack is operational", __func__);
Chris Mantona4020612020-01-14 16:34:08 -0800160}
161
Chris Manton25cc8282020-02-18 07:56:15 -0800162void HeadlessStack::TearDown() {
Chris Mantona4020612020-01-14 16:34:08 -0800163 LOG_INFO(LOG_TAG, "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);
Chris Manton25cc8282020-02-18 07:56:15 -0800173 LOG_INFO(LOG_TAG, "%s HeadlessStack stack has exited", __func__);
Chris Mantona4020612020-01-14 16:34:08 -0800174}