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 namespace bluetooth {
25 namespace rfcomm {
26 
27 class RfcommCallback {
28 public:
29   virtual void PortManagementCallback(uint32_t code, uint16_t port_handle,
30                                       uint16_t callback_index) = 0;
31   virtual void PortEventCallback(uint32_t code, uint16_t port_handle, uint16_t callback_index) = 0;
32   virtual ~RfcommCallback() = default;
33 };
34 
35 class MockRfcommCallback : public RfcommCallback {
36 public:
37   MOCK_METHOD3(PortManagementCallback,
38                void(uint32_t code, uint16_t port_handle, uint16_t callback_index));
39   MOCK_METHOD3(PortEventCallback,
40                void(uint32_t code, uint16_t port_handle, uint16_t callback_index));
41 };
42 
43 /**
44  * Create DLCI using direction of service channel number
45  *
46  * @param on_originator_side is this a channel on initiator side
47  * @param scn service channel number
48  * @return DLCI
49  */
50 uint8_t GetDlci(bool on_originator_side, uint8_t scn);
51 
52 /**
53  * Create address field in RFCOMM packet
54  *
55  * @param ea end of field byte, true if field only has 1 byte, false otherwise
56  * @param cr command and response bit, true if packet is from initiator,
57  *           false otherwise, not actual "command" and "response"
58  * @param dlci DLCI of this pcaket, RFCOMM_MX_DLCI for multiplexer control
59  * @return address field
60  */
61 uint8_t GetAddressField(bool ea, bool cr, uint8_t dlci);
62 
63 /**
64  * Create control field in RFCOMM packet
65  *
66  * @param pf Poll/Finish bit, normally 1 for multiplexer control channel
67  * @param frame_type frame type
68  * @return control field
69  */
70 uint8_t GetControlField(bool pf, uint8_t frame_type);
71 
72 /**
73  * Create Multiplexer control channel parameter negotiation frame
74  *
75  * @param dlci DLCI to be configured
76  * @param i_bits i bits
77  * @param cl_bits cl bits
78  * @param priority priority
79  * @param timer_value timer value
80  * @param rfcomm_mtu rfcomm mtu
81  * @param max_num_retransmission maximum number of retransmission
82  * @param err_recovery_window_k error recovery window k
83  * @return vector of bytes of this frame
84  */
85 std::vector<uint8_t> CreateMccPnFrame(uint8_t dlci, uint8_t i_bits, uint8_t cl_bits,
86                                       uint8_t priority, uint8_t timer_value, uint16_t rfcomm_mtu,
87                                       uint8_t max_num_retransmission,
88                                       uint8_t err_recovery_window_k);
89 /**
90  * Create Multiplexer Control Modem Status Configuration Frame
91  *
92  * @param dlci DLCI to be configured
93  * @param fc flow control
94  * @param rtc ready to communicate
95  * @param rtr ready to receive
96  * @param ic incoming call indicator
97  * @param dv is data valid
98  * @return vector of bytes
99  */
100 std::vector<uint8_t> CreateMccMscFrame(uint8_t dlci, bool fc, bool rtc, bool rtr, bool ic, bool dv);
101 
102 /**
103  * Create Multiplexer Control Frame
104  *
105  * @param command_type type of command
106  * @param cr command or response flag, true when this is a command, false when
107  *           this is a response, regardless of connection direction
108  * @param data frame data
109  * @return vector of bytes
110  */
111 std::vector<uint8_t> CreateMultiplexerControlFrame(uint8_t command_type, bool cr,
112                                                    const std::vector<uint8_t>& data);
113 
114 /**
115  * Create a general RFCOMM packet
116  *
117  * @param address address byte
118  * @param control control byte
119  * @param credits number of credits, <= 0 will cause this to be ignored
120  * @param data frame data
121  * @return vector of bytes
122  */
123 std::vector<uint8_t> CreateRfcommPacket(uint8_t address, uint8_t control, int credits,
124                                         const std::vector<uint8_t>& data);
125 /*
126  * Various shortcut for getting frequently used packets
127  */
128 
129 /**
130  * Create SABM packet that is used to connect to a service channel number in a
131  * multiplexer
132  *
133  * @param dlci DLCI to be connected
134  * @param l2cap_lcid L2CAP channel ID
135  * @param acl_handle ACL handle
136  * @return vector of bytes of unwrapped ACL packet
137  */
138 std::vector<uint8_t> CreateQuickSabmPacket(uint8_t dlci, uint16_t l2cap_lcid, uint16_t acl_handle);
139 
140 /**
141  * Create UA packet that is used to acknowledge service channel connection
142  *
143  * @param dlci DLCI to be connected
144  * @param l2cap_lcid L2CAP channel ID
145  * @param acl_handle ACL handle
146  * @return vector of bytes of unwrapped ACL packet
147  */
148 std::vector<uint8_t> CreateQuickUaPacket(uint8_t dlci, uint16_t l2cap_lcid, uint16_t acl_handle);
149 
150 /**
151  * Create parameter negotiation packet used to setup parameters for a DLCI
152  *
153  * @param rfc_cr RFCOMM command/response bit, true of initiator
154  * @param target_dlci DLCI to be configured
155  * @param mx_cr multiplexer command or response, regardless of initiator
156  * @param rfc_mtu RFCOMM mtu to be used for DLCI
157  * @param cl CL bit
158  * @param priority priority
159  * @param k error recovery window k
160  * @param l2cap_lcid L2CAP channel ID
161  * @param acl_handle ACL handle
162  * @return vector of bytes of unwrapped ACL packet
163  */
164 std::vector<uint8_t> CreateQuickPnPacket(bool rfc_cr, uint8_t target_dlci, bool mx_cr,
165                                          uint16_t rfc_mtu, uint8_t cl, uint8_t priority, uint8_t k,
166                                          uint16_t l2cap_lcid, uint16_t acl_handle);
167 
168 /**
169  * Create modem signal control packet
170  *
171  * @param rfc_cr RFCOMM command/response bit, true of initiator
172  * @param dlci DLCI to be configured
173  * @param l2cap_lcid L2CAP channel ID
174  * @param acl_handle ACL handle
175  * @param mx_cr multiplexer command or response, regardless of initiator
176  * @param fc flow control
177  * @param rtc ready to communicate
178  * @param rtr ready to receive
179  * @param ic incoming call indicator
180  * @param dv data valid
181  * @return vector of bytes of unwrapped ACL packet
182  */
183 std::vector<uint8_t> CreateQuickMscPacket(bool rfc_cr, uint8_t dlci, uint16_t l2cap_lcid,
184                                           uint16_t acl_handle, bool mx_cr, bool fc, bool rtc,
185                                           bool rtr, bool ic, bool dv);
186 
187 /**
188  * Create a quick RFCOMM data packet
189  *
190  * @param dlci DLCI of this packet
191  * @param cr command or control, true for initiator
192  * @param l2cap_lcid L2CAP channel ID
193  * @param acl_handle ACL handle
194  * @param credits number of credits
195  * @param data data bytes
196  * @return vector of bytes of unwrapped ACL packet
197  */
198 std::vector<uint8_t> CreateQuickDataPacket(uint8_t dlci, bool cr, uint16_t l2cap_lcid,
199                                            uint16_t acl_handle, int credits,
200                                            const std::vector<uint8_t>& data);
201 
202 /**
203  * Create a quick RFCOMM data packet
204  *
205  * @param dlci DLCI of this packet
206  * @param cr command or control, true for initiator
207  * @param l2cap_lcid L2CAP channel ID
208  * @param acl_handle ACL handle
209  * @param credits number of credits
210  * @param str message in string format
211  * @return vector of bytes of unwrapped ACL packet
212  */
213 std::vector<uint8_t> CreateQuickDataPacket(uint8_t dlci, bool cr, uint16_t l2cap_lcid,
214                                            uint16_t acl_handle, int credits,
215                                            const std::string& str);
216 
217 }  // namespace rfcomm
218 }  // namespace bluetooth
219