1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #pragma once
18 
19 #include <gmock/gmock.h>
20 
21 #include <optional>
22 
23 #include "bt_octets.h"
24 #include "btm_api.h"
25 #include "stack/btm/security_device_record.h"
26 #include "types/ble_address_with_type.h"
27 #include "types/raw_address.h"
28 
29 namespace bluetooth {
30 namespace manager {
31 
32 class BtmInterface {
33  public:
34   virtual bool GetSecurityFlagsByTransport(const RawAddress& bd_addr,
35                                            uint8_t* p_sec_flags,
36                                            tBT_TRANSPORT transport) = 0;
37   virtual bool IsLinkKeyKnown(const RawAddress& bd_addr,
38                               tBT_TRANSPORT transport) = 0;
39   virtual bool BTM_IsEncrypted(const RawAddress& bd_addr,
40                                tBT_TRANSPORT transport) = 0;
41   virtual tBTM_STATUS SetEncryption(const RawAddress& bd_addr,
42                                     tBT_TRANSPORT transport,
43                                     tBTM_SEC_CALLBACK* p_callback,
44                                     void* p_ref_data,
45                                     tBTM_BLE_SEC_ACT sec_act) = 0;
46   virtual tBTM_SEC_DEV_REC* FindDevice(const RawAddress& bd_addr) = 0;
47   virtual bool IsPhy2mSupported(const RawAddress& remote_bda,
48                                 tBT_TRANSPORT transport) = 0;
49   virtual uint8_t GetPeerSCA(const RawAddress& remote_bda,
50                              tBT_TRANSPORT transport) = 0;
51   virtual void BleSetPhy(const RawAddress& bd_addr, uint8_t tx_phys,
52                          uint8_t rx_phys, uint16_t phy_options) = 0;
53   virtual bool SecIsSecurityPending(const RawAddress& bd_addr) = 0;
54   virtual void RequestPeerSCA(RawAddress const& bd_addr,
55                               tBT_TRANSPORT transport) = 0;
56   virtual uint16_t GetHCIConnHandle(RawAddress const& bd_addr,
57                                     tBT_TRANSPORT transport) = 0;
58   virtual void AclDisconnectFromHandle(uint16_t handle, tHCI_STATUS reason) = 0;
59   virtual tBTM_INQ_INFO* BTM_InqDbFirst() = 0;
60   virtual tBTM_INQ_INFO* BTM_InqDbNext(tBTM_INQ_INFO* p_cur) = 0;
61   virtual std::optional<Octet16> BTM_BleGetPeerLTK(
62       const RawAddress address) = 0;
63   virtual std::optional<Octet16> BTM_BleGetPeerIRK(
64       const RawAddress address) = 0;
65 
66   virtual bool BTM_BleIsLinkKeyKnown(const RawAddress address) = 0;
67   virtual std::optional<tBLE_BD_ADDR> BTM_BleGetIdentityAddress(
68       const RawAddress address) = 0;
69   virtual bool BTM_IsAclConnectionUp(const RawAddress& remote_bda,
70                                      tBT_TRANSPORT transport) = 0;
71 
72   virtual ~BtmInterface() = default;
73 };
74 
75 class MockBtmInterface : public BtmInterface {
76  public:
77   MOCK_METHOD((bool), GetSecurityFlagsByTransport,
78               (const RawAddress& bd_addr, uint8_t* p_sec_flags,
79                tBT_TRANSPORT transport),
80               (override));
81   MOCK_METHOD((bool), IsLinkKeyKnown,
82               (const RawAddress& bd_addr, tBT_TRANSPORT transport), (override));
83   MOCK_METHOD((bool), BTM_IsEncrypted,
84               (const RawAddress& bd_addr, tBT_TRANSPORT transport), (override));
85   MOCK_METHOD((tBTM_STATUS), SetEncryption,
86               (const RawAddress& bd_addr, tBT_TRANSPORT transport,
87                tBTM_SEC_CALLBACK* p_callback, void* p_ref_data,
88                tBTM_BLE_SEC_ACT sec_act),
89               (override));
90   MOCK_METHOD((tBTM_SEC_DEV_REC*), FindDevice, (const RawAddress& bd_addr),
91               (override));
92   MOCK_METHOD((bool), IsPhy2mSupported,
93               (const RawAddress& remote_bda, tBT_TRANSPORT transport),
94               (override));
95   MOCK_METHOD((uint8_t), GetPeerSCA,
96               (const RawAddress& remote_bda, tBT_TRANSPORT transport),
97               (override));
98   MOCK_METHOD((void), BleSetPhy,
99               (const RawAddress& bd_addr, uint8_t tx_phys, uint8_t rx_phys,
100                uint16_t phy_options),
101               (override));
102   MOCK_METHOD((bool), SecIsSecurityPending, (const RawAddress& bd_addr),
103               (override));
104   MOCK_METHOD((void), RequestPeerSCA,
105               (RawAddress const& bd_addr, tBT_TRANSPORT transport), (override));
106   MOCK_METHOD((uint16_t), GetHCIConnHandle,
107               (RawAddress const& bd_addr, tBT_TRANSPORT transport), (override));
108   MOCK_METHOD((void), AclDisconnectFromHandle,
109               (uint16_t handle, tHCI_STATUS reason), (override));
110   MOCK_METHOD((tBTM_INQ_INFO*), BTM_InqDbFirst, (), (override));
111   MOCK_METHOD((tBTM_INQ_INFO*), BTM_InqDbNext, (tBTM_INQ_INFO * p_cur),
112               (override));
113   MOCK_METHOD((std::optional<Octet16>), BTM_BleGetPeerLTK,
114               (const RawAddress address), (override));
115   MOCK_METHOD((std::optional<Octet16>), BTM_BleGetPeerIRK,
116               (const RawAddress address), (override));
117 
118   MOCK_METHOD((bool), BTM_BleIsLinkKeyKnown, (const RawAddress address),
119               (override));
120   MOCK_METHOD((std::optional<tBLE_BD_ADDR>), BTM_BleGetIdentityAddress,
121               (const RawAddress address), (override));
122   MOCK_METHOD((bool), BTM_IsAclConnectionUp,
123               (const RawAddress& remote_bda, tBT_TRANSPORT transport),
124               (override));
125 };
126 
127 /**
128  * Set the {@link MockBtmInterface} for testing
129  *
130  * @param mock_btm_interface pointer to mock btm interface, could be null
131  */
132 void SetMockBtmInterface(MockBtmInterface* mock_btm_interface);
133 
134 }  // namespace manager
135 }  // namespace bluetooth
136