1 /******************************************************************************
2  *
3  *  Copyright 2003-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 module contains the link control state machine and functions which
22  *  operate on the link control block.
23  *
24  ******************************************************************************/
25 
26 #include <bluetooth/log.h>
27 #include <string.h>
28 
29 #include <cstdint>
30 #include <string>
31 
32 #include "avct_api.h"
33 #include "avct_int.h"
34 #include "device/include/device_iot_conf_defs.h"
35 #include "device/include/device_iot_config.h"
36 #include "include/macros.h"
37 #include "internal_include/bt_target.h"
38 #include "l2cap_types.h"
39 #include "osi/include/allocator.h"
40 #include "osi/include/fixed_queue.h"
41 #include "types/raw_address.h"
42 
43 using namespace bluetooth;
44 
45 /*****************************************************************************
46  * state machine constants and types
47  ****************************************************************************/
48 
49 /* verbose state strings for trace */
50 const char* const avct_lcb_st_str[] = {"LCB_IDLE_ST", "LCB_OPENING_ST", "LCB_OPEN_ST",
51                                        "LCB_CLOSING_ST"};
52 
53 /* verbose event strings for trace */
54 const char* const avct_lcb_evt_str[] = {"UL_BIND_EVT",   "UL_UNBIND_EVT", "UL_MSG_EVT",
55                                         "INT_CLOSE_EVT", "LL_OPEN_EVT",   "LL_CLOSE_EVT",
56                                         "LL_MSG_EVT",    "LL_CONG_EVT"};
57 
58 /* lcb state machine states */
59 enum { AVCT_LCB_IDLE_ST, AVCT_LCB_OPENING_ST, AVCT_LCB_OPEN_ST, AVCT_LCB_CLOSING_ST };
60 
avct_sm_state_text(const int & state)61 std::string avct_sm_state_text(const int& state) {
62   switch (state) {
63     CASE_RETURN_STRING(AVCT_LCB_IDLE_ST);
64     CASE_RETURN_STRING(AVCT_LCB_OPENING_ST);
65     CASE_RETURN_STRING(AVCT_LCB_OPEN_ST);
66     CASE_RETURN_STRING(AVCT_LCB_CLOSING_ST);
67   }
68   RETURN_UNKNOWN_TYPE_STRING(int, state);
69 }
70 
71 /* state machine action enumeration list */
72 enum {
73   AVCT_LCB_CHNL_OPEN,
74   AVCT_LCB_CHNL_DISC,
75   AVCT_LCB_SEND_MSG,
76   AVCT_LCB_OPEN_IND,
77   AVCT_LCB_OPEN_FAIL,
78   AVCT_LCB_CLOSE_IND,
79   AVCT_LCB_CLOSE_CFM,
80   AVCT_LCB_MSG_IND,
81   AVCT_LCB_CONG_IND,
82   AVCT_LCB_BIND_CONN,
83   AVCT_LCB_BIND_FAIL,
84   AVCT_LCB_UNBIND_DISC,
85   AVCT_LCB_CHK_DISC,
86   AVCT_LCB_DISCARD_MSG,
87   AVCT_LCB_DEALLOC,
88   AVCT_LCB_FREE_MSG_IND,
89   AVCT_LCB_NUM_ACTIONS
90 };
91 
92 #define AVCT_LCB_IGNORE AVCT_LCB_NUM_ACTIONS
93 
94 /* type for action functions */
95 typedef void (*tAVCT_LCB_ACTION)(tAVCT_LCB* p_ccb, tAVCT_LCB_EVT* p_data);
96 
97 /* action function list */
98 const tAVCT_LCB_ACTION avct_lcb_action[] = {
99         avct_lcb_chnl_open,    // AVCT_LCB_CHNL_OPEN
100         avct_lcb_chnl_disc,    // AVCT_LCB_CHNL_DISC
101         avct_lcb_send_msg,     // AVCT_LCB_SEND_MSG
102         avct_lcb_open_ind,     // AVCT_LCB_OPEN_IND
103         avct_lcb_open_fail,    // AVCT_LCB_OPEN_FAIL
104         avct_lcb_close_ind,    // AVCT_LCB_CLOSE_IND
105         avct_lcb_close_cfm,    // AVCT_LCB_CLOSE_CFM
106         avct_lcb_msg_ind,      // AVCT_LCB_MSG_IND
107         avct_lcb_cong_ind,     // AVCT_LCB_CONG_IND
108         avct_lcb_bind_conn,    // AVCT_LCB_BIND_CONN
109         avct_lcb_bind_fail,    // AVCT_LCB_BIND_FAIL
110         avct_lcb_unbind_disc,  // AVCT_LCB_UNBIND_DISC
111         avct_lcb_chk_disc,     // AVCT_LCB_CHK_DISC
112         avct_lcb_discard_msg,  // AVCT_LCB_DISCARD_MSG
113         avct_lcb_dealloc,      // AVCT_LCB_DEALLOC
114         avct_lcb_free_msg_ind  // AVCT_LCB_FREE_MSG_IND
115 };
116 
117 /* state table information */
118 #define AVCT_LCB_ACTIONS 2    /* number of actions */
119 #define AVCT_LCB_NEXT_STATE 2 /* position of next state */
120 #define AVCT_LCB_NUM_COLS 3   /* number of columns in state tables */
121 
122 /* state table for idle state */
123 const uint8_t avct_lcb_st_idle[][AVCT_LCB_NUM_COLS] = {
124         /* Event        Action 1                Action 2             Next state */
125         /* UL_BIND */ {AVCT_LCB_CHNL_OPEN, AVCT_LCB_IGNORE, AVCT_LCB_OPENING_ST},
126         /* UL_UNBIND */ {AVCT_LCB_UNBIND_DISC, AVCT_LCB_IGNORE, AVCT_LCB_IDLE_ST},
127         /* UL_MSG */ {AVCT_LCB_DISCARD_MSG, AVCT_LCB_IGNORE, AVCT_LCB_IDLE_ST},
128         /* INT_CLOSE */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_IDLE_ST},
129         /* LL_OPEN */ {AVCT_LCB_OPEN_IND, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST},
130         /* LL_CLOSE */ {AVCT_LCB_CLOSE_IND, AVCT_LCB_DEALLOC, AVCT_LCB_IDLE_ST},
131         /* LL_MSG */ {AVCT_LCB_FREE_MSG_IND, AVCT_LCB_IGNORE, AVCT_LCB_IDLE_ST},
132         /* LL_CONG */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_IDLE_ST}};
133 
134 /* state table for opening state */
135 const uint8_t avct_lcb_st_opening[][AVCT_LCB_NUM_COLS] = {
136         /* Event        Action 1                Action 2             Next state */
137         /* UL_BIND */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_OPENING_ST},
138         /* UL_UNBIND */ {AVCT_LCB_UNBIND_DISC, AVCT_LCB_IGNORE, AVCT_LCB_OPENING_ST},
139         /* UL_MSG */ {AVCT_LCB_DISCARD_MSG, AVCT_LCB_IGNORE, AVCT_LCB_OPENING_ST},
140         /* INT_CLOSE */ {AVCT_LCB_CHNL_DISC, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
141         /* LL_OPEN */ {AVCT_LCB_OPEN_IND, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST},
142         /* LL_CLOSE */ {AVCT_LCB_OPEN_FAIL, AVCT_LCB_DEALLOC, AVCT_LCB_IDLE_ST},
143         /* LL_MSG */ {AVCT_LCB_FREE_MSG_IND, AVCT_LCB_IGNORE, AVCT_LCB_OPENING_ST},
144         /* LL_CONG */ {AVCT_LCB_CONG_IND, AVCT_LCB_IGNORE, AVCT_LCB_OPENING_ST}};
145 
146 /* state table for open state */
147 const uint8_t avct_lcb_st_open[][AVCT_LCB_NUM_COLS] = {
148         /* Event         Action 1             Action 2             Next state */
149         /* UL_BIND */ {AVCT_LCB_BIND_CONN, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST},
150         /* UL_UNBIND */ {AVCT_LCB_CHK_DISC, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST},
151         /* UL_MSG */ {AVCT_LCB_SEND_MSG, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST},
152         /* INT_CLOSE */ {AVCT_LCB_CHNL_DISC, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
153         /* LL_OPEN */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST},
154         /* LL_CLOSE */ {AVCT_LCB_CLOSE_IND, AVCT_LCB_DEALLOC, AVCT_LCB_IDLE_ST},
155         /* LL_MSG */ {AVCT_LCB_MSG_IND, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST},
156         /* LL_CONG */ {AVCT_LCB_CONG_IND, AVCT_LCB_IGNORE, AVCT_LCB_OPEN_ST}};
157 
158 /* state table for closing state */
159 const uint8_t avct_lcb_st_closing[][AVCT_LCB_NUM_COLS] = {
160         /* Event         Action 1               Action 2          Next state */
161         /* UL_BIND */ {AVCT_LCB_BIND_FAIL, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
162         /* UL_UNBIND */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
163         /* UL_MSG */ {AVCT_LCB_DISCARD_MSG, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
164         /* INT_CLOSE */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
165         /* LL_OPEN */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
166         /* LL_CLOSE */ {AVCT_LCB_CLOSE_CFM, AVCT_LCB_DEALLOC, AVCT_LCB_IDLE_ST},
167         /* LL_MSG */ {AVCT_LCB_FREE_MSG_IND, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST},
168         /* LL_CONG */ {AVCT_LCB_IGNORE, AVCT_LCB_IGNORE, AVCT_LCB_CLOSING_ST}};
169 
170 /* type for state table */
171 typedef const uint8_t (*tAVCT_LCB_ST_TBL)[AVCT_LCB_NUM_COLS];
172 
173 /* state table */
174 const tAVCT_LCB_ST_TBL avct_lcb_st_tbl[] = {avct_lcb_st_idle, avct_lcb_st_opening, avct_lcb_st_open,
175                                             avct_lcb_st_closing};
176 
177 /*******************************************************************************
178  *
179  * Function         avct_lcb_event
180  *
181  * Description      State machine event handling function for lcb
182  *
183  *
184  * Returns          Nothing.
185  *
186  ******************************************************************************/
avct_lcb_event(tAVCT_LCB * p_lcb,uint8_t event,tAVCT_LCB_EVT * p_data)187 void avct_lcb_event(tAVCT_LCB* p_lcb, uint8_t event, tAVCT_LCB_EVT* p_data) {
188   tAVCT_LCB_ST_TBL state_table;
189   uint8_t action;
190   int i;
191 
192   log::verbose("LCB lcb_allocated={} event={} state={}", p_lcb->allocated, avct_lcb_evt_str[event],
193                avct_lcb_st_str[p_lcb->state]);
194 
195   /* look up the state table for the current state */
196   state_table = avct_lcb_st_tbl[p_lcb->state];
197 
198   if (p_lcb->state == AVCT_LCB_IDLE_ST && event == AVCT_LCB_LL_OPEN_EVT) {
199     DEVICE_IOT_CONFIG_ADDR_INT_ADD_ONE(p_lcb->peer_addr, IOT_CONF_KEY_AVRCP_CONN_COUNT);
200   }
201 
202   /* set next state */
203   p_lcb->state = state_table[event][AVCT_LCB_NEXT_STATE];
204 
205   /* execute action functions */
206   for (i = 0; i < AVCT_LCB_ACTIONS; i++) {
207     action = state_table[event][i];
208     if (action != AVCT_LCB_IGNORE) {
209       (*avct_lcb_action[action])(p_lcb, p_data);
210     } else {
211       break;
212     }
213   }
214 }
215 
216 /*******************************************************************************
217  *
218  * Function         avct_bcb_event
219  *
220  * Description      State machine event handling function for lcb
221  *
222  *
223  * Returns          Nothing.
224  *
225  ******************************************************************************/
avct_bcb_event(tAVCT_BCB * p_bcb,uint8_t event,tAVCT_LCB_EVT * p_data)226 void avct_bcb_event(tAVCT_BCB* p_bcb, uint8_t event, tAVCT_LCB_EVT* p_data) {
227   log::info("BCB bcb_allocated={} event={} state={}", p_bcb->allocated, avct_lcb_evt_str[event],
228             avct_lcb_st_str[p_bcb->state]);
229 
230   /* look up the state table for the current state */
231   tAVCT_LCB_ST_TBL state_table = avct_lcb_st_tbl[p_bcb->state];
232 
233   /* set next state */
234   p_bcb->state = state_table[event][AVCT_LCB_NEXT_STATE];
235 
236   /* execute action functions */
237   for (int i = 0; i < AVCT_LCB_ACTIONS; i++) {
238     uint8_t action = state_table[event][i];
239     if (action != AVCT_LCB_IGNORE) {
240       (*avct_bcb_action[action])(p_bcb, p_data);
241     } else {
242       break;
243     }
244   }
245 }
246 
247 /*******************************************************************************
248  *
249  * Function         avct_lcb_by_bd
250  *
251  * Description      This lookup function finds the lcb for a BD address.
252  *
253  *
254  * Returns          pointer to the lcb, or NULL if none found.
255  *
256  ******************************************************************************/
avct_lcb_by_bd(const RawAddress & bd_addr)257 tAVCT_LCB* avct_lcb_by_bd(const RawAddress& bd_addr) {
258   tAVCT_LCB* p_lcb = &avct_cb.lcb[0];
259   int i;
260 
261   for (i = 0; i < AVCT_NUM_LINKS; i++, p_lcb++) {
262     /* if allocated lcb has matching lcb */
263     if (p_lcb->allocated && p_lcb->peer_addr == bd_addr) {
264       break;
265     }
266   }
267 
268   if (i == AVCT_NUM_LINKS) {
269     /* if no lcb found */
270     p_lcb = NULL;
271 
272     log::verbose("No lcb for addr:{}", bd_addr);
273   }
274   return p_lcb;
275 }
276 
277 /*******************************************************************************
278  *
279  * Function         avct_lcb_alloc
280  *
281  * Description      Allocate a link control block.
282  *
283  *
284  * Returns          pointer to the lcb, or NULL if none could be allocated.
285  *
286  ******************************************************************************/
avct_lcb_alloc(const RawAddress & bd_addr)287 tAVCT_LCB* avct_lcb_alloc(const RawAddress& bd_addr) {
288   tAVCT_LCB* p_lcb = &avct_cb.lcb[0];
289   int i;
290 
291   for (i = 0; i < AVCT_NUM_LINKS; i++, p_lcb++) {
292     if (!p_lcb->allocated) {
293       p_lcb->allocated = (uint8_t)(i + 1);
294       p_lcb->peer_addr = bd_addr;
295       log::verbose("lcb_allocated:{}", p_lcb->allocated);
296       p_lcb->tx_q = fixed_queue_new(SIZE_MAX);
297       p_lcb->peer_mtu = L2CAP_LE_MIN_MTU;
298       break;
299     }
300   }
301 
302   if (i == AVCT_NUM_LINKS) {
303     /* out of lcbs */
304     p_lcb = NULL;
305     log::warn("Out of lcbs");
306   }
307   return p_lcb;
308 }
309 
310 /*******************************************************************************
311  *
312  * Function         avct_lcb_dealloc
313  *
314  * Description      Deallocate a link control block.
315  *
316  *
317  * Returns          void.
318  *
319  ******************************************************************************/
avct_lcb_dealloc(tAVCT_LCB * p_lcb,tAVCT_LCB_EVT *)320 void avct_lcb_dealloc(tAVCT_LCB* p_lcb, tAVCT_LCB_EVT* /* p_data */) {
321   log::verbose("lcb_allocated:{}", p_lcb->allocated);
322 
323   // Check if the LCB is still referenced
324 
325   tAVCT_CCB* p_ccb = &avct_cb.ccb[0];
326   for (size_t i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
327     if (p_ccb->allocated && p_ccb->p_lcb == p_lcb) {
328       log::verbose("LCB in use; lcb index:{}", i);
329       return;
330     }
331   }
332 
333   // If not, de-allocate now...
334 
335   log::verbose("Freeing LCB");
336   osi_free_and_reset((void**)&(p_lcb->p_rx_msg));
337   fixed_queue_free(p_lcb->tx_q, NULL);
338   memset(p_lcb, 0, sizeof(tAVCT_LCB));
339 }
340 
341 /*******************************************************************************
342  *
343  * Function         avct_lcb_by_lcid
344  *
345  * Description      Find the LCB associated with the L2CAP LCID
346  *
347  *
348  * Returns          pointer to the lcb, or NULL if none found.
349  *
350  ******************************************************************************/
avct_lcb_by_lcid(uint16_t lcid)351 tAVCT_LCB* avct_lcb_by_lcid(uint16_t lcid) {
352   tAVCT_LCB* p_lcb = &avct_cb.lcb[0];
353   int i;
354 
355   for (i = 0; i < AVCT_NUM_LINKS; i++, p_lcb++) {
356     if (p_lcb->allocated && ((p_lcb->ch_lcid == lcid) || (p_lcb->conflict_lcid == lcid))) {
357       break;
358     }
359   }
360 
361   if (i == AVCT_NUM_LINKS) {
362     /* out of lcbs */
363     p_lcb = nullptr;
364     log::warn("No lcb for lcid 0x{:04x}", lcid);
365   }
366 
367   return p_lcb;
368 }
369 
370 /*******************************************************************************
371  *
372  * Function         avct_lcb_has_pid
373  *
374  * Description      See if any ccbs on this lcb have a particular pid.
375  *
376  *
377  * Returns          Pointer to CCB if PID found, NULL otherwise.
378  *
379  ******************************************************************************/
avct_lcb_has_pid(tAVCT_LCB * p_lcb,uint16_t pid)380 tAVCT_CCB* avct_lcb_has_pid(tAVCT_LCB* p_lcb, uint16_t pid) {
381   tAVCT_CCB* p_ccb = &avct_cb.ccb[0];
382   int i;
383 
384   for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
385     if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb) && (p_ccb->cc.pid == pid)) {
386       return p_ccb;
387     }
388   }
389   return NULL;
390 }
391 
392 /*******************************************************************************
393  *
394  * Function         avct_lcb_last_ccb
395  *
396  * Description      See if given ccb is only one on the lcb.
397  *
398  *
399  * Returns          true if ccb is last, false otherwise.
400  *
401  ******************************************************************************/
avct_lcb_last_ccb(tAVCT_LCB * p_lcb,tAVCT_CCB * p_ccb_last)402 bool avct_lcb_last_ccb(tAVCT_LCB* p_lcb, tAVCT_CCB* p_ccb_last) {
403   tAVCT_CCB* p_ccb = &avct_cb.ccb[0];
404   int i;
405 
406   log::warn("avct_lcb_last_ccb");
407   for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
408     log::warn("index:{} allocated:{}, ", i, p_ccb->allocated);
409     if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb) && (p_ccb != p_ccb_last)) {
410       return false;
411     }
412   }
413   return true;
414 }
415