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 */, int /* num_properties */,
34                                            bt_property_t* /* properties */) {},
35         .invoke_remote_device_properties_cb = [](bt_status_t /* status */, RawAddress /* bd_addr */,
36                                                  int /* num_properties */,
37                                                  bt_property_t* /* properties */) {},
38         .invoke_device_found_cb = [](int /* num_properties */, bt_property_t* /* properties */) {},
39         .invoke_discovery_state_changed_cb = [](bt_discovery_state_t /* state */) {},
40         .invoke_pin_request_cb = [](RawAddress /* bd_addr */, bt_bdname_t /* bd_name */,
41                                     uint32_t /* cod */, bool /* min_16_digit */) {},
42         .invoke_ssp_request_cb = [](RawAddress /* bd_addr */,
43                                     bt_ssp_variant_t /* pairing_variant */,
44                                     uint32_t /* pass_key */) {},
45         .invoke_oob_data_request_cb = [](tBT_TRANSPORT /* t */, bool /* valid */, Octet16 /* c */,
46                                          Octet16 /* r */, RawAddress /* raw_address */,
47                                          uint8_t /* address_type */) {},
48         .invoke_bond_state_changed_cb = [](bt_status_t /* status */, RawAddress /* bd_addr */,
49                                            bt_bond_state_t /* state */, int /* fail_reason */) {},
50         .invoke_address_consolidate_cb = [](RawAddress /* main_bd_addr */,
51                                             RawAddress /* secondary_bd_addr */) {},
52         .invoke_le_address_associate_cb = [](RawAddress /* main_bd_addr */,
53                                              RawAddress /* secondary_bd_addr */,
54                                              uint8_t /* identity_address_type */) {},
55         .invoke_acl_state_changed_cb =
56                 [](bt_status_t /* status */, RawAddress /* bd_addr */, bt_acl_state_t /* state */,
57                    int /* transport_link_type */, bt_hci_error_code_t /* hci_reason */,
58                    bt_conn_direction_t /* direction */, uint16_t /* acl_handle */) {},
59         .invoke_thread_evt_cb = [](bt_cb_thread_evt /* event */) {},
60         .invoke_le_test_mode_cb = [](bt_status_t /* status */, uint16_t /* count */) {},
61         .invoke_energy_info_cb = [](bt_activity_energy_info /* energy_info */,
62                                     bt_uid_traffic_t* /* uid_data */) {},
63         .invoke_link_quality_report_cb =
64                 [](uint64_t /* timestamp */, int /* report_id */, int /* rssi */, int /* snr */,
65                    int /* retransmission_count */, int /* packets_not_receive_count */,
66                    int /* negative_acknowledgement_count */) {},
67         .invoke_key_missing_cb = [](RawAddress /* bd_addr */) {},
68         .invoke_encryption_change_cb = [](bt_encryption_change_evt /* encryption_change */) {},
69 };
70 
71 // This interface lets us query for configuration properties of the stack that
72 // could change at runtime
73 struct MockConfigInterface : public ConfigInterface {
74   MOCK_METHOD((bool), isA2DPOffloadEnabled, (), ());
75   MOCK_METHOD((bool), isAndroidTVDevice, (), ());
76   MOCK_METHOD((bool), isRestrictedMode, (), ());
77 };
78 MockConfigInterface mock_config_interface;
79 
80 // This interface lets us communicate with encoders used in profiles
81 struct MockCodecInterface : public CodecInterface {
82   MOCK_METHOD((void), initialize, (), ());
83   MOCK_METHOD((void), cleanup, (), ());
84   MOCK_METHOD((uint32_t), encodePacket, (int16_t* input, uint8_t* output), ());
85   MOCK_METHOD((bool), decodePacket, (const uint8_t* i_buf, int16_t* o_buf, size_t out_len), ());
86 };
87 MockCodecInterface mock_codec_msbcCodec;
88 MockCodecInterface mock_codec_lc3Codec;
89 
90 HACK_ProfileInterface mock_HACK_profile_interface = {
91         .btif_hh_virtual_unplug = [](const tAclLinkSpec& /* link_spec */) -> bt_status_t {
92           return BT_STATUS_SUCCESS;
93         },
94         .bta_hh_read_ssr_param =
95                 [](const tAclLinkSpec& /* link_spec */, uint16_t* /* p_max_ssr_lat */,
96                    uint16_t* /* p_min_ssr_tout */) -> tBTA_HH_STATUS { return BTA_HH_OK; },
97 
98         .btif_av_set_dynamic_audio_buffer_size = [](uint8_t /* dynamic_audio_buffer_size */) {},
99         .GetHearingAidDeviceCount = []() -> int { return 0; },
100         .IsLeAudioClientRunning = []() -> bool { return false; },
101         .AVRC_GetProfileVersion = []() -> uint16_t { return 0; },
102 };
103 
104 // This class defines the overall interface expected by bluetooth::core.
105 struct MockCoreInterface : public CoreInterface {
MockCoreInterfaceMockCoreInterface106   MockCoreInterface()
107       : CoreInterface(&mock_event_callbacks, &mock_config_interface, &mock_codec_msbcCodec,
108                       &mock_codec_lc3Codec, &mock_HACK_profile_interface) {}
109 
110   MOCK_METHOD((void), onBluetoothEnabled, (), ());
111   MOCK_METHOD((bt_status_t), toggleProfile, (tBTA_SERVICE_ID service_id, bool enable), ());
112   MOCK_METHOD((void), removeDeviceFromProfiles, (const RawAddress& bd_addr), ());
113   MOCK_METHOD((void), onLinkDown, (const RawAddress& bd_addr, tBT_TRANSPORT transport), ());
114 };
115 
116 }  // namespace testing
117 }  // namespace core
118 }  // namespace bluetooth
119