• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
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 
19 /******************************************************************************
20  *
21  *  this file contains the L2CAP API definitions
22  *
23  ******************************************************************************/
24 #ifndef L2C_API_H
25 #define L2C_API_H
26 
27 #include <stdbool.h>
28 
29 #include <cstdint>
30 #include <vector>
31 
32 #include "bt_target.h"
33 #include "hcidefs.h"
34 #include "l2cdefs.h"
35 #include "stack/include/bt_hdr.h"
36 #include "types/bt_transport.h"
37 #include "types/hci_role.h"
38 #include "types/raw_address.h"
39 
40 /*****************************************************************************
41  *  Constants
42  ****************************************************************************/
43 
44 /* Define the minimum offset that L2CAP needs in a buffer. This is made up of
45  * HCI type(1), len(2), handle(2), L2CAP len(2) and CID(2) => 9
46  */
47 #define L2CAP_MIN_OFFSET 13 /* plus control(2), SDU length(2) */
48 
49 #define L2CAP_LCC_SDU_LENGTH 2
50 #define L2CAP_LCC_OFFSET \
51   (L2CAP_MIN_OFFSET + L2CAP_LCC_SDU_LENGTH) /* plus SDU length(2) */
52 
53 #define L2CAP_FCS_LENGTH 2
54 
55 /* result code for L2CA_DataWrite() */
56 #define L2CAP_DW_FAILED false
57 #define L2CAP_DW_SUCCESS true
58 #define L2CAP_DW_CONGESTED 2
59 
60 /* Values for priority parameter to L2CA_SetAclPriority */
61 typedef enum : uint8_t {
62   L2CAP_PRIORITY_NORMAL = 0,
63   L2CAP_PRIORITY_HIGH = 1,
64 } tL2CAP_PRIORITY;
65 
66 /* Values for priority parameter to L2CA_SetAclLatency */
67 typedef enum : uint8_t {
68   L2CAP_LATENCY_NORMAL = 0,
69   L2CAP_LATENCY_LOW = 1,
70 } tL2CAP_LATENCY;
71 
72 /* Values for priority parameter to L2CA_SetTxPriority */
73 #define L2CAP_CHNL_PRIORITY_HIGH 0
74 #define L2CAP_CHNL_PRIORITY_LOW 2
75 
76 typedef uint8_t tL2CAP_CHNL_PRIORITY;
77 
78 /* Values for Tx/Rx data rate parameter to L2CA_SetChnlDataRate */
79 #define L2CAP_CHNL_DATA_RATE_LOW 1
80 
81 typedef uint8_t tL2CAP_CHNL_DATA_RATE;
82 
83 /* Data Packet Flags  (bits 2-15 are reserved) */
84 /* layer specific 14-15 bits are used for FCR SAR */
85 #define L2CAP_FLUSHABLE_MASK 0x0003
86 #define L2CAP_FLUSHABLE_CH_BASED 0x0000
87 #define L2CAP_FLUSHABLE_PKT 0x0001
88 #define L2CAP_NON_FLUSHABLE_PKT 0x0002
89 
90 /* L2CA_FlushChannel num_to_flush definitions */
91 #define L2CAP_FLUSH_CHANS_ALL 0xffff
92 #define L2CAP_FLUSH_CHANS_GET 0x0000
93 
94 /* Values for 'allowed_modes' field passed in structure tL2CAP_ERTM_INFO
95  */
96 #define L2CAP_FCR_CHAN_OPT_BASIC (1 << L2CAP_FCR_BASIC_MODE)
97 #define L2CAP_FCR_CHAN_OPT_ERTM (1 << L2CAP_FCR_ERTM_MODE)
98 
99 #define L2CAP_FCR_CHAN_OPT_ALL_MASK \
100   (L2CAP_FCR_CHAN_OPT_BASIC | L2CAP_FCR_CHAN_OPT_ERTM)
101 
102 /* Validity check for PSM.  PSM values must be odd.  Also, all PSM values must
103  * be assigned such that the least significant bit of the most sigificant
104  * octet equals zero.
105  */
106 #define L2C_INVALID_PSM(psm) (((psm)&0x0101) != 0x0001)
107 #define L2C_IS_VALID_PSM(psm) (((psm)&0x0101) == 0x0001)
108 #define L2C_IS_VALID_LE_PSM(psm) (((psm) > 0x0000) && ((psm) < 0x0100))
109 
110 #define L2CAP_NO_IDLE_TIMEOUT 0xFFFF
111 
112 /*****************************************************************************
113  *  Type Definitions
114  ****************************************************************************/
115 
116 typedef struct {
117 #define L2CAP_FCR_BASIC_MODE 0x00
118 #define L2CAP_FCR_ERTM_MODE 0x03
119 #define L2CAP_FCR_LE_COC_MODE 0x05
120 
121   uint8_t mode;
122 
123   uint8_t tx_win_sz;
124   uint8_t max_transmit;
125   uint16_t rtrans_tout;
126   uint16_t mon_tout;
127   uint16_t mps;
128 } tL2CAP_FCR_OPTS;
129 
130 /* default options for ERTM mode */
131 constexpr tL2CAP_FCR_OPTS kDefaultErtmOptions = {
132     L2CAP_FCR_ERTM_MODE,
133     10,    /* Tx window size */
134     20,    /* Maximum transmissions before disconnecting */
135     2000,  /* Retransmission timeout (2 secs) */
136     12000, /* Monitor timeout (12 secs) */
137     1010   /* MPS segment size */
138 };
139 
140 typedef struct {
141   uint8_t qos_flags;          /* TBD */
142   uint8_t service_type;       /* see below */
143   uint32_t token_rate;        /* bytes/second */
144   uint32_t token_bucket_size; /* bytes */
145   uint32_t peak_bandwidth;    /* bytes/second */
146   uint32_t latency;           /* microseconds */
147   uint32_t delay_variation;   /* microseconds */
148 } FLOW_SPEC;
149 
150 /* Values for service_type */
151 #define SVC_TYPE_BEST_EFFORT 1
152 #define SVC_TYPE_GUARANTEED 2
153 
154 /* Define a structure to hold the configuration parameters. Since the
155  * parameters are optional, for each parameter there is a boolean to
156  * use to signify its presence or absence.
157  */
158 typedef struct {
159   uint16_t result; /* Only used in confirm messages */
160   bool mtu_present;
161   uint16_t mtu;
162   bool qos_present;
163   FLOW_SPEC qos;
164   bool flush_to_present;
165   uint16_t flush_to;
166   bool fcr_present;
167   tL2CAP_FCR_OPTS fcr;
168   bool fcs_present; /* Optionally bypasses FCS checks */
169   uint8_t fcs;      /* '0' if desire is to bypass FCS, otherwise '1' */
170   bool ext_flow_spec_present;
171   tHCI_EXT_FLOW_SPEC ext_flow_spec;
172   uint16_t flags; /* bit 0: 0-no continuation, 1-continuation */
173 } tL2CAP_CFG_INFO;
174 
175 /* LE credit based L2CAP connection parameters */
176 constexpr uint16_t L2CAP_LE_MIN_MTU = 23;  // Minimum SDU size
177 constexpr uint16_t L2CAP_LE_MIN_MPS = 23;
178 constexpr uint16_t L2CAP_LE_MAX_MPS = 65533;
179 constexpr uint16_t L2CAP_LE_CREDIT_MAX = 65535;
180 
181 // This is initial amout of credits we send, and amount to which we increase
182 // credits once they fall below threshold
183 uint16_t L2CA_LeCreditDefault();
184 
185 // If credit count on remote fall below this value, we send back credits to
186 // reach default value.
187 uint16_t L2CA_LeCreditThreshold();
188 
189 // Max number of CIDs in the L2CAP CREDIT BASED CONNECTION REQUEST
190 constexpr uint16_t L2CAP_CREDIT_BASED_MAX_CIDS = 5;
191 
192 /* Define a structure to hold the configuration parameter for LE L2CAP
193  * connection oriented channels.
194  */
195 struct tL2CAP_LE_CFG_INFO {
196   uint16_t result; /* Only used in confirm messages */
197   uint16_t mtu = 100;
198   uint16_t mps = 100;
199   uint16_t credits = L2CA_LeCreditDefault();
200   uint8_t number_of_channels = L2CAP_CREDIT_BASED_MAX_CIDS;
201 };
202 
203 /*********************************
204  *  Callback Functions Prototypes
205  *********************************/
206 
207 /* Connection indication callback prototype. Parameters are
208  *              BD Address of remote
209  *              Local CID assigned to the connection
210  *              PSM that the remote wants to connect to
211  *              Identifier that the remote sent
212  */
213 typedef void(tL2CA_CONNECT_IND_CB)(const RawAddress&, uint16_t, uint16_t,
214                                    uint8_t);
215 
216 /* Connection confirmation callback prototype. Parameters are
217  *              Local CID
218  *              Result - 0 = connected
219  *              If there is an error, tL2CA_ERROR_CB is invoked
220  */
221 typedef void(tL2CA_CONNECT_CFM_CB)(uint16_t, uint16_t);
222 
223 /* Configuration indication callback prototype. Parameters are
224  *              Local CID assigned to the connection
225  *              Pointer to configuration info
226  */
227 typedef void(tL2CA_CONFIG_IND_CB)(uint16_t, tL2CAP_CFG_INFO*);
228 
229 constexpr uint16_t L2CAP_INITIATOR_LOCAL = 1;
230 constexpr uint16_t L2CAP_INITIATOR_REMOTE = 0;
231 /* Configuration confirm callback prototype. Parameters are
232  *              Local CID assigned to the connection
233  *              Initiator (1 for local, 0 for remote)
234  *              Initial config from remote
235  * If there is an error, tL2CA_ERROR_CB is invoked
236  */
237 typedef void(tL2CA_CONFIG_CFM_CB)(uint16_t, uint16_t, tL2CAP_CFG_INFO*);
238 
239 /* Disconnect indication callback prototype. Parameters are
240  *              Local CID
241  *              Boolean whether upper layer should ack this
242  */
243 typedef void(tL2CA_DISCONNECT_IND_CB)(uint16_t, bool);
244 
245 /* Disconnect confirm callback prototype. Parameters are
246  *              Local CID
247  *              Result
248  */
249 typedef void(tL2CA_DISCONNECT_CFM_CB)(uint16_t, uint16_t);
250 
251 /* Disconnect confirm callback prototype. Parameters are
252  *              Local CID
253  *              Result
254  */
255 typedef void(tL2CA_DATA_IND_CB)(uint16_t, BT_HDR*);
256 
257 /* Congestion status callback protype. This callback is optional. If
258  * an application tries to send data when the transmit queue is full,
259  * the data will anyways be dropped. The parameter is:
260  *              Local CID
261  *              true if congested, false if uncongested
262  */
263 typedef void(tL2CA_CONGESTION_STATUS_CB)(uint16_t, bool);
264 
265 /* Transmit complete callback protype. This callback is optional. If
266  * set, L2CAP will call it when packets are sent or flushed. If the
267  * count is 0xFFFF, it means all packets are sent for that CID (eRTM
268  * mode only). The parameters are:
269  *              Local CID
270  *              Number of SDUs sent or dropped
271  */
272 typedef void(tL2CA_TX_COMPLETE_CB)(uint16_t, uint16_t);
273 
274 /*
275  * Notify the user when the remote send error result on ConnectRsp or ConfigRsp
276  * The parameters are:
277  *              Local CID
278  *              Error type (L2CAP_CONN_OTHER_ERROR for ConnectRsp,
279  *                          L2CAP_CFG_FAILED_NO_REASON for ConfigRsp)
280  */
281 typedef void(tL2CA_ERROR_CB)(uint16_t, uint16_t);
282 
283 /* Create credit based connection request callback prototype. Parameters are
284  *              BD Address of remote
285  *              Vector of allocated local cids to accept
286  *              PSM
287  *              Peer MTU
288  *              Identifier that the remote sent
289  */
290 typedef void(tL2CA_CREDIT_BASED_CONNECT_IND_CB)(const RawAddress& bdaddr,
291                                                 std::vector<uint16_t>& lcids,
292                                                 uint16_t psm, uint16_t peer_mtu,
293                                                 uint8_t identifier);
294 
295 /* Collision Indication callback prototype. Used to notify upper layer that
296  * remote devices sent Credit Based Connection Request but it was rejected due
297  * to ongoing local request. Upper layer might want to sent another request when
298  * local request is completed. Parameters are:
299  *              BD Address of remote
300  */
301 typedef void(tL2CA_CREDIT_BASED_COLLISION_IND_CB)(const RawAddress& bdaddr);
302 
303 /* Credit based connection confirmation callback prototype. Parameters are
304  *              BD Address of remote
305  *              Connected Local CIDs
306  *              Peer MTU
307  *              Result - 0 = connected, non-zero means CID is not connected
308  */
309 typedef void(tL2CA_CREDIT_BASED_CONNECT_CFM_CB)(const RawAddress& bdaddr,
310                                                 uint16_t lcid,
311                                                 uint16_t peer_mtu,
312                                                 uint16_t result);
313 
314 /* Credit based reconfiguration confirm callback prototype. Parameters are
315  *              BD Address of remote
316  *              Local CID assigned to the connection
317  *              Flag indicating if this is local or peer configuration
318  *              Pointer to configuration info
319  */
320 typedef void(tL2CA_CREDIT_BASED_RECONFIG_COMPLETED_CB)(
321     const RawAddress& bdaddr, uint16_t lcid, bool is_local_cfg,
322     tL2CAP_LE_CFG_INFO* p_cfg);
323 
324 /* Define the structure that applications use to register with
325  * L2CAP. This structure includes callback functions. All functions
326  * MUST be provided, with the exception of the "connect pending"
327  * callback and "congestion status" callback.
328  */
329 typedef struct {
330   tL2CA_CONNECT_IND_CB* pL2CA_ConnectInd_Cb;
331   tL2CA_CONNECT_CFM_CB* pL2CA_ConnectCfm_Cb;
332   tL2CA_CONFIG_IND_CB* pL2CA_ConfigInd_Cb;
333   tL2CA_CONFIG_CFM_CB* pL2CA_ConfigCfm_Cb;
334   tL2CA_DISCONNECT_IND_CB* pL2CA_DisconnectInd_Cb;
335   tL2CA_DISCONNECT_CFM_CB* pL2CA_DisconnectCfm_Cb;
336   tL2CA_DATA_IND_CB* pL2CA_DataInd_Cb;
337   tL2CA_CONGESTION_STATUS_CB* pL2CA_CongestionStatus_Cb;
338   tL2CA_TX_COMPLETE_CB* pL2CA_TxComplete_Cb;
339   tL2CA_ERROR_CB* pL2CA_Error_Cb;
340   tL2CA_CREDIT_BASED_CONNECT_IND_CB* pL2CA_CreditBasedConnectInd_Cb;
341   tL2CA_CREDIT_BASED_CONNECT_CFM_CB* pL2CA_CreditBasedConnectCfm_Cb;
342   tL2CA_CREDIT_BASED_RECONFIG_COMPLETED_CB*
343       pL2CA_CreditBasedReconfigCompleted_Cb;
344   tL2CA_CREDIT_BASED_COLLISION_IND_CB* pL2CA_CreditBasedCollisionInd_Cb;
345 } tL2CAP_APPL_INFO;
346 
347 /* Define the structure that applications use to create or accept
348  * connections with enhanced retransmission mode.
349  */
350 typedef struct {
351   uint8_t preferred_mode;
352 } tL2CAP_ERTM_INFO;
353 
354 /**
355  * Stack management declarations
356  */
357 void l2c_init();
358 void l2c_free();
359 
360 /*****************************************************************************
361  *  External Function Declarations
362  ****************************************************************************/
363 
364 // Also does security for you
365 uint16_t L2CA_Register2(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
366                         bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info,
367                         uint16_t my_mtu, uint16_t required_remote_mtu,
368                         uint16_t sec_level);
369 
370 /*******************************************************************************
371  *
372  * Function         L2CA_Register
373  *
374  * Description      Other layers call this function to register for L2CAP
375  *                  services.
376  *
377  * Returns          PSM to use or zero if error. Typically, the PSM returned
378  *                  is the same as was passed in, but for an outgoing-only
379  *                  connection to a dynamic PSM, a "virtual" PSM is returned
380  *                  and should be used in the calls to L2CA_ConnectReq() and
381  *                  BTM_SetSecurityLevel().
382  *
383  ******************************************************************************/
384 uint16_t L2CA_Register(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
385                        bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info,
386                        uint16_t my_mtu, uint16_t required_remote_mtu,
387                        uint16_t sec_level);
388 
389 /*******************************************************************************
390  *
391  * Function         L2CA_Deregister
392  *
393  * Description      Other layers call this function to deregister for L2CAP
394  *                  services.
395  *
396  * Returns          void
397  *
398  ******************************************************************************/
399 void L2CA_Deregister(uint16_t psm);
400 
401 /*******************************************************************************
402  *
403  * Function         L2CA_AllocateLePSM
404  *
405  * Description      Other layers call this function to find an unused LE PSM for
406  *                  L2CAP services.
407  *
408  * Returns          LE_PSM to use if success. Otherwise returns 0.
409  *
410  ******************************************************************************/
411 uint16_t L2CA_AllocateLePSM(void);
412 
413 /*******************************************************************************
414  *
415  * Function         L2CA_FreeLePSM
416  *
417  * Description      Free an assigned LE PSM.
418  *
419  * Returns          void
420  *
421  ******************************************************************************/
422 void L2CA_FreeLePSM(uint16_t psm);
423 
424 uint16_t L2CA_ConnectReq2(uint16_t psm, const RawAddress& p_bd_addr,
425                           uint16_t sec_level);
426 /*******************************************************************************
427  *
428  * Function         L2CA_ConnectReq
429  *
430  * Description      Higher layers call this function to create an L2CAP
431  *                  connection.
432  *                  Note that the connection is not established at this time,
433  *                  but connection establishment gets started. The callback
434  *                  will be invoked when connection establishes or fails.
435  *
436  * Returns          the CID of the connection, or 0 if it failed to start
437  *
438  ******************************************************************************/
439 uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& p_bd_addr);
440 
441 /*******************************************************************************
442  *
443  * Function         L2CA_RegisterLECoc
444  *
445  * Description      Other layers call this function to register for L2CAP
446  *                  Connection Oriented Channel.
447  *
448  * Returns          PSM to use or zero if error. Typically, the PSM returned
449  *                  is the same as was passed in, but for an outgoing-only
450  *                  connection to a dynamic PSM, a "virtual" PSM is returned
451  *                  and should be used in the calls to L2CA_ConnectLECocReq()
452  *                  and BTM_SetSecurityLevel().
453  *
454  ******************************************************************************/
455 uint16_t L2CA_RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info,
456                             uint16_t sec_level, tL2CAP_LE_CFG_INFO cfg);
457 
458 /*******************************************************************************
459  *
460  * Function         L2CA_DeregisterLECoc
461  *
462  * Description      Other layers call this function to deregister for L2CAP
463  *                  Connection Oriented Channel.
464  *
465  * Returns          void
466  *
467  ******************************************************************************/
468 void L2CA_DeregisterLECoc(uint16_t psm);
469 
470 /*******************************************************************************
471  *
472  * Function         L2CA_ConnectLECocReq
473  *
474  * Description      Higher layers call this function to create an L2CAP LE COC.
475  *                  Note that the connection is not established at this time,
476  *                  but connection establishment gets started. The callback
477  *                  will be invoked when connection establishes or fails.
478  *
479  * Returns          the CID of the connection, or 0 if it failed to start
480  *
481  ******************************************************************************/
482 uint16_t L2CA_ConnectLECocReq(uint16_t psm, const RawAddress& p_bd_addr,
483                               tL2CAP_LE_CFG_INFO* p_cfg, uint16_t sec_level);
484 
485 /*******************************************************************************
486  *
487  *  Function         L2CA_GetPeerLECocConfig
488  *
489  *  Description      Get peers configuration for LE Connection Oriented Channel.
490  *
491  *  Return value:    true if peer is connected
492  *
493  ******************************************************************************/
494 bool L2CA_GetPeerLECocConfig(uint16_t lcid, tL2CAP_LE_CFG_INFO* peer_cfg);
495 
496 /*******************************************************************************
497  *
498  *  Function         L2CA_GetPeerLECocCredit
499  *
500  *  Description      Get peers current credit for LE Connection Oriented
501  *                   Channel.
502  *
503  *  Return value:    Number of the peer current credit
504  *
505  ******************************************************************************/
506 uint16_t L2CA_GetPeerLECocCredit(const RawAddress& bd_addr, uint16_t lcid);
507 
508 /*******************************************************************************
509  *
510  *  Function         L2CA_ReconfigCreditBasedConnsReq
511  *
512  *  Description      Start reconfigure procedure on Connection Oriented Channel.
513  *
514  *  Return value:    true if peer is connected
515  *
516  ******************************************************************************/
517 
518 bool L2CA_ReconfigCreditBasedConnsReq(const RawAddress& bd_addr,
519                                       std::vector<uint16_t>& lcids,
520                                       tL2CAP_LE_CFG_INFO* p_cfg);
521 
522 /*******************************************************************************
523  *
524  *  Function         L2CA_ConnectCreditBasedReq
525  *
526  *  Description      With this function L2CAP will initiate setup of up to 5 credit
527  *                   based connections for given psm using provided configuration.
528  *                   L2CAP will notify user on the connection result, by calling
529  *                   pL2CA_CreditBasedConnectCfm_Cb for each cid with a result.
530  *
531  *  Return value: vector of allocated local cids for the connection
532  *
533  ******************************************************************************/
534 
535 std::vector<uint16_t> L2CA_ConnectCreditBasedReq(uint16_t psm,
536                                                  const RawAddress& p_bd_addr,
537                                                  tL2CAP_LE_CFG_INFO* p_cfg);
538 
539 /*******************************************************************************
540  *
541  *  Function         L2CA_ConnectCreditBasedRsp
542  *
543  *  Description      Response for the pL2CA_CreditBasedConnectInd_Cb which is the
544  *                   indication for peer requesting credit based connection.
545  *
546  *  Return value: true if peer is connected
547  *
548  ******************************************************************************/
549 
550 bool L2CA_ConnectCreditBasedRsp(const RawAddress& p_bd_addr, uint8_t id,
551                                 std::vector<uint16_t>& accepted_lcids,
552                                 uint16_t result, tL2CAP_LE_CFG_INFO* p_cfg);
553 /*******************************************************************************
554  *
555  * Function         L2CA_DisconnectReq
556  *
557  * Description      Higher layers call this function to disconnect a channel.
558  *
559  * Returns          true if disconnect sent, else false
560  *
561  ******************************************************************************/
562 bool L2CA_DisconnectReq(uint16_t cid);
563 
564 bool L2CA_DisconnectLECocReq(uint16_t cid);
565 
566 /*******************************************************************************
567  *
568  * Function         L2CA_DataWrite
569  *
570  * Description      Higher layers call this function to write data.
571  *
572  * Returns          L2CAP_DW_SUCCESS, if data accepted, else false
573  *                  L2CAP_DW_CONGESTED, if data accepted and the channel is
574  *                                      congested
575  *                  L2CAP_DW_FAILED, if error
576  *
577  ******************************************************************************/
578 uint8_t L2CA_DataWrite(uint16_t cid, BT_HDR* p_data);
579 
580 uint8_t L2CA_LECocDataWrite(uint16_t cid, BT_HDR* p_data);
581 
582 // Given a local channel identifier, |lcid|, this function returns the bound
583 // remote channel identifier, |rcid|. If
584 // |lcid| is not known or is invalid, this function returns false and does not
585 // modify the value pointed at by |rcid|. |rcid| may be NULL.
586 bool L2CA_GetRemoteCid(uint16_t lcid, uint16_t* rcid);
587 
588 /*******************************************************************************
589  *
590  * Function         L2CA_SetIdleTimeoutByBdAddr
591  *
592  * Description      Higher layers call this function to set the idle timeout for
593  *                  a connection. The "idle timeout" is the amount of time that
594  *                  a connection can remain up with no L2CAP channels on it.
595  *                  A timeout of zero means that the connection will be torn
596  *                  down immediately when the last channel is removed.
597  *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
598  *                  A bd_addr is the remote BD address. If bd_addr =
599  *                  RawAddress::kAny, then the idle timeouts for all active
600  *                  l2cap links will be changed.
601  *
602  * Returns          true if command succeeded, false if failed
603  *
604  * NOTE             This timeout applies to all logical channels active on the
605  *                  ACL link.
606  ******************************************************************************/
607 bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout,
608                                  tBT_TRANSPORT transport);
609 
610 /*******************************************************************************
611  *
612  * Function         L2CA_SetTraceLevel
613  *
614  * Description      This function sets the trace level for L2CAP. If called with
615  *                  a value of 0xFF, it simply reads the current trace level.
616  *
617  * Returns          the new (current) trace level
618  *
619  ******************************************************************************/
620 uint8_t L2CA_SetTraceLevel(uint8_t trace_level);
621 
622 /*******************************************************************************
623  *
624  * Function     L2CA_FlushChannel
625  *
626  * Description  This function flushes none, some or all buffers queued up
627  *              for xmission for a particular CID. If called with
628  *              L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
629  *              of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
630  *              flushes all buffers.  All other values specifies the maximum
631  *              buffers to flush.
632  *
633  * Returns      Number of buffers left queued for that CID
634  *
635  ******************************************************************************/
636 uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush);
637 
638 /*******************************************************************************
639  *
640  * Function         L2CA_UseLatencyMode
641  *
642  * Description      Sets use latency mode for an ACL channel.
643  *
644  * Returns          true if a valid channel, else false
645  *
646  ******************************************************************************/
647 bool L2CA_UseLatencyMode(const RawAddress& bd_addr, bool use_latency_mode);
648 
649 /*******************************************************************************
650  *
651  * Function         L2CA_SetAclPriority
652  *
653  * Description      Sets the transmission priority for an ACL channel.
654  *                  (For initial implementation only two values are valid.
655  *                  L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
656  *
657  * Returns          true if a valid channel, else false
658  *
659  ******************************************************************************/
660 bool L2CA_SetAclPriority(const RawAddress& bd_addr, tL2CAP_PRIORITY priority);
661 
662 /*******************************************************************************
663  *
664  * Function         L2CA_SetAclLatency
665  *
666  * Description      Sets the transmission latency for a channel.
667  *
668  * Returns          true if a valid channel, else false
669  *
670  ******************************************************************************/
671 bool L2CA_SetAclLatency(const RawAddress& bd_addr, tL2CAP_LATENCY latency);
672 
673 /*******************************************************************************
674  *
675  * Function         L2CA_SetTxPriority
676  *
677  * Description      Sets the transmission priority for a channel. (FCR Mode)
678  *
679  * Returns          true if a valid channel, else false
680  *
681  ******************************************************************************/
682 bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority);
683 
684 /*******************************************************************************
685  *
686  * Function         L2CA_SetChnlFlushability
687  *
688  * Description      Higher layers call this function to set a channels
689  *                  flushability flags
690  *
691  * Returns          true if CID found, else false
692  *
693  ******************************************************************************/
694 bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable);
695 
696 /*******************************************************************************
697  *
698  *  Function         L2CA_GetPeerFeatures
699  *
700  *  Description      Get a peers features and fixed channel map
701  *
702  *  Parameters:      BD address of the peer
703  *                   Pointers to features and channel mask storage area
704  *
705  *  Return value:    true if peer is connected
706  *
707  ******************************************************************************/
708 bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat,
709                           uint8_t* p_chnl_mask);
710 
711 /*******************************************************************************
712  *
713  *                      Fixed Channel callback prototypes
714  *
715  ******************************************************************************/
716 
717 /* Fixed channel connected and disconnected. Parameters are
718  *      channel
719  *      BD Address of remote
720  *      true if channel is connected, false if disconnected
721  *      Reason for connection failure
722  *      transport : physical transport, BR/EDR or LE
723  */
724 typedef void(tL2CA_FIXED_CHNL_CB)(uint16_t, const RawAddress&, bool, uint16_t,
725                                   tBT_TRANSPORT);
726 
727 /* Signalling data received. Parameters are
728  *      channel
729  *      BD Address of remote
730  *      Pointer to buffer with data
731  */
732 typedef void(tL2CA_FIXED_DATA_CB)(uint16_t, const RawAddress&, BT_HDR*);
733 
734 /* Congestion status callback protype. This callback is optional. If
735  * an application tries to send data when the transmit queue is full,
736  * the data will anyways be dropped. The parameter is:
737  *      remote BD_ADDR
738  *      true if congested, false if uncongested
739  */
740 typedef void(tL2CA_FIXED_CONGESTION_STATUS_CB)(const RawAddress&, bool);
741 
742 /* Fixed channel registration info (the callback addresses and channel config)
743  */
744 typedef struct {
745   tL2CA_FIXED_CHNL_CB* pL2CA_FixedConn_Cb;
746   tL2CA_FIXED_DATA_CB* pL2CA_FixedData_Cb;
747   tL2CA_FIXED_CONGESTION_STATUS_CB* pL2CA_FixedCong_Cb;
748 
749   uint16_t default_idle_tout;
750 } tL2CAP_FIXED_CHNL_REG;
751 
752 /*******************************************************************************
753  *
754  *  Function        L2CA_RegisterFixedChannel
755  *
756  *  Description     Register a fixed channel.
757  *
758  *  Parameters:     Fixed Channel #
759  *                  Channel Callbacks and config
760  *
761  *  Return value:   true if registered OK
762  *
763  ******************************************************************************/
764 bool L2CA_RegisterFixedChannel(uint16_t fixed_cid,
765                                tL2CAP_FIXED_CHNL_REG* p_freg);
766 
767 /*******************************************************************************
768  *
769  *  Function        L2CA_ConnectFixedChnl
770  *
771  *  Description     Connect an fixed signalling channel to a remote device.
772  *
773  *  Parameters:     Fixed CID
774  *                  BD Address of remote
775  *
776  *  Return value:   true if connection started
777  *
778  ******************************************************************************/
779 bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr);
780 
781 /*******************************************************************************
782  *
783  *  Function        L2CA_SendFixedChnlData
784  *
785  *  Description     Write data on a fixed signalling channel.
786  *
787  *  Parameters:     Fixed CID
788  *                  BD Address of remote
789  *                  Pointer to buffer of type BT_HDR
790  *
791  * Return value     L2CAP_DW_SUCCESS, if data accepted
792  *                  L2CAP_DW_FAILED,  if error
793  *
794  ******************************************************************************/
795 uint16_t L2CA_SendFixedChnlData(uint16_t fixed_cid, const RawAddress& rem_bda,
796                                 BT_HDR* p_buf);
797 
798 /*******************************************************************************
799  *
800  *  Function        L2CA_RemoveFixedChnl
801  *
802  *  Description     Remove a fixed channel to a remote device.
803  *
804  *  Parameters:     Fixed CID
805  *                  BD Address of remote
806  *                  Idle timeout to use (or 0xFFFF if don't care)
807  *
808  *  Return value:   true if channel removed
809  *
810  ******************************************************************************/
811 bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda);
812 
813 /*******************************************************************************
814  *
815  * Function         L2CA_SetLeGattTimeout
816  *
817  * Description      Higher layers call this function to set the idle timeout for
818  *                  a fixed channel. The "idle timeout" is the amount of time
819  *                  that a connection can remain up with no L2CAP channels on
820  *                  it. A timeout of zero means that the connection will be torn
821  *                  down immediately when the last channel is removed.
822  *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
823  *                  A bd_addr is the remote BD address. If bd_addr =
824  *                  RawAddress::kAny, then the idle timeouts for all active
825  *                  l2cap links will be changed.
826  *
827  * Returns          true if command succeeded, false if failed
828  *
829  ******************************************************************************/
830 bool L2CA_SetLeGattTimeout(const RawAddress& rem_bda, uint16_t idle_tout);
831 
832 bool L2CA_MarkLeLinkAsActive(const RawAddress& rem_bda);
833 
834 bool L2CA_UpdateBleConnParams(const RawAddress& rem_bda, uint16_t min_int,
835                               uint16_t max_int, uint16_t latency,
836                               uint16_t timeout, uint16_t min_ce_len,
837                               uint16_t max_ce_len);
838 
839 /*******************************************************************************
840  *
841  *  Function        L2CA_EnableUpdateBleConnParams
842  *
843  *  Description     Update BLE connection parameters.
844  *
845  *  Parameters:     BD Address of remote
846  *                  enable flag
847  *
848  *  Return value:   true if update started
849  *
850  ******************************************************************************/
851 bool L2CA_EnableUpdateBleConnParams(const RawAddress& rem_bda, bool enable);
852 
853 /*******************************************************************************
854  *
855  * Function         L2CA_GetBleConnRole
856  *
857  * Description      This function returns the connection role.
858  *
859  * Returns          link role.
860  *
861  ******************************************************************************/
862 void L2CA_Consolidate(const RawAddress& identity_addr, const RawAddress& rpa);
863 tHCI_ROLE L2CA_GetBleConnRole(const RawAddress& bd_addr);
864 
865 void L2CA_AdjustConnectionIntervals(uint16_t* min_interval,
866                                     uint16_t* max_interval,
867                                     uint16_t floor_interval);
868 
869 /**
870  * Check whether an ACL or LE link to the remote device is established
871  */
872 bool L2CA_IsLinkEstablished(const RawAddress& bd_addr, tBT_TRANSPORT transport);
873 
874 /*******************************************************************************
875  *
876  *  Function        L2CA_SetDefaultSubrate
877  *
878  *  Description     BLE Set Default Subrate.
879  *
880  *  Parameters:     Subrate parameters
881  *
882  *  Return value:   void
883  *
884  ******************************************************************************/
885 void L2CA_SetDefaultSubrate(uint16_t subrate_min, uint16_t subrate_max,
886                             uint16_t max_latency, uint16_t cont_num,
887                             uint16_t timeout);
888 
889 /*******************************************************************************
890  *
891  *  Function        L2CA_SubrateRequest
892  *
893  *  Description     BLE Subrate request.
894  *
895  *  Parameters:     Subrate parameters
896  *
897  *  Return value:   true if update started
898  *
899  ******************************************************************************/
900 bool L2CA_SubrateRequest(const RawAddress& rem_bda, uint16_t subrate_min,
901                          uint16_t subrate_max, uint16_t max_latency,
902                          uint16_t cont_num, uint16_t timeout);
903 
904 /*******************************************************************************
905 **
906 ** Function         L2CA_SetMediaStreamChannel
907 **
908 ** Description      This function is called to set/reset the ccb of active media
909 **                      streaming channel
910 **
911 **  Parameters:     local_media_cid: The local cid provided to A2DP to be used
912 **                      for streaming
913 **                  status: The status of media streaming on this channel
914 **
915 ** Returns          void
916 **
917 *******************************************************************************/
918 void L2CA_SetMediaStreamChannel(uint16_t local_media_cid, bool status);
919 
920 /*******************************************************************************
921 **
922 ** Function         L2CA_isMediaChannel
923 **
924 ** Description      This function returns if the channel id passed as parameter
925 **                      is an A2DP streaming channel
926 **
927 **  Parameters:     handle: Connection handle with the remote device
928 **                  channel_id: Channel ID
929 **                  is_local_cid: Signifies if the channel id passed is local
930 **                      cid or remote cid (true if local, remote otherwise)
931 **
932 ** Returns          bool
933 **
934 *******************************************************************************/
935 bool L2CA_isMediaChannel(uint16_t handle, uint16_t channel_id,
936                          bool is_local_cid);
937 
938 #endif /* L2C_API_H */
939