1 /******************************************************************************
2  *
3  *  Copyright 2018 The Android Open Source Project
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 #pragma once
19 
20 #include <gmock/gmock.h>
21 
22 #include <vector>
23 
24 #include "l2c_api.h"
25 #include "stack/include/bt_hdr.h"
26 #include "types/raw_address.h"
27 
28 namespace bluetooth {
29 namespace l2cap {
30 
31 class L2capInterface {
32  public:
33   virtual uint16_t Register(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
34                             bool enable_snoop,
35                             tL2CAP_ERTM_INFO* p_ertm_info) = 0;
36   virtual uint16_t ConnectRequest(uint16_t psm, const RawAddress& bd_addr) = 0;
37   virtual bool ConnectResponse(const RawAddress& bd_addr, uint8_t id,
38                                uint16_t lcid, uint16_t result,
39                                uint16_t status) = 0;
40   virtual bool DisconnectRequest(uint16_t cid) = 0;
41   virtual bool DisconnectResponse(uint16_t cid) = 0;
42   virtual bool ConfigRequest(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) = 0;
43   virtual bool ConfigResponse(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) = 0;
44   virtual uint8_t DataWrite(uint16_t cid, BT_HDR* p_data) = 0;
45   virtual uint16_t RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO &cb_info, uint16_t sec_level) = 0;
46   virtual void DeregisterLECoc(uint16_t psm) = 0;
47   virtual bool ConnectCreditBasedRsp(const RawAddress& bd_addr, uint8_t id,
48                                        std::vector<uint16_t> &lcids,
49                                        uint16_t result,
50                                        tL2CAP_LE_CFG_INFO* p_cfg) = 0;
51   virtual std::vector<uint16_t>  ConnectCreditBasedReq(uint16_t psm,
52                                 const RawAddress& bd_addr,
53                                 tL2CAP_LE_CFG_INFO* p_cfg) = 0;
54   virtual bool ReconfigCreditBasedConnsReq(const RawAddress& bd_addr, std::vector<uint16_t> &lcids,
55                                 tL2CAP_LE_CFG_INFO* peer_cfg) = 0;
56   virtual uint16_t LeCreditDefault() = 0;
57   virtual uint16_t LeCreditThreshold() = 0;
58   virtual ~L2capInterface() = default;
59 };
60 
61 class MockL2capInterface : public L2capInterface {
62  public:
63   MOCK_METHOD4(Register,
64                uint16_t(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
65                         bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info));
66   MOCK_METHOD2(ConnectRequest,
67                uint16_t(uint16_t psm, const RawAddress& bd_addr));
68   MOCK_METHOD5(ConnectResponse,
69                bool(const RawAddress& bd_addr, uint8_t id, uint16_t lcid,
70                     uint16_t result, uint16_t status));
71   MOCK_METHOD1(DisconnectRequest, bool(uint16_t cid));
72   MOCK_METHOD1(DisconnectResponse, bool(uint16_t cid));
73   MOCK_METHOD2(ConfigRequest, bool(uint16_t cid, tL2CAP_CFG_INFO* p_cfg));
74   MOCK_METHOD2(ConfigResponse, bool(uint16_t cid, tL2CAP_CFG_INFO* p_cfg));
75   MOCK_METHOD2(DataWrite, uint8_t(uint16_t cid, BT_HDR* p_data));
76   MOCK_METHOD3(RegisterLECoc,
77                uint16_t(uint16_t psm, const tL2CAP_APPL_INFO &cb_info, uint16_t sec_level));
78   MOCK_METHOD1(DeregisterLECoc, void(uint16_t psm));
79   MOCK_METHOD1(GetBleConnRole, uint8_t(const RawAddress& bd_addr));
80   MOCK_METHOD5(ConnectCreditBasedRsp,
81                bool(const RawAddress& p_bd_addr, uint8_t id,
82                     std::vector<uint16_t> &lcids,
83                     uint16_t result,
84                     tL2CAP_LE_CFG_INFO* p_cfg));
85   MOCK_METHOD3(ConnectCreditBasedReq,
86                std::vector<uint16_t> (uint16_t psm,
87                     const RawAddress& bd_addr,
88                     tL2CAP_LE_CFG_INFO* p_cfg));
89   MOCK_METHOD3(ReconfigCreditBasedConnsReq,
90                bool(const RawAddress& p_bd_addr, std::vector<uint16_t> &lcids, tL2CAP_LE_CFG_INFO* peer_cfg));
91   MOCK_METHOD(uint16_t, LeCreditDefault, ());
92   MOCK_METHOD(uint16_t, LeCreditThreshold, ());
93 };
94 
95 /**
96  * Set the {@link MockL2capInterface} for testing
97  *
98  * @param mock_l2cap_interface pointer to mock l2cap interface, could be null
99  */
100 void SetMockInterface(MockL2capInterface* mock_l2cap_interface);
101 
102 }  // namespace l2cap
103 }  // namespace bluetooth
104