1 /*
2 * Copyright 2021 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 #include "btif/include/btif_profile_queue.h"
18
19 #include <gtest/gtest.h>
20
21 #include <base/bind.h>
22 #include <base/callback.h>
23 #include <base/location.h>
24 #include "stack_manager.h"
25 #include "types/raw_address.h"
26
27 #include "bta/include/bta_ag_api.h" // tBTA_AG_RES_DATA::kEmpty
28 #include "hci/include/hci_layer.h" // hci_t
29 #include "test/mock/mock_hci_layer.h"
30
31 std::map<std::string, int> mock_function_count_map;
32
33 void set_hal_cbacks(bt_callbacks_t* callbacks); // btif/src/bluetooth.cc
34
35 // tLEGACY_TRACE_LEVEL
36 uint8_t btu_trace_level = 6;
37 uint8_t appl_trace_level = 6;
38 uint8_t btif_trace_level = 6;
39
40 namespace {
41
dump_mock_function_count_map()42 void dump_mock_function_count_map() {
43 LOG_INFO("Mock function count map size:%zu", mock_function_count_map.size());
44
45 for (auto it : mock_function_count_map) {
46 LOG_INFO("function:%s: call_count:%d", it.first.c_str(), it.second);
47 }
48 }
49
50 namespace _adapter_state_changed {
51 bt_state_t state{BT_STATE_OFF};
52 }
adapter_state_changed(bt_state_t state)53 void adapter_state_changed(bt_state_t state) {
54 LOG_INFO("%u => %u", _adapter_state_changed::state, state);
55 }
56
adapter_properties(bt_status_t status,int num_properties,bt_property_t * properties)57 void adapter_properties(bt_status_t status, int num_properties,
58 bt_property_t* properties) {
59 LOG_INFO("Callback rx");
60 }
61
remote_device_properties(bt_status_t status,RawAddress * bd_addr,int num_properties,bt_property_t * properties)62 void remote_device_properties(bt_status_t status, RawAddress* bd_addr,
63 int num_properties, bt_property_t* properties) {
64 LOG_INFO("Callback rx");
65 }
66
device_found(int num_properties,bt_property_t * properties)67 void device_found(int num_properties, bt_property_t* properties) {
68 LOG_INFO("Callback rx");
69 }
70
discovery_state_changed(bt_discovery_state_t state)71 void discovery_state_changed(bt_discovery_state_t state) {
72 LOG_INFO("Callback rx");
73 }
74
75 /** Bluetooth Legacy PinKey Request callback */
pin_request(RawAddress * remote_bd_addr,bt_bdname_t * bd_name,uint32_t cod,bool min_16_digit)76 void pin_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
77 bool min_16_digit) {
78 LOG_INFO("Callback rx");
79 }
80
ssp_request(RawAddress * remote_bd_addr,bt_bdname_t * bd_name,uint32_t cod,bt_ssp_variant_t pairing_variant,uint32_t pass_key)81 void ssp_request(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
82 bt_ssp_variant_t pairing_variant, uint32_t pass_key) {
83 LOG_INFO("Callback rx");
84 }
85
86 /** Bluetooth Bond state changed callback */
87 /* 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)88 void bond_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
89 bt_bond_state_t state) {
90 LOG_INFO("Callback rx");
91 }
92
93 /** Bluetooth ACL connection state changed callback */
acl_state_changed(bt_status_t status,RawAddress * remote_bd_addr,bt_acl_state_t state,bt_hci_error_code_t hci_reason)94 void acl_state_changed(bt_status_t status, RawAddress* remote_bd_addr,
95 bt_acl_state_t state, bt_hci_error_code_t hci_reason) {
96 LOG_INFO("status:%s device:%s state:%s", bt_status_text(status).c_str(),
97 remote_bd_addr->ToString().c_str(),
98 (state) ? "disconnected" : "connected");
99 }
100
101 /** 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)102 void link_quality_report(uint64_t timestamp, int report_id, int rssi, int snr,
103 int retransmission_count,
104 int packets_not_receive_count,
105 int negative_acknowledgement_count) {
106 LOG_INFO("Callback rx");
107 }
108
thread_event(bt_cb_thread_evt evt)109 void thread_event(bt_cb_thread_evt evt) { LOG_INFO("Callback rx"); }
110
dut_mode_recv(uint16_t opcode,uint8_t * buf,uint8_t len)111 void dut_mode_recv(uint16_t opcode, uint8_t* buf, uint8_t len) {
112 LOG_INFO("Callback rx");
113 }
114
le_test_mode(bt_status_t status,uint16_t num_packets)115 void le_test_mode(bt_status_t status, uint16_t num_packets) {
116 LOG_INFO("Callback rx");
117 }
118
energy_info(bt_activity_energy_info * energy_info,bt_uid_traffic_t * uid_data)119 void energy_info(bt_activity_energy_info* energy_info,
120 bt_uid_traffic_t* uid_data) {
121 LOG_INFO("Callback rx");
122 }
123
124 bt_callbacks_t bt_callbacks{
125 /** set to sizeof(bt_callbacks_t) */
126 .size = sizeof(bt_callbacks_t),
127 .adapter_state_changed_cb = adapter_state_changed,
128 .adapter_properties_cb = adapter_properties,
129 .remote_device_properties_cb = remote_device_properties,
130 .device_found_cb = device_found,
131 .discovery_state_changed_cb = discovery_state_changed,
132 .pin_request_cb = pin_request,
133 .ssp_request_cb = ssp_request,
134 .bond_state_changed_cb = bond_state_changed,
135 .acl_state_changed_cb = acl_state_changed,
136 .thread_evt_cb = thread_event,
137 .dut_mode_recv_cb = dut_mode_recv,
138 .le_test_mode_cb = le_test_mode,
139 .energy_info_cb = energy_info,
140 .link_quality_report_cb = link_quality_report,
141 };
142
set_data_cb(base::Callback<void (const base::Location &,BT_HDR *)> send_data_cb)143 void set_data_cb(
144 base::Callback<void(const base::Location&, BT_HDR*)> send_data_cb) {
145 mock_function_count_map[__func__]++;
146 }
147
transmit_command(BT_HDR * command,command_complete_cb complete_callback,command_status_cb status_cb,void * context)148 void transmit_command(BT_HDR* command, command_complete_cb complete_callback,
149 command_status_cb status_cb, void* context) {
150 mock_function_count_map[__func__]++;
151 }
152
transmit_command_futured(BT_HDR * command)153 future_t* transmit_command_futured(BT_HDR* command) {
154 mock_function_count_map[__func__]++;
155 return nullptr;
156 }
157
transmit_downward(uint16_t type,void * data)158 void transmit_downward(uint16_t type, void* data) {
159 mock_function_count_map[__func__]++;
160 }
161
162 } // namespace
163
164 hci_t mock_hci = {
165 .set_data_cb = set_data_cb,
166 .transmit_command = transmit_command,
167 .transmit_command_futured = transmit_command_futured,
168 .transmit_downward = transmit_downward,
169 };
170
is_bluetooth_uid()171 bool is_bluetooth_uid() { return false; }
172 const tBTA_AG_RES_DATA tBTA_AG_RES_DATA::kEmpty = {};
173
174 namespace bluetooth {
175 namespace common {
176
177 class BluetoothMetricsLogger {};
178
179 } // namespace common
180 } // namespace bluetooth
181
182 class StackCycleTest : public ::testing::Test {
183 protected:
SetUp()184 void SetUp() override {
185 test::mock::hci_layer::hci_layer_get_interface.hci = &mock_hci;
186 stack_manager_ = stack_manager_get_interface();
187 }
188
TearDown()189 void TearDown() override { stack_manager_ = nullptr; }
190 const stack_manager_t* stack_manager_{nullptr};
191 };
192
TEST_F(StackCycleTest,stack_init)193 TEST_F(StackCycleTest, stack_init) {
194 // TODO load init flags
195 // bluetooth::common::InitFlags::Load(init_flags);
196
197 set_hal_cbacks(&bt_callbacks);
198
199 stack_manager_get_interface()->init_stack();
200
201 LOG_INFO("Initialized stack");
202
203 ASSERT_EQ(1, mock_function_count_map["set_data_cb"]);
204
205 dump_mock_function_count_map();
206 }
207