1 /*
2  * Copyright 2022 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 "core_interface.h"
18 
19 #include "btif/include/btif_common.h"
20 #include "btif/include/core_callbacks.h"
21 #include "btif/include/stack_manager_t.h"
22 
23 namespace {
24 
25 static bluetooth::core::EventCallbacks eventCallbacks = {
26     .invoke_adapter_state_changed_cb = invoke_adapter_state_changed_cb,
27     .invoke_adapter_properties_cb = invoke_adapter_properties_cb,
28     .invoke_remote_device_properties_cb = invoke_remote_device_properties_cb,
29     .invoke_device_found_cb = invoke_device_found_cb,
30     .invoke_discovery_state_changed_cb = invoke_discovery_state_changed_cb,
31     .invoke_pin_request_cb = invoke_pin_request_cb,
32     .invoke_ssp_request_cb = invoke_ssp_request_cb,
33     .invoke_oob_data_request_cb = invoke_oob_data_request_cb,
34     .invoke_bond_state_changed_cb = invoke_bond_state_changed_cb,
35     .invoke_address_consolidate_cb = invoke_address_consolidate_cb,
36     .invoke_le_address_associate_cb = invoke_le_address_associate_cb,
37     .invoke_acl_state_changed_cb = invoke_acl_state_changed_cb,
38     .invoke_thread_evt_cb = invoke_thread_evt_cb,
39     .invoke_le_test_mode_cb = invoke_le_test_mode_cb,
40     .invoke_energy_info_cb = invoke_energy_info_cb,
41     .invoke_link_quality_report_cb = invoke_link_quality_report_cb,
42     .invoke_key_missing_cb = invoke_key_missing_cb};
43 
44 // This interface lets us query for configuration properties of the stack that
45 // could change at runtime
46 struct MockConfigInterface : public bluetooth::core::ConfigInterface {
isA2DPOffloadEnabled__anone54952220111::MockConfigInterface47   virtual bool isA2DPOffloadEnabled() { return false; }
isAndroidTVDevice__anone54952220111::MockConfigInterface48   virtual bool isAndroidTVDevice() { return false; }
isRestrictedMode__anone54952220111::MockConfigInterface49   virtual bool isRestrictedMode() { return false; }
50 };
51 
52 static auto mockConfigInterface = MockConfigInterface{};
53 
54 // This interface lets us communicate with encoders used in profiles
55 struct MockMsbcCodecInterface : public bluetooth::core::CodecInterface {
initialize__anone54952220111::MockMsbcCodecInterface56   virtual void initialize(){};
cleanup__anone54952220111::MockMsbcCodecInterface57   virtual void cleanup() {}
58 
encodePacket__anone54952220111::MockMsbcCodecInterface59   virtual uint32_t encodePacket(int16_t* /* input */, uint8_t* /* output */) {
60     return 0;
61   };
decodePacket__anone54952220111::MockMsbcCodecInterface62   virtual bool decodePacket(const uint8_t* /* i_buf */, int16_t* /* o_buf */,
63                             size_t /* out_len */) {
64     return false;
65   };
66 };
67 
68 struct MockLc3CodecInterface : public bluetooth::core::CodecInterface {
initialize__anone54952220111::MockLc3CodecInterface69   virtual void initialize(){};
cleanup__anone54952220111::MockLc3CodecInterface70   virtual void cleanup() {}
71 
encodePacket__anone54952220111::MockLc3CodecInterface72   virtual uint32_t encodePacket(int16_t* /* input */, uint8_t* /* output */) {
73     return 0;
74   };
decodePacket__anone54952220111::MockLc3CodecInterface75   virtual bool decodePacket(const uint8_t* /* i_buf */, int16_t* /* o_buf */,
76                             size_t /* out_len */) {
77     return false;
78   };
79 };
80 
81 static auto mockMsbcCodecInterface = MockMsbcCodecInterface{};
82 static auto mockLc3CodecInterface = MockLc3CodecInterface{};
83 
84 struct bluetooth::core::HACK_ProfileInterface HACK_profileInterface = {
85     // HID
86     .btif_hh_connect = nullptr,
87     .btif_hh_virtual_unplug = nullptr,
88     .bta_hh_read_ssr_param = nullptr,
89 
90     // AVDTP
91     .btif_av_set_dynamic_audio_buffer_size = nullptr,
92 
93     // ASHA
94     .GetHearingAidDeviceCount = nullptr,
95 
96     // LE Audio
97     .IsLeAudioClientRunning = nullptr,
98 
99     // AVRCP
100     .AVRC_GetProfileVersion = nullptr,
101 };
102 
103 }  // namespace
104 
InitializeCoreInterface()105 void InitializeCoreInterface() {
106   static auto mockCoreInterface = MockCoreInterface{};
107   stack_manager_get_interface()->init_stack(&mockCoreInterface);
108 }
109 
CleanCoreInterface()110 void CleanCoreInterface() {
111   stack_manager_get_interface()->clean_up_stack([] {});
112 }
113 
MockCoreInterface()114 MockCoreInterface::MockCoreInterface()
115     : bluetooth::core::CoreInterface{
116           &eventCallbacks, &mockConfigInterface, &mockMsbcCodecInterface,
117           &mockLc3CodecInterface, &HACK_profileInterface} {};
118 
onBluetoothEnabled()119 void MockCoreInterface::onBluetoothEnabled(){};
120 
toggleProfile(tBTA_SERVICE_ID,bool)121 bt_status_t MockCoreInterface::toggleProfile(tBTA_SERVICE_ID /* service_id */,
122                                              bool /* enable */) {
123   return BT_STATUS_SUCCESS;
124 };
125 
removeDeviceFromProfiles(const RawAddress &)126 void MockCoreInterface::removeDeviceFromProfiles(
127     const RawAddress& /* bd_addr */){};
128 
onLinkDown(const RawAddress &)129 void MockCoreInterface::onLinkDown(const RawAddress& /* bd_addr */){};
130