1 /******************************************************************************
3 * Copyright 2004-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the PAN main functions and state machine.
23 ******************************************************************************/
26 #include "bt_target.h" // Must be first to define build configuration
28 #if (BTA_PAN_INCLUDED == TRUE)
30 #include "bta/pan/bta_pan_int.h"
31 #include "osi/include/osi.h" // UNUSED_ATTR
33 /*****************************************************************************
35 ****************************************************************************/
37 /* state machine action enumeration list */
51 /* type for action functions */
52 typedef void (*tBTA_PAN_ACTION)(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
54 /* action function list */
55 const tBTA_PAN_ACTION bta_pan_action[] = {
56 bta_pan_api_close, bta_pan_tx_path, bta_pan_rx_path, bta_pan_tx_flow,
57 bta_pan_write_buf, bta_pan_conn_open, bta_pan_conn_close, bta_pan_free_buf,
61 /* state table information */
62 #define BTA_PAN_ACTIONS 1 /* number of actions */
63 #define BTA_PAN_NEXT_STATE 1 /* position of next state */
64 #define BTA_PAN_NUM_COLS 2 /* number of columns in state tables */
66 /* state table for listen state */
67 const uint8_t bta_pan_st_idle[][BTA_PAN_NUM_COLS] = {
68 /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_IDLE_ST},
69 /* CI_TX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
70 /* CI_RX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
71 /* CI_TX_FLOW */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
72 /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
73 /* CI_RX_WRITEBUF */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
74 /* PAN_CONN_OPEN */ {BTA_PAN_CONN_OPEN, BTA_PAN_OPEN_ST},
75 /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_OPEN, BTA_PAN_IDLE_ST},
76 /* FLOW_ENABLE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
77 /* BNEP_DATA */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST}
81 /* state table for open state */
82 const uint8_t bta_pan_st_open[][BTA_PAN_NUM_COLS] = {
83 /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_OPEN_ST},
84 /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST},
85 /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
86 /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_OPEN_ST},
87 /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
88 /* CI_RX_WRITEBUF */ {BTA_PAN_WRITE_BUF, BTA_PAN_OPEN_ST},
89 /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
90 /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
91 /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
92 /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST}};
94 /* state table for closing state */
95 const uint8_t bta_pan_st_closing[][BTA_PAN_NUM_COLS] = {
96 /* API_CLOSE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
97 /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST},
98 /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
99 /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_CLOSING_ST},
100 /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
101 /* CI_RX_WRITEBUF */ {BTA_PAN_FREE_BUF, BTA_PAN_CLOSING_ST},
102 /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
103 /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
104 /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
105 /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST}};
107 /* type for state table */
108 typedef const uint8_t (*tBTA_PAN_ST_TBL)[BTA_PAN_NUM_COLS];
111 const tBTA_PAN_ST_TBL bta_pan_st_tbl[] = {bta_pan_st_idle, bta_pan_st_open,
114 /*****************************************************************************
116 ****************************************************************************/
118 /* PAN control block */
119 tBTA_PAN_CB bta_pan_cb;
121 /*******************************************************************************
123 * Function bta_pan_scb_alloc
125 * Description Allocate a PAN server control block.
128 * Returns pointer to the scb, or NULL if none could be allocated.
130 ******************************************************************************/
131 tBTA_PAN_SCB* bta_pan_scb_alloc(void) {
132 tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
135 for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
136 if (!p_scb->in_use) {
137 p_scb->in_use = true;
138 APPL_TRACE_DEBUG("bta_pan_scb_alloc %d", i);
143 if (i == BTA_PAN_NUM_CONN) {
146 APPL_TRACE_WARNING("Out of scbs");
151 /*******************************************************************************
153 * Function bta_pan_sm_execute
155 * Description State machine event handling function for PAN
160 ******************************************************************************/
161 static void bta_pan_sm_execute(tBTA_PAN_SCB* p_scb, uint16_t event,
162 tBTA_PAN_DATA* p_data) {
163 tBTA_PAN_ST_TBL state_table;
167 APPL_TRACE_EVENT("PAN scb=%d event=0x%x state=%d", bta_pan_scb_to_idx(p_scb),
168 event, p_scb->state);
170 /* look up the state table for the current state */
171 state_table = bta_pan_st_tbl[p_scb->state];
176 p_scb->state = state_table[event][BTA_PAN_NEXT_STATE];
178 /* execute action functions */
179 for (i = 0; i < BTA_PAN_ACTIONS; i++) {
180 action = state_table[event][i];
181 CHECK(action < BTA_PAN_MAX_ACTIONS);
182 if (action == BTA_PAN_IGNORE) continue;
183 (*bta_pan_action[action])(p_scb, p_data);
187 /*******************************************************************************
189 * Function bta_pan_api_enable
191 * Description Handle an API enable event.
196 ******************************************************************************/
197 static void bta_pan_api_enable(tBTA_PAN_DATA* p_data) {
198 /* initialize control block */
199 memset(&bta_pan_cb, 0, sizeof(bta_pan_cb));
201 /* store callback function */
202 bta_pan_cb.p_cback = p_data->api_enable.p_cback;
203 bta_pan_enable(p_data);
206 /*******************************************************************************
208 * Function bta_pan_api_disable
210 * Description Handle an API disable event.
215 ******************************************************************************/
216 static void bta_pan_api_disable(UNUSED_ATTR tBTA_PAN_DATA* p_data) {
220 /*******************************************************************************
222 * Function bta_pan_api_open
224 * Description Handle an API listen event.
229 ******************************************************************************/
230 static void bta_pan_api_open(tBTA_PAN_DATA* p_data) {
234 /* allocate an scb */
235 p_scb = bta_pan_scb_alloc();
237 bta_pan_open(p_scb, p_data);
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);
244 /*******************************************************************************
246 * Function bta_pan_scb_dealloc
248 * Description Deallocate a link control block.
253 ******************************************************************************/
254 void bta_pan_scb_dealloc(tBTA_PAN_SCB* p_scb) {
255 APPL_TRACE_DEBUG("bta_pan_scb_dealloc %d", 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));
260 /*******************************************************************************
262 * Function bta_pan_scb_to_idx
264 * Description Given a pointer to an scb, return its index.
267 * Returns Index of scb.
269 ******************************************************************************/
270 uint8_t bta_pan_scb_to_idx(tBTA_PAN_SCB* p_scb) {
271 return ((uint8_t)(p_scb - bta_pan_cb.scb)) + 1;
274 /*******************************************************************************
276 * Function bta_pan_scb_by_handle
278 * Description Find scb associated with handle.
281 * Returns Pointer to scb or NULL if not found.
283 ******************************************************************************/
284 tBTA_PAN_SCB* bta_pan_scb_by_handle(uint16_t handle) {
285 tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
288 for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
289 if (p_scb->handle == handle) {
295 APPL_TRACE_WARNING("No scb for handle %d", handle);
300 /*******************************************************************************
302 * Function bta_pan_hdl_event
304 * Description Data gateway main event handling function.
309 ******************************************************************************/
310 bool bta_pan_hdl_event(BT_HDR_RIGID* p_msg) {
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);
320 /* handle disable event */
321 case BTA_PAN_API_DISABLE_EVT:
322 bta_pan_api_disable((tBTA_PAN_DATA*)p_msg);
325 /* handle set role event */
326 case BTA_PAN_API_SET_ROLE_EVT:
327 bta_pan_set_role((tBTA_PAN_DATA*)p_msg);
330 /* handle open event */
331 case BTA_PAN_API_OPEN_EVT:
332 bta_pan_api_open((tBTA_PAN_DATA*)p_msg);
335 /* events that require buffer not be released */
336 case BTA_PAN_CI_RX_WRITEBUF_EVT:
338 p_scb = bta_pan_scb_by_handle(p_msg->layer_specific);
340 bta_pan_sm_execute(p_scb, p_msg->event, (tBTA_PAN_DATA*)p_msg);
344 /* all other events */
346 p_scb = bta_pan_scb_by_handle(p_msg->layer_specific);
348 bta_pan_sm_execute(p_scb, p_msg->event, (tBTA_PAN_DATA*)p_msg);
354 #endif /* BTA_PAN_INCLUDED */