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