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 "stack/include/bt_hdr.h"
25 #include "stack/include/l2cap_interface.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, bool enable_snoop,
34                             tL2CAP_ERTM_INFO* p_ertm_info) = 0;
35   virtual uint16_t ConnectRequest(uint16_t psm, const RawAddress& bd_addr) = 0;
36   virtual bool ConnectResponse(const RawAddress& bd_addr, uint8_t id, uint16_t lcid,
37                                tL2CAP_CONN result, uint16_t status) = 0;
38   virtual bool DisconnectRequest(uint16_t cid) = 0;
39   virtual bool DisconnectResponse(uint16_t cid) = 0;
40   virtual bool ConfigRequest(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) = 0;
41   virtual bool ConfigResponse(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) = 0;
42   virtual tL2CAP_DW_RESULT DataWrite(uint16_t cid, BT_HDR* p_data) = 0;
43   virtual uint16_t RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO& cb_info,
44                                  uint16_t sec_level) = 0;
45   virtual void DeregisterLECoc(uint16_t psm) = 0;
46   virtual bool ConnectCreditBasedRsp(const RawAddress& bd_addr, uint8_t id,
47                                      std::vector<uint16_t>& lcids, tL2CAP_LE_RESULT_CODE result,
48                                      tL2CAP_LE_CFG_INFO* p_cfg) = 0;
49   virtual std::vector<uint16_t> ConnectCreditBasedReq(uint16_t psm, const RawAddress& bd_addr,
50                                                       tL2CAP_LE_CFG_INFO* p_cfg) = 0;
51   virtual bool ReconfigCreditBasedConnsReq(const RawAddress& bd_addr, std::vector<uint16_t>& lcids,
52                                            tL2CAP_LE_CFG_INFO* peer_cfg) = 0;
53   virtual uint16_t LeCreditDefault() = 0;
54   virtual uint16_t LeCreditThreshold() = 0;
55   virtual ~L2capInterface() = default;
56 };
57 
58 class MockL2capInterface : public L2capInterface {
59 public:
60   MOCK_METHOD4(Register, uint16_t(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
61                                   bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info));
62   MOCK_METHOD2(ConnectRequest, uint16_t(uint16_t psm, const RawAddress& bd_addr));
63   MOCK_METHOD5(ConnectResponse, bool(const RawAddress& bd_addr, uint8_t id, uint16_t lcid,
64                                      tL2CAP_CONN result, uint16_t status));
65   MOCK_METHOD1(DisconnectRequest, bool(uint16_t cid));
66   MOCK_METHOD1(DisconnectResponse, bool(uint16_t cid));
67   MOCK_METHOD2(ConfigRequest, bool(uint16_t cid, tL2CAP_CFG_INFO* p_cfg));
68   MOCK_METHOD2(ConfigResponse, bool(uint16_t cid, tL2CAP_CFG_INFO* p_cfg));
69   MOCK_METHOD2(DataWrite, tL2CAP_DW_RESULT(uint16_t cid, BT_HDR* p_data));
70   MOCK_METHOD3(RegisterLECoc,
71                uint16_t(uint16_t psm, const tL2CAP_APPL_INFO& cb_info, uint16_t sec_level));
72   MOCK_METHOD1(DeregisterLECoc, void(uint16_t psm));
73   MOCK_METHOD1(GetBleConnRole, uint8_t(const RawAddress& bd_addr));
74   MOCK_METHOD5(ConnectCreditBasedRsp,
75                bool(const RawAddress& p_bd_addr, uint8_t id, std::vector<uint16_t>& lcids,
76                     tL2CAP_LE_RESULT_CODE result, tL2CAP_LE_CFG_INFO* p_cfg));
77   MOCK_METHOD3(ConnectCreditBasedReq, std::vector<uint16_t>(uint16_t psm, const RawAddress& bd_addr,
78                                                             tL2CAP_LE_CFG_INFO* p_cfg));
79   MOCK_METHOD3(ReconfigCreditBasedConnsReq,
80                bool(const RawAddress& p_bd_addr, std::vector<uint16_t>& lcids,
81                     tL2CAP_LE_CFG_INFO* peer_cfg));
82   MOCK_METHOD(uint16_t, LeCreditDefault, ());
83   MOCK_METHOD(uint16_t, LeCreditThreshold, ());
84 };
85 
86 /**
87  * Set the {@link MockL2capInterface} for testing
88  *
89  * @param mock_l2cap_interface pointer to mock l2cap interface, could be null
90  */
91 void SetMockInterface(MockL2capInterface* mock_l2cap_interface);
92 
93 }  // namespace l2cap
94 }  // namespace bluetooth
95