1 /*
2  * Copyright 2023 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 #pragma once
18 
19 #include <gmock/gmock.h>
20 
21 #include "btif/include/core_callbacks.h"
22 #include "include/hardware/bluetooth.h"
23 #include "types/raw_address.h"
24 
25 namespace bluetooth {
26 namespace core {
27 namespace testing {
28 
29 // These callbacks are not profile specific (e.g. connection complete, bond
30 // complete, etc) and are what go to the Java layer.
31 EventCallbacks mock_event_callbacks = {
32     .invoke_adapter_state_changed_cb = [](bt_state_t /* state */) {},
33     .invoke_adapter_properties_cb = [](bt_status_t /* status */,
34                                        int /* num_properties */,
35                                        bt_property_t* /* properties */) {},
36     .invoke_remote_device_properties_cb =
37         [](bt_status_t /* status */, RawAddress /* bd_addr */,
38            int /* num_properties */, bt_property_t* /* properties */) {},
39     .invoke_device_found_cb = [](int /* num_properties */,
40                                  bt_property_t* /* properties */) {},
41     .invoke_discovery_state_changed_cb =
42         [](bt_discovery_state_t /* state */) {},
43     .invoke_pin_request_cb = [](RawAddress /* bd_addr */,
44                                 bt_bdname_t /* bd_name */, uint32_t /* cod */,
45                                 bool /* min_16_digit */) {},
46     .invoke_ssp_request_cb = [](RawAddress /* bd_addr */,
47                                 bt_ssp_variant_t /* pairing_variant */,
48                                 uint32_t /* pass_key */) {},
49     .invoke_oob_data_request_cb = [](tBT_TRANSPORT /* t */, bool /* valid */,
50                                      Octet16 /* c */, Octet16 /* r */,
51                                      RawAddress /* raw_address */,
52                                      uint8_t /* address_type */) {},
53     .invoke_bond_state_changed_cb =
54         [](bt_status_t /* status */, RawAddress /* bd_addr */,
55            bt_bond_state_t /* state */, int /* fail_reason */) {},
56     .invoke_address_consolidate_cb = [](RawAddress /* main_bd_addr */,
57                                         RawAddress /* secondary_bd_addr */) {},
58     .invoke_le_address_associate_cb = [](RawAddress /* main_bd_addr */,
59                                          RawAddress /* secondary_bd_addr */) {},
60     .invoke_acl_state_changed_cb =
61         [](bt_status_t /* status */, RawAddress /* bd_addr */,
62            bt_acl_state_t /* state */, int /* transport_link_type */,
63            bt_hci_error_code_t /* hci_reason */,
64            bt_conn_direction_t /* direction */, uint16_t /* acl_handle */) {},
65     .invoke_thread_evt_cb = [](bt_cb_thread_evt /* event */) {},
66     .invoke_le_test_mode_cb = [](bt_status_t /* status */,
67                                  uint16_t /* count */) {},
68     .invoke_energy_info_cb = [](bt_activity_energy_info /* energy_info */,
69                                 bt_uid_traffic_t* /* uid_data */) {},
70     .invoke_link_quality_report_cb =
71         [](uint64_t /* timestamp */, int /* report_id */, int /* rssi */,
72            int /* snr */, int /* retransmission_count */,
73            int /* packets_not_receive_count */,
74            int /* negative_acknowledgement_count */) {},
75     .invoke_key_missing_cb = [](RawAddress /* bd_addr */) {},
76 };
77 
78 // This interface lets us query for configuration properties of the stack that
79 // could change at runtime
80 struct MockConfigInterface : public ConfigInterface {
81   MOCK_METHOD((bool), isA2DPOffloadEnabled, (), ());
82   MOCK_METHOD((bool), isAndroidTVDevice, (), ());
83   MOCK_METHOD((bool), isRestrictedMode, (), ());
84 };
85 MockConfigInterface mock_config_interface;
86 
87 // This interface lets us communicate with encoders used in profiles
88 struct MockCodecInterface : public CodecInterface {
89   MOCK_METHOD((void), initialize, (), ());
90   MOCK_METHOD((void), cleanup, (), ());
91   MOCK_METHOD((uint32_t), encodePacket, (int16_t * input, uint8_t* output), ());
92   MOCK_METHOD((bool), decodePacket,
93               (const uint8_t* i_buf, int16_t* o_buf, size_t out_len), ());
94 };
95 MockCodecInterface mock_codec_msbcCodec;
96 MockCodecInterface mock_codec_lc3Codec;
97 
98 HACK_ProfileInterface mock_HACK_profile_interface = {
99     .btif_hh_connect = [](const tAclLinkSpec& /* link_spec */) -> bt_status_t {
100       return BT_STATUS_SUCCESS;
101     },
102     .btif_hh_virtual_unplug = [](const tAclLinkSpec& /* link_spec */)
103         -> bt_status_t { return BT_STATUS_SUCCESS; },
104     .bta_hh_read_ssr_param =
105         [](const tAclLinkSpec& /* link_spec */, uint16_t* /* p_max_ssr_lat */,
106            uint16_t* /* p_min_ssr_tout */) -> tBTA_HH_STATUS {
107       return BTA_HH_OK;
108     },
109 
110     .btif_av_set_dynamic_audio_buffer_size =
111         [](uint8_t /* dynamic_audio_buffer_size */) {},
112     .GetHearingAidDeviceCount = []() -> int { return 0; },
113     .IsLeAudioClientRunning = []() -> bool { return false; },
114     .AVRC_GetProfileVersion = []() -> uint16_t { return 0; },
115 };
116 
117 // This class defines the overall interface expected by bluetooth::core.
118 struct MockCoreInterface : public CoreInterface {
MockCoreInterfaceMockCoreInterface119   MockCoreInterface()
120       : CoreInterface(&mock_event_callbacks, &mock_config_interface,
121                       &mock_codec_msbcCodec, &mock_codec_lc3Codec,
122                       &mock_HACK_profile_interface) {}
123 
124   MOCK_METHOD((void), onBluetoothEnabled, (), ());
125   MOCK_METHOD((bt_status_t), toggleProfile,
126               (tBTA_SERVICE_ID service_id, bool enable), ());
127   MOCK_METHOD((void), removeDeviceFromProfiles, (const RawAddress& bd_addr),
128               ());
129   MOCK_METHOD((void), onLinkDown, (const RawAddress& bd_addr), ());
130 };
131 
132 }  // namespace testing
133 }  // namespace core
134 }  // namespace bluetooth
135