1 /******************************************************************************
2  *
3  *  Copyright 2004-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 PAN main functions and state machine.
22  *
23  ******************************************************************************/
24 #include <bluetooth/log.h>
25 
26 #include <cstdint>
27 
28 #include "bta/pan/bta_pan_int.h"
29 #include "internal_include/bt_target.h"
30 #include "stack/include/bt_hdr.h"
31 
32 using namespace bluetooth;
33 
34 /*****************************************************************************
35  * Constants and types
36  ****************************************************************************/
37 
38 /* state machine action enumeration list */
39 enum {
40   BTA_PAN_API_CLOSE,
41   BTA_PAN_TX_PATH,
42   BTA_PAN_RX_PATH,
43   BTA_PAN_TX_FLOW,
44   BTA_PAN_WRITE_BUF,
45   BTA_PAN_CONN_OPEN,
46   BTA_PAN_CONN_CLOSE,
47   BTA_PAN_FREE_BUF,
48   BTA_PAN_IGNORE,
49   BTA_PAN_MAX_ACTIONS
50 };
51 
52 /* type for action functions */
53 typedef void (*tBTA_PAN_ACTION)(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
54 
55 /* action function list */
56 const tBTA_PAN_ACTION bta_pan_action[] = {
57     bta_pan_api_close, bta_pan_tx_path,   bta_pan_rx_path,    bta_pan_tx_flow,
58     bta_pan_write_buf, bta_pan_conn_open, bta_pan_conn_close, bta_pan_free_buf,
59 
60 };
61 
62 /* state table information */
63 #define BTA_PAN_ACTIONS 1    /* number of actions */
64 #define BTA_PAN_NEXT_STATE 1 /* position of next state */
65 #define BTA_PAN_NUM_COLS 2   /* number of columns in state tables */
66 
67 /* state table for listen state */
68 const uint8_t bta_pan_st_idle[][BTA_PAN_NUM_COLS] = {
69     /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_IDLE_ST},
70     /* CI_TX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
71     /* CI_RX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
72     /* CI_TX_FLOW */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
73     /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
74     /* CI_RX_WRITEBUF */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
75     /* PAN_CONN_OPEN */ {BTA_PAN_CONN_OPEN, BTA_PAN_OPEN_ST},
76     /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_OPEN, BTA_PAN_IDLE_ST},
77     /* FLOW_ENABLE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
78     /* BNEP_DATA */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST}
79 
80 };
81 
82 /* state table for open state */
83 const uint8_t bta_pan_st_open[][BTA_PAN_NUM_COLS] = {
84     /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_OPEN_ST},
85     /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST},
86     /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
87     /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_OPEN_ST},
88     /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
89     /* CI_RX_WRITEBUF */ {BTA_PAN_WRITE_BUF, BTA_PAN_OPEN_ST},
90     /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
91     /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
92     /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
93     /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST}};
94 
95 /* state table for closing state */
96 const uint8_t bta_pan_st_closing[][BTA_PAN_NUM_COLS] = {
97     /* API_CLOSE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
98     /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST},
99     /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
100     /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_CLOSING_ST},
101     /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
102     /* CI_RX_WRITEBUF */ {BTA_PAN_FREE_BUF, BTA_PAN_CLOSING_ST},
103     /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
104     /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
105     /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
106     /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST}};
107 
108 /* type for state table */
109 typedef const uint8_t (*tBTA_PAN_ST_TBL)[BTA_PAN_NUM_COLS];
110 
111 /* state table */
112 const tBTA_PAN_ST_TBL bta_pan_st_tbl[] = {bta_pan_st_idle, bta_pan_st_open,
113                                           bta_pan_st_closing};
114 
115 /*****************************************************************************
116  * Global data
117  ****************************************************************************/
118 
119 /* PAN control block */
120 tBTA_PAN_CB bta_pan_cb;
121 
122 /*******************************************************************************
123  *
124  * Function         bta_pan_scb_alloc
125  *
126  * Description      Allocate a PAN server control block.
127  *
128  *
129  * Returns          pointer to the scb, or NULL if none could be allocated.
130  *
131  ******************************************************************************/
bta_pan_scb_alloc(void)132 tBTA_PAN_SCB* bta_pan_scb_alloc(void) {
133   tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
134   int i;
135 
136   for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
137     if (!p_scb->in_use) {
138       p_scb->in_use = true;
139       log::verbose("bta_pan_scb_alloc {}", i);
140       break;
141     }
142   }
143 
144   if (i == BTA_PAN_NUM_CONN) {
145     /* out of scbs */
146     p_scb = NULL;
147     log::warn("Out of scbs");
148   }
149   return p_scb;
150 }
151 
152 /*******************************************************************************
153  *
154  * Function         bta_pan_sm_execute
155  *
156  * Description      State machine event handling function for PAN
157  *
158  *
159  * Returns          void
160  *
161  ******************************************************************************/
bta_pan_sm_execute(tBTA_PAN_SCB * p_scb,uint16_t event,tBTA_PAN_DATA * p_data)162 void bta_pan_sm_execute(tBTA_PAN_SCB* p_scb, uint16_t event,
163                         tBTA_PAN_DATA* p_data) {
164   tBTA_PAN_ST_TBL state_table;
165   uint8_t action;
166   int i;
167 
168   log::verbose("PAN scb={} event=0x{:x} state={}", bta_pan_scb_to_idx(p_scb),
169                event, p_scb->state);
170 
171   /* look up the state table for the current state */
172   state_table = bta_pan_st_tbl[p_scb->state];
173 
174   event &= 0x00FF;
175 
176   /* set next state */
177   p_scb->state = state_table[event][BTA_PAN_NEXT_STATE];
178 
179   /* execute action functions */
180   for (i = 0; i < BTA_PAN_ACTIONS; i++) {
181     action = state_table[event][i];
182     log::assert_that(action < BTA_PAN_MAX_ACTIONS,
183                      "assert failed: action < BTA_PAN_MAX_ACTIONS");
184     if (action == BTA_PAN_IGNORE) continue;
185     (*bta_pan_action[action])(p_scb, p_data);
186   }
187 }
188 
189 /*******************************************************************************
190  *
191  * Function         bta_pan_api_enable
192  *
193  * Description      Handle an API enable event.
194  *
195  *
196  * Returns          void
197  *
198  ******************************************************************************/
bta_pan_api_enable(tBTA_PAN_DATA * p_data)199 void bta_pan_api_enable(tBTA_PAN_DATA* p_data) {
200   /* initialize control block */
201   memset(&bta_pan_cb, 0, sizeof(bta_pan_cb));
202 
203   /* store callback function */
204   bta_pan_cb.p_cback = p_data->api_enable.p_cback;
205   bta_pan_enable(p_data);
206 }
207 
208 /*******************************************************************************
209  *
210  * Function         bta_pan_api_disable
211  *
212  * Description      Handle an API disable event.
213  *
214  *
215  * Returns          void
216  *
217  ******************************************************************************/
bta_pan_api_disable(tBTA_PAN_DATA *)218 void bta_pan_api_disable(tBTA_PAN_DATA* /* p_data */) { bta_pan_disable(); }
219 
220 /*******************************************************************************
221  *
222  * Function         bta_pan_api_open
223  *
224  * Description      Handle an API listen event.
225  *
226  *
227  * Returns          void
228  *
229  ******************************************************************************/
bta_pan_api_open(tBTA_PAN_DATA * p_data)230 void bta_pan_api_open(tBTA_PAN_DATA* p_data) {
231   tBTA_PAN_SCB* p_scb;
232   tBTA_PAN bta_pan;
233 
234   /* allocate an scb */
235   p_scb = bta_pan_scb_alloc();
236   if (p_scb != NULL) {
237     bta_pan_open(p_scb, p_data);
238   } else {
239     bta_pan.open.bd_addr = p_data->api_open.bd_addr;
240     bta_pan.open.status = BTA_PAN_FAIL;
241     bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, &bta_pan);
242   }
243 }
244 /*******************************************************************************
245  *
246  * Function         bta_pan_scb_dealloc
247  *
248  * Description      Deallocate a link control block.
249  *
250  *
251  * Returns          void
252  *
253  ******************************************************************************/
bta_pan_scb_dealloc(tBTA_PAN_SCB * p_scb)254 void bta_pan_scb_dealloc(tBTA_PAN_SCB* p_scb) {
255   log::verbose("bta_pan_scb_dealloc {}", bta_pan_scb_to_idx(p_scb));
256   fixed_queue_free(p_scb->data_queue, NULL);
257   memset(p_scb, 0, sizeof(tBTA_PAN_SCB));
258 }
259 
260 /*******************************************************************************
261  *
262  * Function         bta_pan_scb_to_idx
263  *
264  * Description      Given a pointer to an scb, return its index.
265  *
266  *
267  * Returns          Index of scb.
268  *
269  ******************************************************************************/
bta_pan_scb_to_idx(tBTA_PAN_SCB * p_scb)270 uint8_t bta_pan_scb_to_idx(tBTA_PAN_SCB* p_scb) {
271   return ((uint8_t)(p_scb - bta_pan_cb.scb)) + 1;
272 }
273 
274 /*******************************************************************************
275  *
276  * Function         bta_pan_scb_by_handle
277  *
278  * Description      Find scb associated with handle.
279  *
280  *
281  * Returns          Pointer to scb or NULL if not found.
282  *
283  ******************************************************************************/
bta_pan_scb_by_handle(uint16_t handle)284 tBTA_PAN_SCB* bta_pan_scb_by_handle(uint16_t handle) {
285   tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
286   uint8_t i;
287 
288   for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
289     if (p_scb->handle == handle) {
290       return p_scb;
291       ;
292     }
293   }
294 
295   log::warn("No scb for handle {}", handle);
296 
297   return NULL;
298 }
299 
300 /*******************************************************************************
301  *
302  * Function         bta_pan_hdl_event
303  *
304  * Description      Data gateway main event handling function.
305  *
306  *
307  * Returns          void
308  *
309  ******************************************************************************/
bta_pan_hdl_event(const BT_HDR_RIGID * p_msg)310 bool bta_pan_hdl_event(const BT_HDR_RIGID* p_msg) {
311   tBTA_PAN_SCB* p_scb;
312   bool freebuf = true;
313 
314   switch (p_msg->event) {
315     /* handle enable event */
316     case BTA_PAN_API_ENABLE_EVT:
317       bta_pan_api_enable((tBTA_PAN_DATA*)p_msg);
318       break;
319 
320     /* handle disable event */
321     case BTA_PAN_API_DISABLE_EVT:
322       bta_pan_api_disable((tBTA_PAN_DATA*)p_msg);
323       break;
324 
325     /* handle set role event */
326     case BTA_PAN_API_SET_ROLE_EVT:
327       bta_pan_set_role((tBTA_PAN_DATA*)p_msg);
328       break;
329 
330     /* handle open event */
331     case BTA_PAN_API_OPEN_EVT:
332       bta_pan_api_open((tBTA_PAN_DATA*)p_msg);
333       break;
334 
335     /* events that require buffer not be released */
336     case BTA_PAN_CI_RX_WRITEBUF_EVT:
337       freebuf = false;
338       p_scb = bta_pan_scb_by_handle(p_msg->layer_specific);
339       if (p_scb != NULL) {
340         bta_pan_sm_execute(p_scb, p_msg->event, (tBTA_PAN_DATA*)p_msg);
341       }
342       break;
343 
344     /* all other events */
345     default:
346       p_scb = bta_pan_scb_by_handle(p_msg->layer_specific);
347       if (p_scb != NULL) {
348         bta_pan_sm_execute(p_scb, p_msg->event, (tBTA_PAN_DATA*)p_msg);
349       }
350       break;
351   }
352   return freebuf;
353 }
354