• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
25 
26 #include "bt_target.h"  // Must be first to define build configuration
27 
28 #include "bta/pan/bta_pan_int.h"
29 #include "osi/include/osi.h"  // UNUSED_ATTR
30 #include "stack/include/bt_hdr.h"
31 
32 /*****************************************************************************
33  * Constants and types
34  ****************************************************************************/
35 
36 /* state machine action enumeration list */
37 enum {
38   BTA_PAN_API_CLOSE,
39   BTA_PAN_TX_PATH,
40   BTA_PAN_RX_PATH,
41   BTA_PAN_TX_FLOW,
42   BTA_PAN_WRITE_BUF,
43   BTA_PAN_CONN_OPEN,
44   BTA_PAN_CONN_CLOSE,
45   BTA_PAN_FREE_BUF,
46   BTA_PAN_IGNORE,
47   BTA_PAN_MAX_ACTIONS
48 };
49 
50 /* type for action functions */
51 typedef void (*tBTA_PAN_ACTION)(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
52 
53 /* action function list */
54 const tBTA_PAN_ACTION bta_pan_action[] = {
55     bta_pan_api_close, bta_pan_tx_path,   bta_pan_rx_path,    bta_pan_tx_flow,
56     bta_pan_write_buf, bta_pan_conn_open, bta_pan_conn_close, bta_pan_free_buf,
57 
58 };
59 
60 /* state table information */
61 #define BTA_PAN_ACTIONS 1    /* number of actions */
62 #define BTA_PAN_NEXT_STATE 1 /* position of next state */
63 #define BTA_PAN_NUM_COLS 2   /* number of columns in state tables */
64 
65 /* state table for listen state */
66 const uint8_t bta_pan_st_idle[][BTA_PAN_NUM_COLS] = {
67     /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_IDLE_ST},
68     /* CI_TX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
69     /* CI_RX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
70     /* CI_TX_FLOW */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
71     /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
72     /* CI_RX_WRITEBUF */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
73     /* PAN_CONN_OPEN */ {BTA_PAN_CONN_OPEN, BTA_PAN_OPEN_ST},
74     /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_OPEN, BTA_PAN_IDLE_ST},
75     /* FLOW_ENABLE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
76     /* BNEP_DATA */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST}
77 
78 };
79 
80 /* state table for open state */
81 const uint8_t bta_pan_st_open[][BTA_PAN_NUM_COLS] = {
82     /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_OPEN_ST},
83     /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST},
84     /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
85     /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_OPEN_ST},
86     /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
87     /* CI_RX_WRITEBUF */ {BTA_PAN_WRITE_BUF, BTA_PAN_OPEN_ST},
88     /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
89     /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
90     /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
91     /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST}};
92 
93 /* state table for closing state */
94 const uint8_t bta_pan_st_closing[][BTA_PAN_NUM_COLS] = {
95     /* API_CLOSE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
96     /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST},
97     /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
98     /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_CLOSING_ST},
99     /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
100     /* CI_RX_WRITEBUF */ {BTA_PAN_FREE_BUF, BTA_PAN_CLOSING_ST},
101     /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
102     /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
103     /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
104     /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST}};
105 
106 /* type for state table */
107 typedef const uint8_t (*tBTA_PAN_ST_TBL)[BTA_PAN_NUM_COLS];
108 
109 /* state table */
110 const tBTA_PAN_ST_TBL bta_pan_st_tbl[] = {bta_pan_st_idle, bta_pan_st_open,
111                                           bta_pan_st_closing};
112 
113 /*****************************************************************************
114  * Global data
115  ****************************************************************************/
116 
117 /* PAN control block */
118 tBTA_PAN_CB bta_pan_cb;
119 
120 /*******************************************************************************
121  *
122  * Function         bta_pan_scb_alloc
123  *
124  * Description      Allocate a PAN server control block.
125  *
126  *
127  * Returns          pointer to the scb, or NULL if none could be allocated.
128  *
129  ******************************************************************************/
bta_pan_scb_alloc(void)130 tBTA_PAN_SCB* bta_pan_scb_alloc(void) {
131   tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
132   int i;
133 
134   for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
135     if (!p_scb->in_use) {
136       p_scb->in_use = true;
137       APPL_TRACE_DEBUG("bta_pan_scb_alloc %d", i);
138       break;
139     }
140   }
141 
142   if (i == BTA_PAN_NUM_CONN) {
143     /* out of scbs */
144     p_scb = NULL;
145     APPL_TRACE_WARNING("Out of scbs");
146   }
147   return p_scb;
148 }
149 
150 /*******************************************************************************
151  *
152  * Function         bta_pan_sm_execute
153  *
154  * Description      State machine event handling function for PAN
155  *
156  *
157  * Returns          void
158  *
159  ******************************************************************************/
bta_pan_sm_execute(tBTA_PAN_SCB * p_scb,uint16_t event,tBTA_PAN_DATA * p_data)160 void bta_pan_sm_execute(tBTA_PAN_SCB* p_scb, uint16_t event,
161                         tBTA_PAN_DATA* p_data) {
162   tBTA_PAN_ST_TBL state_table;
163   uint8_t action;
164   int i;
165 
166   APPL_TRACE_EVENT("PAN scb=%d event=0x%x state=%d", bta_pan_scb_to_idx(p_scb),
167                    event, p_scb->state);
168 
169   /* look up the state table for the current state */
170   state_table = bta_pan_st_tbl[p_scb->state];
171 
172   event &= 0x00FF;
173 
174   /* set next state */
175   p_scb->state = state_table[event][BTA_PAN_NEXT_STATE];
176 
177   /* execute action functions */
178   for (i = 0; i < BTA_PAN_ACTIONS; i++) {
179     action = state_table[event][i];
180     CHECK(action < BTA_PAN_MAX_ACTIONS);
181     if (action == BTA_PAN_IGNORE) continue;
182     (*bta_pan_action[action])(p_scb, p_data);
183   }
184 }
185 
186 /*******************************************************************************
187  *
188  * Function         bta_pan_api_enable
189  *
190  * Description      Handle an API enable event.
191  *
192  *
193  * Returns          void
194  *
195  ******************************************************************************/
bta_pan_api_enable(tBTA_PAN_DATA * p_data)196 void bta_pan_api_enable(tBTA_PAN_DATA* p_data) {
197   /* initialize control block */
198   memset(&bta_pan_cb, 0, sizeof(bta_pan_cb));
199 
200   /* store callback function */
201   bta_pan_cb.p_cback = p_data->api_enable.p_cback;
202   bta_pan_enable(p_data);
203 }
204 
205 /*******************************************************************************
206  *
207  * Function         bta_pan_api_disable
208  *
209  * Description      Handle an API disable event.
210  *
211  *
212  * Returns          void
213  *
214  ******************************************************************************/
bta_pan_api_disable(UNUSED_ATTR tBTA_PAN_DATA * p_data)215 void bta_pan_api_disable(UNUSED_ATTR tBTA_PAN_DATA* p_data) {
216   bta_pan_disable();
217 }
218 
219 /*******************************************************************************
220  *
221  * Function         bta_pan_api_open
222  *
223  * Description      Handle an API listen event.
224  *
225  *
226  * Returns          void
227  *
228  ******************************************************************************/
bta_pan_api_open(tBTA_PAN_DATA * p_data)229 void bta_pan_api_open(tBTA_PAN_DATA* p_data) {
230   tBTA_PAN_SCB* p_scb;
231   tBTA_PAN bta_pan;
232 
233   /* allocate an scb */
234   p_scb = bta_pan_scb_alloc();
235   if (p_scb != NULL) {
236     bta_pan_open(p_scb, p_data);
237   } else {
238     bta_pan.open.bd_addr = p_data->api_open.bd_addr;
239     bta_pan.open.status = BTA_PAN_FAIL;
240     bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, &bta_pan);
241   }
242 }
243 /*******************************************************************************
244  *
245  * Function         bta_pan_scb_dealloc
246  *
247  * Description      Deallocate a link control block.
248  *
249  *
250  * Returns          void
251  *
252  ******************************************************************************/
bta_pan_scb_dealloc(tBTA_PAN_SCB * p_scb)253 void bta_pan_scb_dealloc(tBTA_PAN_SCB* p_scb) {
254   APPL_TRACE_DEBUG("bta_pan_scb_dealloc %d", bta_pan_scb_to_idx(p_scb));
255   fixed_queue_free(p_scb->data_queue, NULL);
256   memset(p_scb, 0, sizeof(tBTA_PAN_SCB));
257 }
258 
259 /*******************************************************************************
260  *
261  * Function         bta_pan_scb_to_idx
262  *
263  * Description      Given a pointer to an scb, return its index.
264  *
265  *
266  * Returns          Index of scb.
267  *
268  ******************************************************************************/
bta_pan_scb_to_idx(tBTA_PAN_SCB * p_scb)269 uint8_t bta_pan_scb_to_idx(tBTA_PAN_SCB* p_scb) {
270   return ((uint8_t)(p_scb - bta_pan_cb.scb)) + 1;
271 }
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   APPL_TRACE_WARNING("No scb for handle %d", 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(BT_HDR_RIGID * p_msg)309 bool bta_pan_hdl_event(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