1/******************************************************************************
2 *
3 *  Copyright (C) 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 "bt_target.h"
30#include "hcidefs.h"
31#include "l2cdefs.h"
32
33/*****************************************************************************
34 *  Constants
35 ****************************************************************************/
36
37/* Define the minimum offset that L2CAP needs in a buffer. This is made up of
38 * HCI type(1), len(2), handle(2), L2CAP len(2) and CID(2) => 9
39*/
40#define L2CAP_MIN_OFFSET 13 /* plus control(2), SDU length(2) */
41
42#define L2CAP_LCC_SDU_LENGTH 2
43#define L2CAP_LCC_OFFSET \
44  (L2CAP_MIN_OFFSET + L2CAP_LCC_SDU_LENGTH) /* plus SDU length(2) */
45
46/* ping result codes */
47#define L2CAP_PING_RESULT_OK 0      /* Ping reply received OK     */
48#define L2CAP_PING_RESULT_NO_LINK 1 /* Link could not be setup    */
49#define L2CAP_PING_RESULT_NO_RESP 2 /* Remote L2CAP did not reply */
50
51/* result code for L2CA_DataWrite() */
52#define L2CAP_DW_FAILED false
53#define L2CAP_DW_SUCCESS true
54#define L2CAP_DW_CONGESTED 2
55
56/* Values for priority parameter to L2CA_SetAclPriority */
57#define L2CAP_PRIORITY_NORMAL 0
58#define L2CAP_PRIORITY_HIGH 1
59
60/* Values for priority parameter to L2CA_SetTxPriority */
61#define L2CAP_CHNL_PRIORITY_HIGH 0
62#define L2CAP_CHNL_PRIORITY_MEDIUM 1
63#define L2CAP_CHNL_PRIORITY_LOW 2
64
65typedef uint8_t tL2CAP_CHNL_PRIORITY;
66
67/* Values for Tx/Rx data rate parameter to L2CA_SetChnlDataRate */
68#define L2CAP_CHNL_DATA_RATE_HIGH 3
69#define L2CAP_CHNL_DATA_RATE_MEDIUM 2
70#define L2CAP_CHNL_DATA_RATE_LOW 1
71#define L2CAP_CHNL_DATA_RATE_NO_TRAFFIC 0
72
73typedef uint8_t tL2CAP_CHNL_DATA_RATE;
74
75/* Data Packet Flags  (bits 2-15 are reserved) */
76/* layer specific 14-15 bits are used for FCR SAR */
77#define L2CAP_FLUSHABLE_MASK 0x0003
78#define L2CAP_FLUSHABLE_CH_BASED 0x0000
79#define L2CAP_FLUSHABLE_PKT 0x0001
80#define L2CAP_NON_FLUSHABLE_PKT 0x0002
81
82/* L2CA_FlushChannel num_to_flush definitions */
83#define L2CAP_FLUSH_CHANS_ALL 0xffff
84#define L2CAP_FLUSH_CHANS_GET 0x0000
85
86/* special CID for Multi-AV for reporting congestion */
87#define L2CAP_MULTI_AV_CID 0
88
89/* length of the HCI header block */
90/* HCI header(4) + SNK count(1) + FCR bits(1) + AV data length(2) */
91#define L2CAP_MULTI_AV_HCI_HDR_LEN 8
92
93/* length of padding for 4 bytes align */
94#define L2CAP_MULTI_AV_PADDING_LEN 2
95
96/* length of the HCI header block with padding for FCR */
97/* HCI header(4) + SNK count(1) + FCR bits(1) + AV data length(2) + padding(2)
98 */
99#define L2CAP_MULTI_AV_HCI_HDR_LEN_WITH_PADDING 10
100
101/* length of the L2CAP header block */
102/* HCI header(4) + L2CAP header(4) + padding(4) or control word(2) + FCS(2) */
103#define L2CAP_MULTI_AV_L2C_HDR_LEN 12
104
105/* definition used for L2CA_SetDesireRole */
106#define L2CAP_ROLE_SLAVE HCI_ROLE_SLAVE
107#define L2CAP_ROLE_MASTER HCI_ROLE_MASTER
108/* set this bit to allow switch at create conn */
109#define L2CAP_ROLE_ALLOW_SWITCH 0x80
110/* set this bit to disallow switch at create conn */
111#define L2CAP_ROLE_DISALLOW_SWITCH 0x40
112#define L2CAP_ROLE_CHECK_SWITCH 0xC0
113
114/* Values for 'allowed_modes' field passed in structure tL2CAP_ERTM_INFO
115*/
116#define L2CAP_FCR_CHAN_OPT_BASIC (1 << L2CAP_FCR_BASIC_MODE)
117#define L2CAP_FCR_CHAN_OPT_ERTM (1 << L2CAP_FCR_ERTM_MODE)
118#define L2CAP_FCR_CHAN_OPT_STREAM (1 << L2CAP_FCR_STREAM_MODE)
119
120#define L2CAP_FCR_CHAN_OPT_ALL_MASK                     \
121  (L2CAP_FCR_CHAN_OPT_BASIC | L2CAP_FCR_CHAN_OPT_ERTM | \
122   L2CAP_FCR_CHAN_OPT_STREAM)
123
124/* Validity check for PSM.  PSM values must be odd.  Also, all PSM values must
125 * be assigned such that the least significant bit of the most sigificant
126 * octet equals zero.
127*/
128#define L2C_INVALID_PSM(psm) (((psm)&0x0101) != 0x0001)
129#define L2C_IS_VALID_PSM(psm) (((psm)&0x0101) == 0x0001)
130#define L2C_IS_VALID_LE_PSM(psm) (((psm) > 0x0000) && ((psm) < 0x0100))
131
132/*****************************************************************************
133 *  Type Definitions
134 ****************************************************************************/
135
136typedef struct {
137#define L2CAP_FCR_BASIC_MODE 0x00
138#define L2CAP_FCR_ERTM_MODE 0x03
139#define L2CAP_FCR_STREAM_MODE 0x04
140#define L2CAP_FCR_LE_COC_MODE 0x05
141
142  uint8_t mode;
143
144  uint8_t tx_win_sz;
145  uint8_t max_transmit;
146  uint16_t rtrans_tout;
147  uint16_t mon_tout;
148  uint16_t mps;
149} tL2CAP_FCR_OPTS;
150
151/* Define a structure to hold the configuration parameters. Since the
152 * parameters are optional, for each parameter there is a boolean to
153 * use to signify its presence or absence.
154*/
155typedef struct {
156  uint16_t result; /* Only used in confirm messages */
157  bool mtu_present;
158  uint16_t mtu;
159  bool qos_present;
160  FLOW_SPEC qos;
161  bool flush_to_present;
162  uint16_t flush_to;
163  bool fcr_present;
164  tL2CAP_FCR_OPTS fcr;
165  bool fcs_present; /* Optionally bypasses FCS checks */
166  uint8_t fcs;      /* '0' if desire is to bypass FCS, otherwise '1' */
167  bool ext_flow_spec_present;
168  tHCI_EXT_FLOW_SPEC ext_flow_spec;
169  uint16_t flags; /* bit 0: 0-no continuation, 1-continuation */
170} tL2CAP_CFG_INFO;
171
172/* Define a structure to hold the configuration parameter for LE L2CAP
173 * connection oriented channels.
174*/
175typedef struct {
176  uint16_t mtu;
177  uint16_t mps;
178  uint16_t credits;
179} tL2CAP_LE_CFG_INFO;
180
181/* L2CAP channel configured field bitmap */
182#define L2CAP_CH_CFG_MASK_MTU 0x0001
183#define L2CAP_CH_CFG_MASK_QOS 0x0002
184#define L2CAP_CH_CFG_MASK_FLUSH_TO 0x0004
185#define L2CAP_CH_CFG_MASK_FCR 0x0008
186#define L2CAP_CH_CFG_MASK_FCS 0x0010
187#define L2CAP_CH_CFG_MASK_EXT_FLOW_SPEC 0x0020
188
189typedef uint16_t tL2CAP_CH_CFG_BITS;
190
191/*********************************
192 *  Callback Functions Prototypes
193 *********************************/
194
195/* Connection indication callback prototype. Parameters are
196 *              BD Address of remote
197 *              Local CID assigned to the connection
198 *              PSM that the remote wants to connect to
199 *              Identifier that the remote sent
200*/
201typedef void(tL2CA_CONNECT_IND_CB)(BD_ADDR, uint16_t, uint16_t, uint8_t);
202
203/* Connection confirmation callback prototype. Parameters are
204 *              Local CID
205 *              Result - 0 = connected, non-zero means failure reason
206*/
207typedef void(tL2CA_CONNECT_CFM_CB)(uint16_t, uint16_t);
208
209/* Connection pending callback prototype. Parameters are
210 *              Local CID
211*/
212typedef void(tL2CA_CONNECT_PND_CB)(uint16_t);
213
214/* Configuration indication callback prototype. Parameters are
215 *              Local CID assigned to the connection
216 *              Pointer to configuration info
217*/
218typedef void(tL2CA_CONFIG_IND_CB)(uint16_t, tL2CAP_CFG_INFO*);
219
220/* Configuration confirm callback prototype. Parameters are
221 *              Local CID assigned to the connection
222 *              Pointer to configuration info
223*/
224typedef void(tL2CA_CONFIG_CFM_CB)(uint16_t, tL2CAP_CFG_INFO*);
225
226/* Disconnect indication callback prototype. Parameters are
227 *              Local CID
228 *              Boolean whether upper layer should ack this
229*/
230typedef void(tL2CA_DISCONNECT_IND_CB)(uint16_t, bool);
231
232/* Disconnect confirm callback prototype. Parameters are
233 *              Local CID
234 *              Result
235*/
236typedef void(tL2CA_DISCONNECT_CFM_CB)(uint16_t, uint16_t);
237
238/* QOS Violation indication callback prototype. Parameters are
239 *              BD Address of violating device
240*/
241typedef void(tL2CA_QOS_VIOLATION_IND_CB)(BD_ADDR);
242
243/* Data received indication callback prototype. Parameters are
244 *              Local CID
245 *              Address of buffer
246*/
247typedef void(tL2CA_DATA_IND_CB)(uint16_t, BT_HDR*);
248
249/* Echo response callback prototype. Note that this is not included in the
250 * registration information, but is passed to L2CAP as part of the API to
251 * actually send an echo request. Parameters are
252 *              Result
253*/
254typedef void(tL2CA_ECHO_RSP_CB)(uint16_t);
255
256/* Callback function prototype to pass broadcom specific echo response  */
257/* to the upper layer                                                   */
258typedef void(tL2CA_ECHO_DATA_CB)(BD_ADDR, uint16_t, uint8_t*);
259
260/* Congestion status callback protype. This callback is optional. If
261 * an application tries to send data when the transmit queue is full,
262 * the data will anyways be dropped. The parameter is:
263 *              Local CID
264 *              true if congested, false if uncongested
265*/
266typedef void(tL2CA_CONGESTION_STATUS_CB)(uint16_t, bool);
267
268/* Callback prototype for number of packets completed events.
269 * This callback notifies the application when Number of Completed Packets
270 * event has been received.
271 * This callback is originally designed for 3DG devices.
272 * The parameter is:
273 *          peer BD_ADDR
274*/
275typedef void(tL2CA_NOCP_CB)(BD_ADDR);
276
277/* Transmit complete callback protype. This callback is optional. If
278 * set, L2CAP will call it when packets are sent or flushed. If the
279 * count is 0xFFFF, it means all packets are sent for that CID (eRTM
280 * mode only). The parameters are:
281 *              Local CID
282 *              Number of SDUs sent or dropped
283*/
284typedef void(tL2CA_TX_COMPLETE_CB)(uint16_t, uint16_t);
285
286/* Define the structure that applications use to register with
287 * L2CAP. This structure includes callback functions. All functions
288 * MUST be provided, with the exception of the "connect pending"
289 * callback and "congestion status" callback.
290*/
291typedef struct {
292  tL2CA_CONNECT_IND_CB* pL2CA_ConnectInd_Cb;
293  tL2CA_CONNECT_CFM_CB* pL2CA_ConnectCfm_Cb;
294  tL2CA_CONNECT_PND_CB* pL2CA_ConnectPnd_Cb;
295  tL2CA_CONFIG_IND_CB* pL2CA_ConfigInd_Cb;
296  tL2CA_CONFIG_CFM_CB* pL2CA_ConfigCfm_Cb;
297  tL2CA_DISCONNECT_IND_CB* pL2CA_DisconnectInd_Cb;
298  tL2CA_DISCONNECT_CFM_CB* pL2CA_DisconnectCfm_Cb;
299  tL2CA_QOS_VIOLATION_IND_CB* pL2CA_QoSViolationInd_Cb;
300  tL2CA_DATA_IND_CB* pL2CA_DataInd_Cb;
301  tL2CA_CONGESTION_STATUS_CB* pL2CA_CongestionStatus_Cb;
302  tL2CA_TX_COMPLETE_CB* pL2CA_TxComplete_Cb;
303
304} tL2CAP_APPL_INFO;
305
306/* Define the structure that applications use to create or accept
307 * connections with enhanced retransmission mode.
308*/
309typedef struct {
310  uint8_t preferred_mode;
311  uint8_t allowed_modes;
312  uint16_t user_rx_buf_size;
313  uint16_t user_tx_buf_size;
314  uint16_t fcr_rx_buf_size;
315  uint16_t fcr_tx_buf_size;
316
317} tL2CAP_ERTM_INFO;
318
319#define L2CA_REGISTER(a, b, c) L2CA_Register(a, (tL2CAP_APPL_INFO*)(b))
320#define L2CA_DEREGISTER(a) L2CA_Deregister(a)
321#define L2CA_CONNECT_REQ(a, b, c) L2CA_ErtmConnectReq(a, b, c)
322#define L2CA_CONNECT_RSP(a, b, c, d, e, f) L2CA_ErtmConnectRsp(a, b, c, d, e, f)
323#define L2CA_CONFIG_REQ(a, b) L2CA_ConfigReq(a, b)
324#define L2CA_CONFIG_RSP(a, b) L2CA_ConfigRsp(a, b)
325#define L2CA_DISCONNECT_REQ(a) L2CA_DisconnectReq(a)
326#define L2CA_DISCONNECT_RSP(a) L2CA_DisconnectRsp(a)
327#define L2CA_DATA_WRITE(a, b) L2CA_DataWrite(a, b)
328#define L2CA_REGISTER_COC(a, b, c) L2CA_RegisterLECoc(a, (tL2CAP_APPL_INFO*)(b))
329#define L2CA_DEREGISTER_COC(a) L2CA_DeregisterLECoc(a)
330#define L2CA_CONNECT_COC_REQ(a, b, c) L2CA_ConnectLECocReq(a, b, c)
331#define L2CA_CONNECT_COC_RSP(a, b, c, d, e, f) \
332  L2CA_ConnectLECocRsp(a, b, c, d, e, f)
333#define L2CA_GET_PEER_COC_CONFIG(a, b) L2CA_GetPeerLECocConfig(a, b)
334
335/*****************************************************************************
336 *  External Function Declarations
337 ****************************************************************************/
338
339/*******************************************************************************
340 *
341 * Function         L2CA_Register
342 *
343 * Description      Other layers call this function to register for L2CAP
344 *                  services.
345 *
346 * Returns          PSM to use or zero if error. Typically, the PSM returned
347 *                  is the same as was passed in, but for an outgoing-only
348 *                  connection to a dynamic PSM, a "virtual" PSM is returned
349 *                  and should be used in the calls to L2CA_ConnectReq() and
350 *                  BTM_SetSecurityLevel().
351 *
352 ******************************************************************************/
353extern uint16_t L2CA_Register(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info);
354
355/*******************************************************************************
356 *
357 * Function         L2CA_Deregister
358 *
359 * Description      Other layers call this function to deregister for L2CAP
360 *                  services.
361 *
362 * Returns          void
363 *
364 ******************************************************************************/
365extern void L2CA_Deregister(uint16_t psm);
366
367/*******************************************************************************
368 *
369 * Function         L2CA_AllocatePSM
370 *
371 * Description      Other layers call this function to find an unused PSM for
372 *                  L2CAP services.
373 *
374 * Returns          PSM to use.
375 *
376 ******************************************************************************/
377extern uint16_t L2CA_AllocatePSM(void);
378
379/*******************************************************************************
380 *
381 * Function         L2CA_ConnectReq
382 *
383 * Description      Higher layers call this function to create an L2CAP
384 *                  connection.
385 *                  Note that the connection is not established at this time,
386 *                  but connection establishment gets started. The callback
387 *                  will be invoked when connection establishes or fails.
388 *
389 * Returns          the CID of the connection, or 0 if it failed to start
390 *
391 ******************************************************************************/
392extern uint16_t L2CA_ConnectReq(uint16_t psm, BD_ADDR p_bd_addr);
393
394/*******************************************************************************
395 *
396 * Function         L2CA_ConnectRsp
397 *
398 * Description      Higher layers call this function to accept an incoming
399 *                  L2CAP connection, for which they had gotten an connect
400 *                  indication callback.
401 *
402 * Returns          true for success, false for failure
403 *
404 ******************************************************************************/
405extern bool L2CA_ConnectRsp(BD_ADDR p_bd_addr, uint8_t id, uint16_t lcid,
406                            uint16_t result, uint16_t status);
407
408/*******************************************************************************
409 *
410 * Function         L2CA_ErtmConnectReq
411 *
412 * Description      Higher layers call this function to create an L2CAP
413 *                  connection that needs to use Enhanced Retransmission Mode.
414 *                  Note that the connection is not established at this time,
415 *                  but connection establishment gets started. The callback
416 *                  will be invoked when connection establishes or fails.
417 *
418 * Returns          the CID of the connection, or 0 if it failed to start
419 *
420 ******************************************************************************/
421extern uint16_t L2CA_ErtmConnectReq(uint16_t psm, BD_ADDR p_bd_addr,
422                                    tL2CAP_ERTM_INFO* p_ertm_info);
423
424/*******************************************************************************
425 *
426 * Function         L2CA_RegisterLECoc
427 *
428 * Description      Other layers call this function to register for L2CAP
429 *                  Connection Oriented Channel.
430 *
431 * Returns          PSM to use or zero if error. Typically, the PSM returned
432 *                  is the same as was passed in, but for an outgoing-only
433 *                  connection to a dynamic PSM, a "virtual" PSM is returned
434 *                  and should be used in the calls to L2CA_ConnectLECocReq()
435 *                  and BTM_SetSecurityLevel().
436 *
437 ******************************************************************************/
438extern uint16_t L2CA_RegisterLECoc(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info);
439
440/*******************************************************************************
441 *
442 * Function         L2CA_DeregisterLECoc
443 *
444 * Description      Other layers call this function to deregister for L2CAP
445 *                  Connection Oriented Channel.
446 *
447 * Returns          void
448 *
449 ******************************************************************************/
450extern void L2CA_DeregisterLECoc(uint16_t psm);
451
452/*******************************************************************************
453 *
454 * Function         L2CA_ConnectLECocReq
455 *
456 * Description      Higher layers call this function to create an L2CAP LE COC.
457 *                  Note that the connection is not established at this time,
458 *                  but connection establishment gets started. The callback
459 *                  will be invoked when connection establishes or fails.
460 *
461 * Returns          the CID of the connection, or 0 if it failed to start
462 *
463 ******************************************************************************/
464extern uint16_t L2CA_ConnectLECocReq(uint16_t psm, BD_ADDR p_bd_addr,
465                                     tL2CAP_LE_CFG_INFO* p_cfg);
466
467/*******************************************************************************
468 *
469 * Function         L2CA_ConnectLECocRsp
470 *
471 * Description      Higher layers call this function to accept an incoming
472 *                  L2CAP LE COC connection, for which they had gotten a connect
473 *                  indication callback.
474 *
475 * Returns          true for success, false for failure
476 *
477 ******************************************************************************/
478extern bool L2CA_ConnectLECocRsp(BD_ADDR p_bd_addr, uint8_t id, uint16_t lcid,
479                                 uint16_t result, uint16_t status,
480                                 tL2CAP_LE_CFG_INFO* p_cfg);
481
482/*******************************************************************************
483 *
484 *  Function         L2CA_GetPeerLECocConfig
485 *
486 *  Description      Get peers configuration for LE Connection Oriented Channel.
487 *
488 *  Return value:    true if peer is connected
489 *
490 ******************************************************************************/
491extern bool L2CA_GetPeerLECocConfig(uint16_t lcid,
492                                    tL2CAP_LE_CFG_INFO* peer_cfg);
493
494// This function sets the callback routines for the L2CAP connection referred to
495// by |local_cid|. The callback routines can only be modified for outgoing
496// connections established by |L2CA_ConnectReq| or accepted incoming
497// connections. |callbacks| must not be NULL. This function returns true if the
498// callbacks could be updated, false if not (e.g. |local_cid| was not found).
499bool L2CA_SetConnectionCallbacks(uint16_t local_cid,
500                                 const tL2CAP_APPL_INFO* callbacks);
501
502/*******************************************************************************
503 *
504 * Function         L2CA_ErtmConnectRsp
505 *
506 * Description      Higher layers call this function to accept an incoming
507 *                  L2CAP connection, for which they had gotten an connect
508 *                  indication callback, and for which the higher layer wants
509 *                  to use Enhanced Retransmission Mode.
510 *
511 * Returns          true for success, false for failure
512 *
513 ******************************************************************************/
514extern bool L2CA_ErtmConnectRsp(BD_ADDR p_bd_addr, uint8_t id, uint16_t lcid,
515                                uint16_t result, uint16_t status,
516                                tL2CAP_ERTM_INFO* p_ertm_info);
517
518/*******************************************************************************
519 *
520 * Function         L2CA_ConfigReq
521 *
522 * Description      Higher layers call this function to send configuration.
523 *
524 * Returns          true if configuration sent, else false
525 *
526 ******************************************************************************/
527extern bool L2CA_ConfigReq(uint16_t cid, tL2CAP_CFG_INFO* p_cfg);
528
529/*******************************************************************************
530 *
531 * Function         L2CA_ConfigRsp
532 *
533 * Description      Higher layers call this function to send a configuration
534 *                  response.
535 *
536 * Returns          true if configuration response sent, else false
537 *
538 ******************************************************************************/
539extern bool L2CA_ConfigRsp(uint16_t cid, tL2CAP_CFG_INFO* p_cfg);
540
541/*******************************************************************************
542 *
543 * Function         L2CA_DisconnectReq
544 *
545 * Description      Higher layers call this function to disconnect a channel.
546 *
547 * Returns          true if disconnect sent, else false
548 *
549 ******************************************************************************/
550extern bool L2CA_DisconnectReq(uint16_t cid);
551
552/*******************************************************************************
553 *
554 * Function         L2CA_DisconnectRsp
555 *
556 * Description      Higher layers call this function to acknowledge the
557 *                  disconnection of a channel.
558 *
559 * Returns          void
560 *
561 ******************************************************************************/
562extern bool L2CA_DisconnectRsp(uint16_t cid);
563
564/*******************************************************************************
565 *
566 * Function         L2CA_DataWrite
567 *
568 * Description      Higher layers call this function to write data.
569 *
570 * Returns          L2CAP_DW_SUCCESS, if data accepted, else false
571 *                  L2CAP_DW_CONGESTED, if data accepted and the channel is
572 *                                      congested
573 *                  L2CAP_DW_FAILED, if error
574 *
575 ******************************************************************************/
576extern uint8_t L2CA_DataWrite(uint16_t cid, BT_HDR* p_data);
577
578/*******************************************************************************
579 *
580 * Function         L2CA_Ping
581 *
582 * Description      Higher layers call this function to send an echo request.
583 *
584 * Returns          true if echo request sent, else false.
585 *
586 ******************************************************************************/
587extern bool L2CA_Ping(BD_ADDR p_bd_addr, tL2CA_ECHO_RSP_CB* p_cb);
588
589/*******************************************************************************
590 *
591 * Function         L2CA_Echo
592 *
593 * Description      Higher layers call this function to send an echo request
594 *                  with application-specific data.
595 *
596 * Returns          true if echo request sent, else false.
597 *
598 ******************************************************************************/
599extern bool L2CA_Echo(BD_ADDR p_bd_addr, BT_HDR* p_data,
600                      tL2CA_ECHO_DATA_CB* p_callback);
601
602// Given a local channel identifier, |lcid|, this function returns the bound
603// remote channel identifier, |rcid|, and the ACL link handle, |handle|. If
604// |lcid| is not known or is invalid, this function returns false and does not
605// modify the values pointed at by |rcid| and |handle|. |rcid| and |handle| may
606// be NULL.
607bool L2CA_GetIdentifiers(uint16_t lcid, uint16_t* rcid, uint16_t* handle);
608
609/*******************************************************************************
610 *
611 * Function         L2CA_SetIdleTimeout
612 *
613 * Description      Higher layers call this function to set the idle timeout for
614 *                  a connection, or for all future connections. The "idle
615 *                  timeout" is the amount of time that a connection can remain
616 *                  up with no L2CAP channels on it. A timeout of zero means
617 *                  that the connection will be torn down immediately when the
618 *                  last channel is removed. A timeout of 0xFFFF means no
619 *                  timeout. Values are in seconds.
620 *
621 * Returns          true if command succeeded, false if failed
622 *
623 ******************************************************************************/
624extern bool L2CA_SetIdleTimeout(uint16_t cid, uint16_t timeout, bool is_global);
625
626/*******************************************************************************
627 *
628 * Function         L2CA_SetIdleTimeoutByBdAddr
629 *
630 * Description      Higher layers call this function to set the idle timeout for
631 *                  a connection. The "idle timeout" is the amount of time that
632 *                  a connection can remain up with no L2CAP channels on it.
633 *                  A timeout of zero means that the connection will be torn
634 *                  down immediately when the last channel is removed.
635 *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
636 *                  A bd_addr is the remote BD address. If bd_addr = BT_BD_ANY,
637 *                  then the idle timeouts for all active l2cap links will be
638 *                  changed.
639 *
640 * Returns          true if command succeeded, false if failed
641 *
642 * NOTE             This timeout applies to all logical channels active on the
643 *                  ACL link.
644 ******************************************************************************/
645extern bool L2CA_SetIdleTimeoutByBdAddr(BD_ADDR bd_addr, uint16_t timeout,
646                                        tBT_TRANSPORT transport);
647
648/*******************************************************************************
649 *
650 * Function         L2CA_SetTraceLevel
651 *
652 * Description      This function sets the trace level for L2CAP. If called with
653 *                  a value of 0xFF, it simply reads the current trace level.
654 *
655 * Returns          the new (current) trace level
656 *
657 ******************************************************************************/
658extern uint8_t L2CA_SetTraceLevel(uint8_t trace_level);
659
660/*******************************************************************************
661 *
662 * Function     L2CA_SetDesireRole
663 *
664 * Description  This function sets the desire role for L2CAP.
665 *              If the new role is L2CAP_ROLE_ALLOW_SWITCH, allow switch on
666 *              HciCreateConnection.
667 *              If the new role is L2CAP_ROLE_DISALLOW_SWITCH, do not allow
668 *              switch on HciCreateConnection.
669 *
670 *              If the new role is a valid role (HCI_ROLE_MASTER or
671 *              HCI_ROLE_SLAVE), the desire role is set to the new value.
672 *              Otherwise, it is not changed.
673 *
674 * Returns      the new (current) role
675 *
676 ******************************************************************************/
677extern uint8_t L2CA_SetDesireRole(uint8_t new_role);
678
679/*******************************************************************************
680 *
681 * Function     L2CA_LocalLoopbackReq
682 *
683 * Description  This function sets up a CID for local loopback
684 *
685 * Returns      CID of 0 if none.
686 *
687 ******************************************************************************/
688extern uint16_t L2CA_LocalLoopbackReq(uint16_t psm, uint16_t handle,
689                                      BD_ADDR p_bd_addr);
690
691/*******************************************************************************
692 *
693 * Function     L2CA_FlushChannel
694 *
695 * Description  This function flushes none, some or all buffers queued up
696 *              for xmission for a particular CID. If called with
697 *              L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
698 *              of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
699 *              flushes all buffers.  All other values specifies the maximum
700 *              buffers to flush.
701 *
702 * Returns      Number of buffers left queued for that CID
703 *
704 ******************************************************************************/
705extern uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush);
706
707/*******************************************************************************
708 *
709 * Function         L2CA_SetAclPriority
710 *
711 * Description      Sets the transmission priority for an ACL channel.
712 *                  (For initial implementation only two values are valid.
713 *                  L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
714 *
715 * Returns          true if a valid channel, else false
716 *
717 ******************************************************************************/
718extern bool L2CA_SetAclPriority(BD_ADDR bd_addr, uint8_t priority);
719
720/*******************************************************************************
721 *
722 * Function         L2CA_FlowControl
723 *
724 * Description      Higher layers call this function to flow control a channel.
725 *
726 *                  data_enabled - true data flows, false data is stopped
727 *
728 * Returns          true if valid channel, else false
729 *
730 ******************************************************************************/
731extern bool L2CA_FlowControl(uint16_t cid, bool data_enabled);
732
733/*******************************************************************************
734 *
735 * Function         L2CA_SendTestSFrame
736 *
737 * Description      Higher layers call this function to send a test S-frame.
738 *
739 * Returns          true if valid Channel, else false
740 *
741 ******************************************************************************/
742extern bool L2CA_SendTestSFrame(uint16_t cid, uint8_t sup_type,
743                                uint8_t back_track);
744
745/*******************************************************************************
746 *
747 * Function         L2CA_SetTxPriority
748 *
749 * Description      Sets the transmission priority for a channel. (FCR Mode)
750 *
751 * Returns          true if a valid channel, else false
752 *
753 ******************************************************************************/
754extern bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority);
755
756/*******************************************************************************
757 *
758 * Function         L2CA_RegForNoCPEvt
759 *
760 * Description      Register callback for Number of Completed Packets event.
761 *
762 * Input Param      p_cb - callback for Number of completed packets event
763 *                  p_bda - BT address of remote device
764 *
765 * Returns
766 *
767 ******************************************************************************/
768extern bool L2CA_RegForNoCPEvt(tL2CA_NOCP_CB* p_cb, BD_ADDR p_bda);
769
770/*******************************************************************************
771 *
772 * Function         L2CA_SetChnlDataRate
773 *
774 * Description      Sets the tx/rx data rate for a channel.
775 *
776 * Returns          true if a valid channel, else false
777 *
778 ******************************************************************************/
779extern bool L2CA_SetChnlDataRate(uint16_t cid, tL2CAP_CHNL_DATA_RATE tx,
780                                 tL2CAP_CHNL_DATA_RATE rx);
781
782typedef void(tL2CA_RESERVE_CMPL_CBACK)(void);
783
784/*******************************************************************************
785 *
786 * Function         L2CA_SetFlushTimeout
787 *
788 * Description      This function set the automatic flush time out in Baseband
789 *                  for ACL-U packets.
790 *                  BdAddr : the remote BD address of ACL link. If it is
791 *                           BT_DB_ANY then the flush time out will be applied
792 *                           to all ACL link.
793 *                  FlushTimeout: flush time out in ms
794 *                           0x0000 : No automatic flush
795 *                           L2CAP_NO_RETRANSMISSION : No retransmission
796 *                           0x0002 - 0xFFFE : flush time out, if
797 *                                             (flush_tout * 8) + 3 / 5)
798 *                                               <= HCI_MAX_AUTO_FLUSH_TOUT
799 *                                             (in 625us slot).
800 *                                    Otherwise, return false.
801 *                           L2CAP_NO_AUTOMATIC_FLUSH : No automatic flush
802 *
803 * Returns          true if command succeeded, false if failed
804 *
805 * NOTE             This flush timeout applies to all logical channels active on
806 *                  the ACL link.
807 ******************************************************************************/
808extern bool L2CA_SetFlushTimeout(BD_ADDR bd_addr, uint16_t flush_tout);
809
810/*******************************************************************************
811 *
812 * Function         L2CA_DataWriteEx
813 *
814 * Description      Higher layers call this function to write data with extended
815 *                  flags.
816 *                  flags : L2CAP_FLUSHABLE_CH_BASED
817 *                          L2CAP_FLUSHABLE_PKT
818 *                          L2CAP_NON_FLUSHABLE_PKT
819 *
820 * Returns          L2CAP_DW_SUCCESS, if data accepted, else false
821 *                  L2CAP_DW_CONGESTED, if data accepted and the channel is
822 *                                      congested
823 *                  L2CAP_DW_FAILED, if error
824 *
825 ******************************************************************************/
826extern uint8_t L2CA_DataWriteEx(uint16_t cid, BT_HDR* p_data, uint16_t flags);
827
828/*******************************************************************************
829 *
830 * Function         L2CA_SetChnlFlushability
831 *
832 * Description      Higher layers call this function to set a channels
833 *                  flushability flags
834 *
835 * Returns          true if CID found, else false
836 *
837 ******************************************************************************/
838extern bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable);
839
840/*******************************************************************************
841 *
842 *  Function         L2CA_GetPeerFeatures
843 *
844 *  Description      Get a peers features and fixed channel map
845 *
846 *  Parameters:      BD address of the peer
847 *                   Pointers to features and channel mask storage area
848 *
849 *  Return value:    true if peer is connected
850 *
851 ******************************************************************************/
852extern bool L2CA_GetPeerFeatures(BD_ADDR bd_addr, uint32_t* p_ext_feat,
853                                 uint8_t* p_chnl_mask);
854
855/*******************************************************************************
856 *
857 *  Function         L2CA_GetBDAddrbyHandle
858 *
859 *  Description      Get BD address for the given HCI handle
860 *
861 *  Parameters:      HCI handle
862 *                   BD address of the peer
863 *
864 *  Return value:    true if found lcb for the given handle, false otherwise
865 *
866 ******************************************************************************/
867extern bool L2CA_GetBDAddrbyHandle(uint16_t handle, BD_ADDR bd_addr);
868
869/*******************************************************************************
870 *
871 *  Function         L2CA_GetChnlFcrMode
872 *
873 *  Description      Get the channel FCR mode
874 *
875 *  Parameters:      Local CID
876 *
877 *  Return value:    Channel mode
878 *
879 ******************************************************************************/
880extern uint8_t L2CA_GetChnlFcrMode(uint16_t lcid);
881
882/*******************************************************************************
883 *
884 *                      UCD callback prototypes
885 *
886 ******************************************************************************/
887
888/* UCD discovery. Parameters are
889 *      BD Address of remote
890 *      Data Type
891 *      Data
892*/
893#define L2CAP_UCD_INFO_TYPE_RECEPTION 0x01
894#define L2CAP_UCD_INFO_TYPE_MTU 0x02
895
896typedef void(tL2CA_UCD_DISCOVER_CB)(BD_ADDR, uint8_t, uint32_t);
897
898/* UCD data received. Parameters are
899 *      BD Address of remote
900 *      Pointer to buffer with data
901*/
902typedef void(tL2CA_UCD_DATA_CB)(BD_ADDR, BT_HDR*);
903
904/* Congestion status callback protype. This callback is optional. If
905 * an application tries to send data when the transmit queue is full,
906 * the data will anyways be dropped. The parameter is:
907 *              remote BD_ADDR
908 *              true if congested, false if uncongested
909*/
910typedef void(tL2CA_UCD_CONGESTION_STATUS_CB)(BD_ADDR, bool);
911
912/* UCD registration info (the callback addresses and PSM)
913*/
914typedef struct {
915  tL2CA_UCD_DISCOVER_CB* pL2CA_UCD_Discover_Cb;
916  tL2CA_UCD_DATA_CB* pL2CA_UCD_Data_Cb;
917  tL2CA_UCD_CONGESTION_STATUS_CB* pL2CA_UCD_Congestion_Status_Cb;
918} tL2CAP_UCD_CB_INFO;
919
920/*******************************************************************************
921 *
922 *  Function        L2CA_UcdRegister
923 *
924 *  Description     Register PSM on UCD.
925 *
926 *  Parameters:     tL2CAP_UCD_CB_INFO
927 *
928 *  Return value:   true if successs
929 *
930 ******************************************************************************/
931extern bool L2CA_UcdRegister(uint16_t psm, tL2CAP_UCD_CB_INFO* p_cb_info);
932
933/*******************************************************************************
934 *
935 *  Function        L2CA_UcdDeregister
936 *
937 *  Description     Deregister PSM on UCD.
938 *
939 *  Parameters:     PSM
940 *
941 *  Return value:   true if successs
942 *
943 ******************************************************************************/
944extern bool L2CA_UcdDeregister(uint16_t psm);
945
946/*******************************************************************************
947 *
948 *  Function        L2CA_UcdDiscover
949 *
950 *  Description     Discover UCD of remote device.
951 *
952 *  Parameters:     PSM
953 *                  BD_ADDR of remote device
954 *                  info_type : L2CAP_UCD_INFO_TYPE_RECEPTION
955 *                              L2CAP_UCD_INFO_TYPE_MTU
956 *
957 *
958 *  Return value:   true if successs
959 *
960 ******************************************************************************/
961extern bool L2CA_UcdDiscover(uint16_t psm, BD_ADDR rem_bda, uint8_t info_type);
962
963/*******************************************************************************
964 *
965 *  Function        L2CA_UcdDataWrite
966 *
967 *  Description     Send UCD to remote device
968 *
969 *  Parameters:     PSM
970 *                  BD Address of remote
971 *                  Pointer to buffer of type BT_HDR
972 *                  flags : L2CAP_FLUSHABLE_CH_BASED
973 *                          L2CAP_FLUSHABLE_PKT
974 *                          L2CAP_NON_FLUSHABLE_PKT
975 *
976 * Return value     L2CAP_DW_SUCCESS, if data accepted
977 *                  L2CAP_DW_FAILED,  if error
978 *
979 ******************************************************************************/
980extern uint16_t L2CA_UcdDataWrite(uint16_t psm, BD_ADDR rem_bda, BT_HDR* p_buf,
981                                  uint16_t flags);
982
983/*******************************************************************************
984 *
985 *  Function        L2CA_UcdSetIdleTimeout
986 *
987 *  Description     Set UCD Idle timeout.
988 *
989 *  Parameters:     BD Addr
990 *                  Timeout in second
991 *
992 *  Return value:   true if successs
993 *
994 ******************************************************************************/
995extern bool L2CA_UcdSetIdleTimeout(BD_ADDR rem_bda, uint16_t timeout);
996
997/*******************************************************************************
998 *
999 * Function         L2CA_UCDSetTxPriority
1000 *
1001 * Description      Sets the transmission priority for a connectionless channel.
1002 *
1003 * Returns          true if a valid channel, else false
1004 *
1005 ******************************************************************************/
1006extern bool L2CA_UCDSetTxPriority(BD_ADDR rem_bda,
1007                                  tL2CAP_CHNL_PRIORITY priority);
1008
1009/*******************************************************************************
1010 *
1011 *                      Fixed Channel callback prototypes
1012 *
1013 ******************************************************************************/
1014
1015/* Fixed channel connected and disconnected. Parameters are
1016 *      channel
1017 *      BD Address of remote
1018 *      true if channel is connected, false if disconnected
1019 *      Reason for connection failure
1020 *      transport : physical transport, BR/EDR or LE
1021*/
1022typedef void(tL2CA_FIXED_CHNL_CB)(uint16_t, BD_ADDR, bool, uint16_t,
1023                                  tBT_TRANSPORT);
1024
1025/* Signalling data received. Parameters are
1026 *      channel
1027 *      BD Address of remote
1028 *      Pointer to buffer with data
1029*/
1030typedef void(tL2CA_FIXED_DATA_CB)(uint16_t, BD_ADDR, BT_HDR*);
1031
1032/* Congestion status callback protype. This callback is optional. If
1033 * an application tries to send data when the transmit queue is full,
1034 * the data will anyways be dropped. The parameter is:
1035 *      remote BD_ADDR
1036 *      true if congested, false if uncongested
1037*/
1038typedef void(tL2CA_FIXED_CONGESTION_STATUS_CB)(BD_ADDR, bool);
1039
1040/* Fixed channel registration info (the callback addresses and channel config)
1041*/
1042typedef struct {
1043  tL2CA_FIXED_CHNL_CB* pL2CA_FixedConn_Cb;
1044  tL2CA_FIXED_DATA_CB* pL2CA_FixedData_Cb;
1045  tL2CA_FIXED_CONGESTION_STATUS_CB* pL2CA_FixedCong_Cb;
1046  tL2CAP_FCR_OPTS fixed_chnl_opts;
1047
1048  uint16_t default_idle_tout;
1049  tL2CA_TX_COMPLETE_CB*
1050      pL2CA_FixedTxComplete_Cb; /* fixed channel tx complete callback */
1051} tL2CAP_FIXED_CHNL_REG;
1052
1053#if (L2CAP_NUM_FIXED_CHNLS > 0)
1054/*******************************************************************************
1055 *
1056 *  Function        L2CA_RegisterFixedChannel
1057 *
1058 *  Description     Register a fixed channel.
1059 *
1060 *  Parameters:     Fixed Channel #
1061 *                  Channel Callbacks and config
1062 *
1063 *  Return value:   true if registered OK
1064 *
1065 ******************************************************************************/
1066extern bool L2CA_RegisterFixedChannel(uint16_t fixed_cid,
1067                                      tL2CAP_FIXED_CHNL_REG* p_freg);
1068
1069/*******************************************************************************
1070 *
1071 *  Function        L2CA_ConnectFixedChnl
1072 *
1073 *  Description     Connect an fixed signalling channel to a remote device.
1074 *
1075 *  Parameters:     Fixed CID
1076 *                  BD Address of remote
1077 *
1078 *  Return value:   true if connection started
1079 *
1080 ******************************************************************************/
1081extern bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, BD_ADDR bd_addr);
1082extern bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, BD_ADDR bd_addr,
1083                                  uint8_t initiating_phys);
1084
1085/*******************************************************************************
1086 *
1087 *  Function        L2CA_SendFixedChnlData
1088 *
1089 *  Description     Write data on a fixed signalling channel.
1090 *
1091 *  Parameters:     Fixed CID
1092 *                  BD Address of remote
1093 *                  Pointer to buffer of type BT_HDR
1094 *
1095 * Return value     L2CAP_DW_SUCCESS, if data accepted
1096 *                  L2CAP_DW_FAILED,  if error
1097 *
1098 ******************************************************************************/
1099extern uint16_t L2CA_SendFixedChnlData(uint16_t fixed_cid, BD_ADDR rem_bda,
1100                                       BT_HDR* p_buf);
1101
1102/*******************************************************************************
1103 *
1104 *  Function        L2CA_RemoveFixedChnl
1105 *
1106 *  Description     Remove a fixed channel to a remote device.
1107 *
1108 *  Parameters:     Fixed CID
1109 *                  BD Address of remote
1110 *                  Idle timeout to use (or 0xFFFF if don't care)
1111 *
1112 *  Return value:   true if channel removed
1113 *
1114 ******************************************************************************/
1115extern bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, BD_ADDR rem_bda);
1116
1117/*******************************************************************************
1118 *
1119 * Function         L2CA_SetFixedChannelTout
1120 *
1121 * Description      Higher layers call this function to set the idle timeout for
1122 *                  a fixed channel. The "idle timeout" is the amount of time
1123 *                  that a connection can remain up with no L2CAP channels on
1124 *                  it. A timeout of zero means that the connection will be torn
1125 *                  down immediately when the last channel is removed.
1126 *                  A timeout of 0xFFFF means no timeout. Values are in seconds.
1127 *                  A bd_addr is the remote BD address. If bd_addr = BT_BD_ANY,
1128 *                  then the idle timeouts for all active l2cap links will be
1129 *                  changed.
1130 *
1131 * Returns          true if command succeeded, false if failed
1132 *
1133 ******************************************************************************/
1134extern bool L2CA_SetFixedChannelTout(BD_ADDR rem_bda, uint16_t fixed_cid,
1135                                     uint16_t idle_tout);
1136
1137#endif /* (L2CAP_NUM_FIXED_CHNLS > 0) */
1138
1139/*******************************************************************************
1140 *
1141 * Function     L2CA_GetCurrentConfig
1142 *
1143 * Description  This function returns configurations of L2CAP channel
1144 *              pp_our_cfg : pointer of our saved configuration options
1145 *              p_our_cfg_bits : valid config in bitmap
1146 *              pp_peer_cfg: pointer of peer's saved configuration options
1147 *              p_peer_cfg_bits : valid config in bitmap
1148 *
1149 * Returns      true if successful
1150 *
1151 ******************************************************************************/
1152extern bool L2CA_GetCurrentConfig(uint16_t lcid, tL2CAP_CFG_INFO** pp_our_cfg,
1153                                  tL2CAP_CH_CFG_BITS* p_our_cfg_bits,
1154                                  tL2CAP_CFG_INFO** pp_peer_cfg,
1155                                  tL2CAP_CH_CFG_BITS* p_peer_cfg_bits);
1156
1157/*******************************************************************************
1158 *
1159 * Function     L2CA_GetConnectionConfig
1160 *
1161 * Description  This function polulates the mtu, remote cid & lm_handle for
1162 *              a given local L2CAP channel
1163 *
1164 * Returns      true if successful
1165 *
1166 ******************************************************************************/
1167extern bool L2CA_GetConnectionConfig(uint16_t lcid, uint16_t* mtu,
1168                                     uint16_t* rcid, uint16_t* handle);
1169
1170/*******************************************************************************
1171 *
1172 *  Function        L2CA_CancelBleConnectReq
1173 *
1174 *  Description     Cancel a pending connection attempt to a BLE device.
1175 *
1176 *  Parameters:     BD Address of remote
1177 *
1178 *  Return value:   true if connection was cancelled
1179 *
1180 ******************************************************************************/
1181extern bool L2CA_CancelBleConnectReq(BD_ADDR rem_bda);
1182
1183/*******************************************************************************
1184 *
1185 *  Function        L2CA_UpdateBleConnParams
1186 *
1187 *  Description     Update BLE connection parameters.
1188 *
1189 *  Parameters:     BD Address of remote
1190 *
1191 *  Return value:   true if update started
1192 *
1193 ******************************************************************************/
1194extern bool L2CA_UpdateBleConnParams(BD_ADDR rem_bdRa, uint16_t min_int,
1195                                     uint16_t max_int, uint16_t latency,
1196                                     uint16_t timeout);
1197
1198/*******************************************************************************
1199 *
1200 *  Function        L2CA_EnableUpdateBleConnParams
1201 *
1202 *  Description     Update BLE connection parameters.
1203 *
1204 *  Parameters:     BD Address of remote
1205 *                  enable flag
1206 *
1207 *  Return value:   true if update started
1208 *
1209 ******************************************************************************/
1210extern bool L2CA_EnableUpdateBleConnParams(BD_ADDR rem_bda, bool enable);
1211
1212/*******************************************************************************
1213 *
1214 * Function         L2CA_GetBleConnRole
1215 *
1216 * Description      This function returns the connection role.
1217 *
1218 * Returns          link role.
1219 *
1220 ******************************************************************************/
1221extern uint8_t L2CA_GetBleConnRole(BD_ADDR bd_addr);
1222
1223/*******************************************************************************
1224 *
1225 * Function         L2CA_GetDisconnectReason
1226 *
1227 * Description      This function returns the disconnect reason code.
1228 *
1229 *  Parameters:     BD Address of remote
1230 *                  Physical transport for the L2CAP connection (BR/EDR or LE)
1231 *
1232 * Returns          disconnect reason
1233 *
1234 ******************************************************************************/
1235extern uint16_t L2CA_GetDisconnectReason(BD_ADDR remote_bda,
1236                                         tBT_TRANSPORT transport);
1237
1238#endif /* L2C_API_H */
1239