• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "stack/include/l2c_api.h"
24 #include "types/hci_role.h"
25 
26 namespace bluetooth {
27 namespace shim {
28 
29 /*******************************************************************************
30  *
31  * Function         L2CA_Register
32  *
33  * Description      Other layers call this function to register for L2CAP
34  *                  services.
35  *
36  * Returns          PSM to use or zero if error. Typically, the PSM returned
37  *                  is the same as was passed in, but for an outgoing-only
38  *                  connection to a dynamic PSM, a "virtual" PSM is returned
39  *                  and should be used in the calls to L2CA_ConnectReq() and
40  *                  BTM_SetSecurityLevel().
41  *
42  ******************************************************************************/
43 uint16_t L2CA_Register(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
44                        bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info,
45                        uint16_t my_mtu, uint16_t required_remote_mtu,
46                        uint16_t sec_level);
47 
48 /*******************************************************************************
49  *
50  * Function         L2CA_Deregister
51  *
52  * Description      Other layers call this function to deregister for L2CAP
53  *                  services.
54  *
55  * Returns          void
56  *
57  ******************************************************************************/
58 void L2CA_Deregister(uint16_t psm);
59 
60 /*******************************************************************************
61  *
62  * Function         L2CA_AllocateLePSM
63  *
64  * Description      Other layers call this function to find an unused LE PSM for
65  *                  L2CAP services.
66  *
67  * Returns          LE_PSM to use if success. Otherwise returns 0.
68  *
69  ******************************************************************************/
70 uint16_t L2CA_AllocateLePSM(void);
71 
72 /*******************************************************************************
73  *
74  * Function         L2CA_FreeLePSM
75  *
76  * Description      Free an assigned LE PSM.
77  *
78  * Returns          void
79  *
80  ******************************************************************************/
81 void L2CA_FreeLePSM(uint16_t psm);
82 
83 /*******************************************************************************
84  *
85  * Function         L2CA_ConnectReq
86  *
87  * Description      Higher layers call this function to create an L2CAP
88  *                  connection.
89  *                  Note that the connection is not established at this time,
90  *                  but connection establishment gets started. The callback
91  *                  will be invoked when connection establishes or fails.
92  *
93  * Returns          the CID of the connection, or 0 if it failed to start
94  *
95  ******************************************************************************/
96 uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& p_bd_addr);
97 
98 /*******************************************************************************
99  *
100  * Function         L2CA_RegisterLECoc
101  *
102  * Description      Other layers call this function to register for L2CAP
103  *                  Connection Oriented Channel.
104  *
105  * Returns          PSM to use or zero if error. Typically, the PSM returned
106  *                  is the same as was passed in, but for an outgoing-only
107  *                  connection to a dynamic PSM, a "virtual" PSM is returned
108  *                  and should be used in the calls to L2CA_ConnectLECocReq()
109  *                  and BTM_SetSecurityLevel().
110  *
111  ******************************************************************************/
112 uint16_t L2CA_RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
113                             uint16_t sec_level, tL2CAP_LE_CFG_INFO cfg);
114 
115 /*******************************************************************************
116  *
117  * Function         L2CA_DeregisterLECoc
118  *
119  * Description      Other layers call this function to deregister for L2CAP
120  *                  Connection Oriented Channel.
121  *
122  * Returns          void
123  *
124  ******************************************************************************/
125 void L2CA_DeregisterLECoc(uint16_t psm);
126 
127 /*******************************************************************************
128  *
129  * Function         L2CA_ConnectLECocReq
130  *
131  * Description      Higher layers call this function to create an L2CAP LE COC.
132  *                  Note that the connection is not established at this time,
133  *                  but connection establishment gets started. The callback
134  *                  will be invoked when connection establishes or fails.
135  *
136  * Returns          the CID of the connection, or 0 if it failed to start
137  *
138  ******************************************************************************/
139 uint16_t L2CA_ConnectLECocReq(uint16_t psm, const RawAddress& p_bd_addr,
140                               tL2CAP_LE_CFG_INFO* p_cfg);
141 
142 /*******************************************************************************
143  *
144  *  Function         L2CA_GetPeerLECocConfig
145  *
146  *  Description      Get peers configuration for LE Connection Oriented Channel.
147  *
148  *  Return value:    true if peer is connected
149  *
150  ******************************************************************************/
151 bool L2CA_GetPeerLECocConfig(uint16_t lcid, tL2CAP_LE_CFG_INFO* peer_cfg);
152 
153 /*******************************************************************************
154  *
155  *  Function         L2CA_ReconfigCreditBasedConnsReq
156  *
157  *  Description      Start reconfigure procedure on Credit Based Connection Oriented
158  *                   Channels.
159  *
160  *  Return value:    true if peer is connected
161  *
162  ******************************************************************************/
163 
164 bool L2CA_ReconfigCreditBasedConnsReq(const RawAddress& bd_addr,
165                                       std::vector<uint16_t>& lcids,
166                                       tL2CAP_LE_CFG_INFO* p_cfg);
167 
168 /*******************************************************************************
169  *
170  *  Function         L2CA_ConnectCreditBasedReq
171  *
172  *  Description      With this function L2CAP will initiate setup of up to 5 credit
173  *                   based connections for given psm using provided configuration.
174  *                   L2CAP will notify user on the connection result, by calling
175  *                   pL2CA_CreditBasedConnectCfm_Cb for each cid with a result.
176  *
177  *  Return value: vector of allocated local cids for the connection
178  *
179  ******************************************************************************/
180 
181 extern std::vector<uint16_t> L2CA_ConnectCreditBasedReq(
182     uint16_t psm, const RawAddress& p_bd_addr, tL2CAP_LE_CFG_INFO* p_cfg);
183 
184 /*******************************************************************************
185  *
186  *  Function         L2CA_ConnectCreditBasedRsp
187  *
188  *  Description      Response for the pL2CA_CreditBasedConnectInd_Cb which is the
189  *                   indication for peer requesting credit based connection.
190  *
191  *  Return value:    true if peer is connected
192  *
193  ******************************************************************************/
194 
195 extern bool L2CA_ConnectCreditBasedRsp(const RawAddress& p_bd_addr, uint8_t id,
196                                        std::vector<uint16_t>& accepted_lcids,
197                                        uint16_t result,
198                                        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 // Given a local channel identifier, |lcid|, this function returns the bound
230 // remote channel identifier, |rcid|. If
231 // |lcid| is not known or is invalid, this function returns false and does not
232 // modify the value pointed at by |rcid|. |rcid| may be NULL.
233 bool L2CA_GetRemoteCid(uint16_t lcid, uint16_t* rcid);
234 
235 /*******************************************************************************
236  *
237  * Function         L2CA_SetIdleTimeoutByBdAddr
238  *
239  * Description      Higher layers call this function to set the idle timeout for
240  *                  a connection. The "idle timeout" is the amount of time that
241  *                  a connection can remain up with no L2CAP channels on it.
242  *                  A timeout of zero means that the connection will be torn
243  *                  down immediately when the last channel is removed.
244  *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
245  *                  A bd_addr is the remote BD address. If bd_addr =
246  *                  RawAddress::kAny, then the idle timeouts for all active
247  *                  l2cap links will be changed.
248  *
249  * Returns          true if command succeeded, false if failed
250  *
251  * NOTE             This timeout applies to all logical channels active on the
252  *                  ACL link.
253  ******************************************************************************/
254 bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout,
255                                  tBT_TRANSPORT transport);
256 
257 /*******************************************************************************
258  *
259  * Function         L2CA_SetTraceLevel
260  *
261  * Description      This function sets the trace level for L2CAP. If called with
262  *                  a value of 0xFF, it simply reads the current trace level.
263  *
264  * Returns          the new (current) trace level
265  *
266  ******************************************************************************/
267 uint8_t L2CA_SetTraceLevel(uint8_t trace_level);
268 
269 /*******************************************************************************
270  *
271  * Function     L2CA_FlushChannel
272  *
273  * Description  This function flushes none, some or all buffers queued up
274  *              for xmission for a particular CID. If called with
275  *              L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
276  *              of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
277  *              flushes all buffers.  All other values specifies the maximum
278  *              buffers to flush.
279  *
280  * Returns      Number of buffers left queued for that CID
281  *
282  ******************************************************************************/
283 uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush);
284 
285 /*******************************************************************************
286  *
287  * Function         L2CA_SetAclPriority
288  *
289  * Description      Sets the transmission priority for an ACL channel.
290  *                  (For initial implementation only two values are valid.
291  *                  L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
292  *
293  * Returns          true if a valid channel, else false
294  *
295  ******************************************************************************/
296 bool L2CA_SetAclPriority(const RawAddress& bd_addr, tL2CAP_PRIORITY priority);
297 
298 /*******************************************************************************
299  *
300  * Function         L2CA_SetTxPriority
301  *
302  * Description      Sets the transmission priority for a channel. (FCR Mode)
303  *
304  * Returns          true if a valid channel, else false
305  *
306  ******************************************************************************/
307 bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority);
308 
309 /*******************************************************************************
310  *
311  * Function         L2CA_SetChnlFlushability
312  *
313  * Description      Higher layers call this function to set a channels
314  *                  flushability flags
315  *
316  * Returns          true if CID found, else false
317  *
318  ******************************************************************************/
319 bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable);
320 
321 /*******************************************************************************
322  *
323  *  Function         L2CA_GetPeerFeatures
324  *
325  *  Description      Get a peers features and fixed channel map
326  *
327  *  Parameters:      BD address of the peer
328  *                   Pointers to features and channel mask storage area
329  *
330  *  Return value:    true if peer is connected
331  *
332  ******************************************************************************/
333 bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat,
334                           uint8_t* p_chnl_mask);
335 
336 /*******************************************************************************
337  *
338  *  Function        L2CA_RegisterFixedChannel
339  *
340  *  Description     Register a fixed channel.
341  *
342  *  Parameters:     Fixed Channel #
343  *                  Channel Callbacks and config
344  *
345  *  Return value:   true if registered OK
346  *
347  ******************************************************************************/
348 bool L2CA_RegisterFixedChannel(uint16_t fixed_cid,
349                                tL2CAP_FIXED_CHNL_REG* p_freg);
350 
351 /*******************************************************************************
352  *
353  *  Function        L2CA_ConnectFixedChnl
354  *
355  *  Description     Connect an fixed signalling channel to a remote device.
356  *
357  *  Parameters:     Fixed CID
358  *                  BD Address of remote
359  *
360  *  Return value:   true if connection started
361  *
362  ******************************************************************************/
363 bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr);
364 
365 /*******************************************************************************
366  *
367  *  Function        L2CA_SendFixedChnlData
368  *
369  *  Description     Write data on a fixed signalling channel.
370  *
371  *  Parameters:     Fixed CID
372  *                  BD Address of remote
373  *                  Pointer to buffer of type BT_HDR
374  *
375  * Return value     L2CAP_DW_SUCCESS, if data accepted
376  *                  L2CAP_DW_FAILED,  if error
377  *
378  ******************************************************************************/
379 uint16_t L2CA_SendFixedChnlData(uint16_t fixed_cid, const RawAddress& rem_bda,
380                                 BT_HDR* p_buf);
381 
382 /*******************************************************************************
383  *
384  *  Function        L2CA_RemoveFixedChnl
385  *
386  *  Description     Remove a fixed channel to a remote device.
387  *
388  *  Parameters:     Fixed CID
389  *                  BD Address of remote
390  *                  Idle timeout to use (or 0xFFFF if don't care)
391  *
392  *  Return value:   true if channel removed
393  *
394  ******************************************************************************/
395 bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda);
396 
397 uint16_t L2CA_GetLeHandle(const RawAddress& rem_bda);
398 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr);
399 
400 void L2CA_LeConnectionUpdate(const RawAddress& rem_bda, uint16_t min_int,
401                              uint16_t max_int, uint16_t latency,
402                              uint16_t timeout, uint16_t min_ce_len,
403                              uint16_t max_ce_len);
404 
405 // When GATT discovery is in progress, use the minimal connection interval, and
406 // reject remote connection updates, until done.
407 bool L2CA_EnableUpdateBleConnParams(const RawAddress& rem_bda, bool enable);
408 
409 /*******************************************************************************
410  *
411  * Function         L2CA_SetLeGattTimeout
412  *
413  * Description      Higher layers call this function to set the idle timeout for
414  *                  a fixed channel. The "idle timeout" is the amount of time
415  *                  that a connection can remain up with no L2CAP channels on
416  *                  it. A timeout of zero means that the connection will be torn
417  *                  down immediately when the last channel is removed.
418  *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
419  *                  A bd_addr is the remote BD address. If bd_addr =
420  *                  RawAddress::kAny, then the idle timeouts for all active
421  *                  l2cap links will be changed.
422  *
423  * Returns          true if command succeeded, false if failed
424  *
425  ******************************************************************************/
426 bool L2CA_SetLeGattTimeout(const RawAddress& rem_bda, uint16_t idle_tout);
427 
428 bool L2CA_UpdateBleConnParams(const RawAddress& rem_bda, uint16_t min_int,
429                               uint16_t max_int, uint16_t latency,
430                               uint16_t timeout, uint16_t min_ce_len,
431                               uint16_t max_ce_len);
432 
433 /*******************************************************************************
434  *
435  *  Function        L2CA_EnableUpdateBleConnParams
436  *
437  *  Description     Update BLE connection parameters.
438  *
439  *  Parameters:     BD Address of remote
440  *                  enable flag
441  *
442  *  Return value:   true if update started
443  *
444  ******************************************************************************/
445 bool L2CA_EnableUpdateBleConnParams(const RawAddress& rem_bda, bool enable);
446 
447 /*******************************************************************************
448  *
449  * Function         L2CA_GetBleConnRole
450  *
451  * Description      This function returns the connection role.
452  *
453  * Returns          link role.
454  *
455  ******************************************************************************/
456 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr);
457 
458 /**
459  * Check whether an ACL or LE link to the remote device is established
460  */
461 bool L2CA_IsLinkEstablished(const RawAddress& bd_addr, tBT_TRANSPORT transport);
462 
463 void L2CA_ConnectForSecurity(const RawAddress& bd_addr);
464 
465 // Set bonding state to acquire/release link refcount
466 void L2CA_SetBondingState(const RawAddress& p_bd_addr, bool is_bonding);
467 
468 // Indicated by shim stack manager that GD L2cap is enabled but Security is not
469 void L2CA_UseLegacySecurityModule();
470 
471 void L2CA_SwitchRoleToCentral(const RawAddress& addr);
472 
473 bool L2CA_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version,
474                             uint16_t* manufacturer, uint16_t* lmp_sub_version);
475 
476 uint8_t* L2CA_ReadRemoteFeatures(const RawAddress& addr);
477 
478 void L2CA_DisconnectLink(const RawAddress& remote);
479 
480 uint16_t L2CA_GetNumLinks();
481 
482 bool L2CA_IsLeLink(uint16_t acl_handle);
483 
484 void L2CA_ReadConnectionAddr(const RawAddress& pseudo_addr,
485                              RawAddress& conn_addr, uint8_t* p_addr_type);
486 
487 bool L2CA_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr,
488                                    RawAddress& conn_addr, uint8_t* p_addr_type);
489 
490 }  // namespace shim
491 }  // namespace bluetooth
492