1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <vector>
20 
21 #include "stack/include/bt_hdr.h"
22 #include "stack/include/l2c_api.h"
23 #include "types/ble_address_with_type.h"
24 #include "types/hci_role.h"
25 #include "types/raw_address.h"
26 
27 namespace bluetooth {
28 namespace shim {
29 
30 /*******************************************************************************
31  *
32  * Function         L2CA_Register
33  *
34  * Description      Other layers call this function to register for L2CAP
35  *                  services.
36  *
37  * Returns          PSM to use or zero if error. Typically, the PSM returned
38  *                  is the same as was passed in, but for an outgoing-only
39  *                  connection to a dynamic PSM, a "virtual" PSM is returned
40  *                  and should be used in the calls to L2CA_ConnectReq() and
41  *                  BTM_SetSecurityLevel().
42  *
43  ******************************************************************************/
44 uint16_t L2CA_Register(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
45                        bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info,
46                        uint16_t my_mtu, uint16_t required_remote_mtu,
47                        uint16_t sec_level);
48 
49 /*******************************************************************************
50  *
51  * Function         L2CA_Deregister
52  *
53  * Description      Other layers call this function to deregister for L2CAP
54  *                  services.
55  *
56  * Returns          void
57  *
58  ******************************************************************************/
59 void L2CA_Deregister(uint16_t psm);
60 
61 /*******************************************************************************
62  *
63  * Function         L2CA_AllocateLePSM
64  *
65  * Description      Other layers call this function to find an unused LE PSM for
66  *                  L2CAP services.
67  *
68  * Returns          LE_PSM to use if success. Otherwise returns 0.
69  *
70  ******************************************************************************/
71 uint16_t L2CA_AllocateLePSM(void);
72 
73 /*******************************************************************************
74  *
75  * Function         L2CA_FreeLePSM
76  *
77  * Description      Free an assigned LE PSM.
78  *
79  * Returns          void
80  *
81  ******************************************************************************/
82 void L2CA_FreeLePSM(uint16_t psm);
83 
84 /*******************************************************************************
85  *
86  * Function         L2CA_ConnectReq
87  *
88  * Description      Higher layers call this function to create an L2CAP
89  *                  connection.
90  *                  Note that the connection is not established at this time,
91  *                  but connection establishment gets started. The callback
92  *                  will be invoked when connection establishes or fails.
93  *
94  * Returns          the CID of the connection, or 0 if it failed to start
95  *
96  ******************************************************************************/
97 uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& p_bd_addr);
98 
99 /*******************************************************************************
100  *
101  * Function         L2CA_RegisterLECoc
102  *
103  * Description      Other layers call this function to register for L2CAP
104  *                  Connection Oriented Channel.
105  *
106  * Returns          PSM to use or zero if error. Typically, the PSM returned
107  *                  is the same as was passed in, but for an outgoing-only
108  *                  connection to a dynamic PSM, a "virtual" PSM is returned
109  *                  and should be used in the calls to L2CA_ConnectLECocReq()
110  *                  and BTM_SetSecurityLevel().
111  *
112  ******************************************************************************/
113 uint16_t L2CA_RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
114                             uint16_t sec_level, tL2CAP_LE_CFG_INFO cfg);
115 
116 /*******************************************************************************
117  *
118  * Function         L2CA_DeregisterLECoc
119  *
120  * Description      Other layers call this function to deregister for L2CAP
121  *                  Connection Oriented Channel.
122  *
123  * Returns          void
124  *
125  ******************************************************************************/
126 void L2CA_DeregisterLECoc(uint16_t psm);
127 
128 /*******************************************************************************
129  *
130  * Function         L2CA_ConnectLECocReq
131  *
132  * Description      Higher layers call this function to create an L2CAP LE COC.
133  *                  Note that the connection is not established at this time,
134  *                  but connection establishment gets started. The callback
135  *                  will be invoked when connection establishes or fails.
136  *
137  * Returns          the CID of the connection, or 0 if it failed to start
138  *
139  ******************************************************************************/
140 uint16_t L2CA_ConnectLECocReq(uint16_t psm, const RawAddress& p_bd_addr,
141                               tL2CAP_LE_CFG_INFO* p_cfg);
142 
143 /*******************************************************************************
144  *
145  *  Function         L2CA_GetPeerLECocConfig
146  *
147  *  Description      Get peers configuration for LE Connection Oriented Channel.
148  *
149  *  Return value:    true if peer is connected
150  *
151  ******************************************************************************/
152 bool L2CA_GetPeerLECocConfig(uint16_t lcid, tL2CAP_LE_CFG_INFO* peer_cfg);
153 
154 /*******************************************************************************
155  *
156  *  Function         L2CA_ReconfigCreditBasedConnsReq
157  *
158  *  Description      Start reconfigure procedure on Credit Based Connection Oriented
159  *                   Channels.
160  *
161  *  Return value:    true if peer is connected
162  *
163  ******************************************************************************/
164 
165 bool L2CA_ReconfigCreditBasedConnsReq(const RawAddress& bd_addr,
166                                       std::vector<uint16_t>& lcids,
167                                       tL2CAP_LE_CFG_INFO* p_cfg);
168 
169 /*******************************************************************************
170  *
171  *  Function         L2CA_ConnectCreditBasedReq
172  *
173  *  Description      With this function L2CAP will initiate setup of up to 5 credit
174  *                   based connections for given psm using provided configuration.
175  *                   L2CAP will notify user on the connection result, by calling
176  *                   pL2CA_CreditBasedConnectCfm_Cb for each cid with a result.
177  *
178  *  Return value: vector of allocated local cids for the connection
179  *
180  ******************************************************************************/
181 
182 extern std::vector<uint16_t> L2CA_ConnectCreditBasedReq(
183     uint16_t psm, const RawAddress& p_bd_addr, tL2CAP_LE_CFG_INFO* p_cfg);
184 
185 /*******************************************************************************
186  *
187  *  Function         L2CA_ConnectCreditBasedRsp
188  *
189  *  Description      Response for the pL2CA_CreditBasedConnectInd_Cb which is the
190  *                   indication for peer requesting credit based connection.
191  *
192  *  Return value:    true if peer is connected
193  *
194  ******************************************************************************/
195 
196 bool L2CA_ConnectCreditBasedRsp(const RawAddress& p_bd_addr, uint8_t id,
197                                 std::vector<uint16_t>& accepted_lcids,
198                                 uint16_t result, tL2CAP_LE_CFG_INFO* p_cfg);
199 
200 /*******************************************************************************
201  *
202  * Function         L2CA_DisconnectReq
203  *
204  * Description      Higher layers call this function to disconnect a channel.
205  *
206  * Returns          true if disconnect sent, else false
207  *
208  ******************************************************************************/
209 bool L2CA_DisconnectReq(uint16_t cid);
210 
211 bool L2CA_DisconnectLECocReq(uint16_t cid);
212 
213 /*******************************************************************************
214  *
215  * Function         L2CA_DataWrite
216  *
217  * Description      Higher layers call this function to write data.
218  *
219  * Returns          L2CAP_DW_SUCCESS, if data accepted, else false
220  *                  L2CAP_DW_CONGESTED, if data accepted and the channel is
221  *                                      congested
222  *                  L2CAP_DW_FAILED, if error
223  *
224  ******************************************************************************/
225 uint8_t L2CA_DataWrite(uint16_t cid, BT_HDR* p_data);
226 
227 uint8_t L2CA_LECocDataWrite(uint16_t cid, BT_HDR* p_data);
228 
229 /*******************************************************************************
230  *
231  * Function         L2CA_SetIdleTimeoutByBdAddr
232  *
233  * Description      Higher layers call this function to set the idle timeout for
234  *                  a connection. The "idle timeout" is the amount of time that
235  *                  a connection can remain up with no L2CAP channels on it.
236  *                  A timeout of zero means that the connection will be torn
237  *                  down immediately when the last channel is removed.
238  *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
239  *                  A bd_addr is the remote BD address. If bd_addr =
240  *                  RawAddress::kAny, then the idle timeouts for all active
241  *                  l2cap links will be changed.
242  *
243  * Returns          true if command succeeded, false if failed
244  *
245  * NOTE             This timeout applies to all logical channels active on the
246  *                  ACL link.
247  ******************************************************************************/
248 bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout,
249                                  tBT_TRANSPORT transport);
250 
251 /*******************************************************************************
252  *
253  * Function     L2CA_FlushChannel
254  *
255  * Description  This function flushes none, some or all buffers queued up
256  *              for xmission for a particular CID. If called with
257  *              L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
258  *              of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
259  *              flushes all buffers.  All other values specifies the maximum
260  *              buffers to flush.
261  *
262  * Returns      Number of buffers left queued for that CID
263  *
264  ******************************************************************************/
265 uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush);
266 
267 /*******************************************************************************
268  *
269  * Function         L2CA_SetAclPriority
270  *
271  * Description      Sets the transmission priority for an ACL channel.
272  *                  (For initial implementation only two values are valid.
273  *                  L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
274  *
275  * Returns          true if a valid channel, else false
276  *
277  ******************************************************************************/
278 bool L2CA_SetAclPriority(const RawAddress& bd_addr, tL2CAP_PRIORITY priority);
279 
280 /*******************************************************************************
281  *
282  * Function         L2CA_SetTxPriority
283  *
284  * Description      Sets the transmission priority for a channel. (FCR Mode)
285  *
286  * Returns          true if a valid channel, else false
287  *
288  ******************************************************************************/
289 bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority);
290 
291 /*******************************************************************************
292  *
293  * Function         L2CA_SetChnlFlushability
294  *
295  * Description      Higher layers call this function to set a channels
296  *                  flushability flags
297  *
298  * Returns          true if CID found, else false
299  *
300  ******************************************************************************/
301 bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable);
302 
303 /*******************************************************************************
304  *
305  *  Function         L2CA_GetPeerFeatures
306  *
307  *  Description      Get a peers features and fixed channel map
308  *
309  *  Parameters:      BD address of the peer
310  *                   Pointers to features and channel mask storage area
311  *
312  *  Return value:    true if peer is connected
313  *
314  ******************************************************************************/
315 bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat,
316                           uint8_t* p_chnl_mask);
317 
318 /*******************************************************************************
319  *
320  *  Function        L2CA_RegisterFixedChannel
321  *
322  *  Description     Register a fixed channel.
323  *
324  *  Parameters:     Fixed Channel #
325  *                  Channel Callbacks and config
326  *
327  *  Return value:   true if registered OK
328  *
329  ******************************************************************************/
330 bool L2CA_RegisterFixedChannel(uint16_t fixed_cid,
331                                tL2CAP_FIXED_CHNL_REG* p_freg);
332 
333 /*******************************************************************************
334  *
335  *  Function        L2CA_ConnectFixedChnl
336  *
337  *  Description     Connect an fixed signalling channel to a remote device.
338  *
339  *  Parameters:     Fixed CID
340  *                  BD Address of remote
341  *
342  *  Return value:   true if connection started
343  *
344  ******************************************************************************/
345 bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr);
346 
347 /*******************************************************************************
348  *
349  *  Function        L2CA_SendFixedChnlData
350  *
351  *  Description     Write data on a fixed signalling channel.
352  *
353  *  Parameters:     Fixed CID
354  *                  BD Address of remote
355  *                  Pointer to buffer of type BT_HDR
356  *
357  * Return value     L2CAP_DW_SUCCESS, if data accepted
358  *                  L2CAP_DW_FAILED,  if error
359  *
360  ******************************************************************************/
361 uint16_t L2CA_SendFixedChnlData(uint16_t fixed_cid, const RawAddress& rem_bda,
362                                 BT_HDR* p_buf);
363 
364 /*******************************************************************************
365  *
366  *  Function        L2CA_RemoveFixedChnl
367  *
368  *  Description     Remove a fixed channel to a remote device.
369  *
370  *  Parameters:     Fixed CID
371  *                  BD Address of remote
372  *                  Idle timeout to use (or 0xFFFF if don't care)
373  *
374  *  Return value:   true if channel removed
375  *
376  ******************************************************************************/
377 bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda);
378 
379 uint16_t L2CA_GetLeHandle(const RawAddress& rem_bda);
380 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr);
381 
382 void L2CA_LeConnectionUpdate(const RawAddress& rem_bda, uint16_t min_int,
383                              uint16_t max_int, uint16_t latency,
384                              uint16_t timeout, uint16_t min_ce_len,
385                              uint16_t max_ce_len);
386 
387 /*******************************************************************************
388  *
389  * Function         L2CA_SetLeGattTimeout
390  *
391  * Description      Higher layers call this function to set the idle timeout for
392  *                  a fixed channel. The "idle timeout" is the amount of time
393  *                  that a connection can remain up with no L2CAP channels on
394  *                  it. A timeout of zero means that the connection will be torn
395  *                  down immediately when the last channel is removed.
396  *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
397  *                  A bd_addr is the remote BD address. If bd_addr =
398  *                  RawAddress::kAny, then the idle timeouts for all active
399  *                  l2cap links will be changed.
400  *
401  * Returns          true if command succeeded, false if failed
402  *
403  ******************************************************************************/
404 bool L2CA_SetLeGattTimeout(const RawAddress& rem_bda, uint16_t idle_tout);
405 
406 bool L2CA_MarkLeLinkAsActive(const RawAddress& rem_bda);
407 
408 bool L2CA_UpdateBleConnParams(const RawAddress& rem_bda, uint16_t min_int,
409                               uint16_t max_int, uint16_t latency,
410                               uint16_t timeout, uint16_t min_ce_len,
411                               uint16_t max_ce_len);
412 /* When called with lock=true, LE connection parameters will be locked on
413  * fastest value, and we won't accept request to change it from remote. When
414  * called with lock=false, parameters are relaxed.
415  */
416 void L2CA_LockBleConnParamsForServiceDiscovery(const RawAddress& rem_bda,
417                                                bool lock);
418 
419 /* When called with lock=true, LE connection parameters will be locked on
420  * fastest value, and we won't accept request to change it from remote. When
421  * called with lock=false, parameters are relaxed.
422  */
423 void L2CA_LockBleConnParamsForProfileConnection(const RawAddress& rem_bda,
424                                                 bool lock);
425 
426 /*******************************************************************************
427  *
428  * Function         L2CA_GetBleConnRole
429  *
430  * Description      This function returns the connection role.
431  *
432  * Returns          link role.
433  *
434  ******************************************************************************/
435 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr);
436 
437 /**
438  * Check whether an ACL or LE link to the remote device is established
439  */
440 bool L2CA_IsLinkEstablished(const RawAddress& bd_addr, tBT_TRANSPORT transport);
441 
442 void L2CA_ConnectForSecurity(const RawAddress& bd_addr);
443 
444 // Set bonding state to acquire/release link refcount
445 void L2CA_SetBondingState(const RawAddress& p_bd_addr, bool is_bonding);
446 
447 void L2CA_SwitchRoleToCentral(const RawAddress& addr);
448 
449 bool L2CA_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version,
450                             uint16_t* manufacturer, uint16_t* lmp_sub_version);
451 
452 uint8_t* L2CA_ReadRemoteFeatures(const RawAddress& addr);
453 
454 void L2CA_DisconnectLink(const RawAddress& remote);
455 
456 uint16_t L2CA_GetNumLinks();
457 
458 bool L2CA_IsLeLink(uint16_t acl_handle);
459 
460 void L2CA_ReadConnectionAddr(const RawAddress& pseudo_addr,
461                              RawAddress& conn_addr,
462                              tBLE_ADDR_TYPE* p_addr_type);
463 
464 bool L2CA_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr,
465                                    RawAddress& conn_addr,
466                                    tBLE_ADDR_TYPE* p_addr_type);
467 
468 }  // namespace shim
469 }  // namespace bluetooth
470