1 /******************************************************************************
2  *
3  *  Copyright 2003-2016 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 interfaces which are internal to AVCTP.
22  *
23  ******************************************************************************/
24 #ifndef AVCT_INT_H
25 #define AVCT_INT_H
26 
27 #include <string>
28 
29 #include "avct_api.h"
30 #include "include/macros.h"
31 #include "internal_include/bt_target.h"
32 #include "osi/include/fixed_queue.h"
33 #include "stack/include/bt_hdr.h"
34 #include "stack/include/l2cap_interface.h"
35 #include "types/raw_address.h"
36 
37 /*****************************************************************************
38  * constants
39  ****************************************************************************/
40 
41 /* lcb state machine events */
42 enum {
43   AVCT_LCB_UL_BIND_EVT,
44   AVCT_LCB_UL_UNBIND_EVT,
45   AVCT_LCB_UL_MSG_EVT,
46   AVCT_LCB_INT_CLOSE_EVT,
47   AVCT_LCB_LL_OPEN_EVT,
48   AVCT_LCB_LL_CLOSE_EVT,
49   AVCT_LCB_LL_MSG_EVT,
50   AVCT_LCB_LL_CONG_EVT
51 };
52 
53 /* "states" used for L2CAP channel */
54 enum tAVCT_CH {
55   AVCT_CH_IDLE = 0, /* No connection */
56   AVCT_CH_CONN = 1, /* Waiting for connection confirm */
57   AVCT_CH_CFG = 2,  /* Waiting for configuration complete */
58   AVCT_CH_OPEN = 3, /* Channel opened */
59 };
60 
avct_ch_state_text(const int & state)61 inline std::string avct_ch_state_text(const int& state) {
62   switch (state) {
63     CASE_RETURN_STRING(AVCT_CH_IDLE);
64     CASE_RETURN_STRING(AVCT_CH_CONN);
65     CASE_RETURN_STRING(AVCT_CH_CFG);
66     CASE_RETURN_STRING(AVCT_CH_OPEN);
67   }
68   RETURN_UNKNOWN_TYPE_STRING(int, state);
69 }
70 
71 /* "no event" indicator used by ccb dealloc */
72 #define AVCT_NO_EVT 0xFF
73 
74 /*****************************************************************************
75  * data types
76  ****************************************************************************/
77 /* link control block type */
78 typedef struct {
79   uint16_t peer_mtu;      /* peer l2c mtu */
80   uint16_t ch_result;     /* L2CAP connection result value */
81   uint16_t ch_lcid;       /* L2CAP channel LCID */
82   uint8_t allocated;      /* 0, not allocated. index+1, otherwise. */
83   uint8_t state;          /* The state machine state */
84   uint8_t ch_state;       /* L2CAP channel state */
85   BT_HDR* p_rx_msg;       /* Message being reassembled */
86   uint16_t conflict_lcid; /* L2CAP channel LCID */
87   RawAddress peer_addr;   /* BD address of peer */
88   fixed_queue_t* tx_q;    /* Transmit data buffer queue       */
89   bool cong;              /* true, if congested */
90 } tAVCT_LCB;
91 
92 /* browse control block type */
93 typedef struct {
94   uint16_t peer_mtu;      /* peer l2c mtu */
95   uint16_t ch_result;     /* L2CAP connection result value */
96   uint16_t ch_lcid;       /* L2CAP channel LCID */
97   uint8_t allocated;      // 0: no link allocated. otherwise link index+1
98   uint8_t state;          /* The state machine state */
99   uint8_t ch_state;       /* L2CAP channel state */
100   uint16_t conflict_lcid; /* L2CAP channel LCID */
101   BT_HDR* p_tx_msg;       /* Message to be sent - in case the browsing channel is not
102                              open when MsgReg is called */
103   uint8_t ch_close;       /* CCB index+1, if CCB initiated channel close */
104   RawAddress peer_addr;   /* BD address of peer */
105 } tAVCT_BCB;
106 
107 #define AVCT_ALOC_LCB 0x01
108 #define AVCT_ALOC_BCB 0x02
109 /* connection control block */
110 typedef struct {
111   tAVCT_CC cc;       /* parameters from connection creation */
112   tAVCT_LCB* p_lcb;  /* Associated LCB */
113   tAVCT_BCB* p_bcb;  /* associated BCB */
114   bool ch_close;     /* Whether CCB initiated channel close */
115   uint8_t allocated; /* Whether LCB/BCB is allocated */
116 } tAVCT_CCB;
117 
118 /* data type associated with UL_MSG_EVT */
119 typedef struct {
120   BT_HDR* p_buf;
121   tAVCT_CCB* p_ccb;
122   uint8_t label;
123   uint8_t cr;
124 } tAVCT_UL_MSG;
125 
126 /* union associated with lcb state machine events */
127 typedef union {
128   tAVCT_UL_MSG ul_msg;
129   BT_HDR* p_buf;
130   tAVCT_CCB* p_ccb;
131   uint16_t result;
132   bool cong;
133   uint8_t err_code;
134 } tAVCT_LCB_EVT;
135 
136 /* Control block for AVCT */
137 typedef struct {
138   tAVCT_LCB lcb[AVCT_NUM_LINKS]; /* link control blocks */
139   tAVCT_BCB bcb[AVCT_NUM_LINKS]; /* browse control blocks */
140   tAVCT_CCB ccb[AVCT_NUM_CONN];  /* connection control blocks */
141 } tAVCT_CB;
142 
143 /*****************************************************************************
144  * function declarations
145  ****************************************************************************/
146 
147 /* LCB function declarations */
148 void avct_lcb_event(tAVCT_LCB* p_lcb, uint8_t event, tAVCT_LCB_EVT* p_data);
149 void avct_bcb_event(tAVCT_BCB* p_bcb, uint8_t event, tAVCT_LCB_EVT* p_data);
150 void avct_close_bcb(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
151 tAVCT_LCB* avct_lcb_by_bcb(tAVCT_BCB* p_bcb);
152 tAVCT_BCB* avct_bcb_by_lcb(tAVCT_LCB* p_lcb);
153 uint8_t avct_bcb_get_last_ccb_index(tAVCT_BCB* p_bcb, tAVCT_CCB* p_ccb_last);
154 tAVCT_BCB* avct_bcb_by_lcid(uint16_t lcid);
155 tAVCT_LCB* avct_lcb_by_bd(const RawAddress& bd_addr);
156 tAVCT_LCB* avct_lcb_alloc(const RawAddress& bd_addr);
157 void avct_lcb_dealloc(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
158 tAVCT_LCB* avct_lcb_by_lcid(uint16_t lcid);
159 tAVCT_CCB* avct_lcb_has_pid(tAVCT_LCB* p_lcb, uint16_t pid);
160 bool avct_lcb_last_ccb(tAVCT_LCB* p_lcb, tAVCT_CCB* p_ccb_last);
161 
162 /* LCB action functions */
163 void avct_lcb_chnl_open(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
164 void avct_lcb_unbind_disc(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
165 void avct_lcb_open_ind(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
166 void avct_lcb_open_fail(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
167 void avct_lcb_close_ind(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
168 void avct_lcb_close_cfm(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
169 void avct_lcb_bind_conn(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
170 void avct_lcb_chk_disc(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
171 void avct_lcb_chnl_disc(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
172 void avct_lcb_bind_fail(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
173 void avct_lcb_cong_ind(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
174 void avct_lcb_discard_msg(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
175 void avct_lcb_send_msg(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
176 void avct_lcb_msg_ind(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
177 void avct_lcb_free_msg_ind(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data);
178 
179 /* BCB action functions */
180 typedef void (*tAVCT_BCB_ACTION)(tAVCT_BCB* p_bcb, tAVCT_LCB_EVT* p_data);
181 
182 extern const tAVCT_BCB_ACTION avct_bcb_action[];
183 extern const uint8_t avct_lcb_pkt_type_len[];
184 
185 /* CCB function declarations */
186 tAVCT_CCB* avct_ccb_alloc(tAVCT_CC* p_cc);
187 void avct_ccb_dealloc(tAVCT_CCB* p_ccb, uint8_t event, uint16_t result, const RawAddress* bd_addr);
188 uint8_t avct_ccb_to_idx(tAVCT_CCB* p_ccb);
189 tAVCT_CCB* avct_ccb_by_idx(uint8_t idx);
190 
191 extern bool avct_msg_ind_for_src_sink_coexist(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* p_data,
192                                               uint8_t label, uint8_t cr_ipid, uint16_t pid);
193 
194 std::string avct_sm_state_text(const int& state);
195 
196 /*****************************************************************************
197  * global data
198  ****************************************************************************/
199 
200 /* Main control block */
201 extern tAVCT_CB avct_cb;
202 
203 /* L2CAP callback registration structure */
204 extern const tL2CAP_APPL_INFO avct_l2c_appl;
205 extern const tL2CAP_APPL_INFO avct_l2c_br_appl;
206 
207 void avct_l2c_disconnect(uint16_t lcid, uint16_t result);
208 void avct_l2c_br_disconnect(uint16_t lcid, uint16_t result);
209 
210 constexpr uint16_t kAvrcMtu = 512;
211 constexpr uint16_t kAvrcBrMtu = 1008;
212 
213 #endif /* AVCT_INT_H */
214