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 
18 #include "btm_api_mock.h"
19 
20 #include <bluetooth/log.h>
21 
22 #include <optional>
23 
24 #include "bt_octets.h"
25 #include "stack/include/btm_ble_sec_api.h"
26 #include "types/raw_address.h"
27 
28 using namespace bluetooth;
29 
30 static bluetooth::manager::MockBtmInterface* btm_interface = nullptr;
31 
SetMockBtmInterface(MockBtmInterface * mock_btm_interface)32 void bluetooth::manager::SetMockBtmInterface(
33     MockBtmInterface* mock_btm_interface) {
34   btm_interface = mock_btm_interface;
35 }
36 
BTM_IsLinkKeyKnown(const RawAddress & bd_addr,tBT_TRANSPORT transport)37 bool BTM_IsLinkKeyKnown(const RawAddress& bd_addr, tBT_TRANSPORT transport) {
38   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
39   return btm_interface->IsLinkKeyKnown(bd_addr, transport);
40 }
41 
BTM_IsEncrypted(const RawAddress & bd_addr,tBT_TRANSPORT transport)42 bool BTM_IsEncrypted(const RawAddress& bd_addr, tBT_TRANSPORT transport) {
43   return btm_interface->BTM_IsEncrypted(bd_addr, transport);
44 }
45 
BTM_SetEncryption(const RawAddress & bd_addr,tBT_TRANSPORT transport,tBTM_SEC_CALLBACK * p_callback,void * p_ref_data,tBTM_BLE_SEC_ACT sec_act)46 tBTM_STATUS BTM_SetEncryption(const RawAddress& bd_addr,
47                               tBT_TRANSPORT transport,
48                               tBTM_SEC_CALLBACK* p_callback, void* p_ref_data,
49                               tBTM_BLE_SEC_ACT sec_act) {
50   return btm_interface->SetEncryption(bd_addr, transport, p_callback,
51                                       p_ref_data, sec_act);
52 }
53 
BTM_IsPhy2mSupported(const RawAddress & remote_bda,tBT_TRANSPORT transport)54 bool BTM_IsPhy2mSupported(const RawAddress& remote_bda,
55                           tBT_TRANSPORT transport) {
56   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
57   return btm_interface->IsPhy2mSupported(remote_bda, transport);
58 }
59 
BTM_GetPeerSCA(const RawAddress & remote_bda,tBT_TRANSPORT transport)60 uint8_t BTM_GetPeerSCA(const RawAddress& remote_bda, tBT_TRANSPORT transport) {
61   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
62   return btm_interface->GetPeerSCA(remote_bda, transport);
63 }
64 
BTM_BleSetPhy(const RawAddress & bd_addr,uint8_t tx_phys,uint8_t rx_phys,uint16_t phy_options)65 void BTM_BleSetPhy(const RawAddress& bd_addr, uint8_t tx_phys, uint8_t rx_phys,
66                    uint16_t phy_options) {
67   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
68   btm_interface->BleSetPhy(bd_addr, tx_phys, rx_phys, phy_options);
69 }
70 
BTM_SecIsSecurityPending(const RawAddress & bd_addr)71 bool BTM_SecIsSecurityPending(const RawAddress& bd_addr) {
72   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
73   return btm_interface->SecIsSecurityPending(bd_addr);
74 }
75 
btm_find_dev(const RawAddress & bd_addr)76 tBTM_SEC_DEV_REC* btm_find_dev(const RawAddress& bd_addr) {
77   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
78   return btm_interface->FindDevice(bd_addr);
79 }
80 
BTM_RequestPeerSCA(RawAddress const & bd_addr,tBT_TRANSPORT transport)81 void BTM_RequestPeerSCA(RawAddress const& bd_addr, tBT_TRANSPORT transport) {
82   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
83   btm_interface->RequestPeerSCA(bd_addr, transport);
84 }
85 
BTM_GetHCIConnHandle(RawAddress const & bd_addr,tBT_TRANSPORT transport)86 uint16_t BTM_GetHCIConnHandle(RawAddress const& bd_addr,
87                               tBT_TRANSPORT transport) {
88   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
89   return btm_interface->GetHCIConnHandle(bd_addr, transport);
90 }
91 
acl_disconnect_from_handle(uint16_t handle,tHCI_STATUS reason,std::string comment)92 void acl_disconnect_from_handle(uint16_t handle, tHCI_STATUS reason,
93                                 std::string comment) {
94   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
95   return btm_interface->AclDisconnectFromHandle(handle, reason);
96 }
97 
BTM_InqDbFirst(void)98 tBTM_INQ_INFO* BTM_InqDbFirst(void) {
99   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
100   return btm_interface->BTM_InqDbFirst();
101 }
BTM_InqDbNext(tBTM_INQ_INFO * p_cur)102 tBTM_INQ_INFO* BTM_InqDbNext(tBTM_INQ_INFO* p_cur) {
103   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
104   return btm_interface->BTM_InqDbNext(p_cur);
105 }
106 
BTM_BleGetPeerLTK(const RawAddress address)107 std::optional<Octet16> BTM_BleGetPeerLTK(const RawAddress address) {
108   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
109   return btm_interface->BTM_BleGetPeerLTK(address);
110 }
111 
BTM_BleGetPeerIRK(const RawAddress address)112 std::optional<Octet16> BTM_BleGetPeerIRK(const RawAddress address) {
113   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
114   return btm_interface->BTM_BleGetPeerIRK(address);
115 }
116 
BTM_BleIsLinkKeyKnown(const RawAddress address)117 bool BTM_BleIsLinkKeyKnown(const RawAddress address) {
118   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
119   return btm_interface->BTM_BleIsLinkKeyKnown(address);
120 }
121 
BTM_IsAclConnectionUp(const RawAddress & remote_bda,tBT_TRANSPORT transport)122 bool BTM_IsAclConnectionUp(const RawAddress& remote_bda,
123                            tBT_TRANSPORT transport) {
124   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
125   return btm_interface->BTM_IsAclConnectionUp(remote_bda, transport);
126 }
127 
BTM_BleGetIdentityAddress(const RawAddress address)128 std::optional<tBLE_BD_ADDR> BTM_BleGetIdentityAddress(
129     const RawAddress address) {
130   log::assert_that(btm_interface != nullptr, "Mock btm interface not set!");
131   return btm_interface->BTM_BleGetIdentityAddress(address);
132 }
133