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 #if (BTA_PAN_INCLUDED == TRUE)
29
30 #include "bta/pan/bta_pan_int.h"
31 #include "osi/include/osi.h" // UNUSED_ATTR
32
33 /*****************************************************************************
34 * Constants and types
35 ****************************************************************************/
36
37 /* state machine action enumeration list */
38 enum {
39 BTA_PAN_API_CLOSE,
40 BTA_PAN_TX_PATH,
41 BTA_PAN_RX_PATH,
42 BTA_PAN_TX_FLOW,
43 BTA_PAN_WRITE_BUF,
44 BTA_PAN_CONN_OPEN,
45 BTA_PAN_CONN_CLOSE,
46 BTA_PAN_FREE_BUF,
47 BTA_PAN_IGNORE,
48 BTA_PAN_MAX_ACTIONS
49 };
50
51 /* type for action functions */
52 typedef void (*tBTA_PAN_ACTION)(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
53
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,
58
59 };
60
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 */
65
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}
78
79 };
80
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}};
93
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}};
106
107 /* type for state table */
108 typedef const uint8_t (*tBTA_PAN_ST_TBL)[BTA_PAN_NUM_COLS];
109
110 /* state table */
111 const tBTA_PAN_ST_TBL bta_pan_st_tbl[] = {bta_pan_st_idle, bta_pan_st_open,
112 bta_pan_st_closing};
113
114 /*****************************************************************************
115 * Global data
116 ****************************************************************************/
117
118 /* PAN control block */
119 tBTA_PAN_CB bta_pan_cb;
120
121 /*******************************************************************************
122 *
123 * Function bta_pan_scb_alloc
124 *
125 * Description Allocate a PAN server control block.
126 *
127 *
128 * Returns pointer to the scb, or NULL if none could be allocated.
129 *
130 ******************************************************************************/
bta_pan_scb_alloc(void)131 tBTA_PAN_SCB* bta_pan_scb_alloc(void) {
132 tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
133 int i;
134
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);
139 break;
140 }
141 }
142
143 if (i == BTA_PAN_NUM_CONN) {
144 /* out of scbs */
145 p_scb = NULL;
146 APPL_TRACE_WARNING("Out of scbs");
147 }
148 return p_scb;
149 }
150
151 /*******************************************************************************
152 *
153 * Function bta_pan_sm_execute
154 *
155 * Description State machine event handling function for PAN
156 *
157 *
158 * Returns void
159 *
160 ******************************************************************************/
bta_pan_sm_execute(tBTA_PAN_SCB * p_scb,uint16_t event,tBTA_PAN_DATA * p_data)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;
164 uint8_t action;
165 int i;
166
167 APPL_TRACE_EVENT("PAN scb=%d event=0x%x state=%d", bta_pan_scb_to_idx(p_scb),
168 event, p_scb->state);
169
170 /* look up the state table for the current state */
171 state_table = bta_pan_st_tbl[p_scb->state];
172
173 event &= 0x00FF;
174
175 /* set next state */
176 p_scb->state = state_table[event][BTA_PAN_NEXT_STATE];
177
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);
184 }
185 }
186
187 /*******************************************************************************
188 *
189 * Function bta_pan_api_enable
190 *
191 * Description Handle an API enable event.
192 *
193 *
194 * Returns void
195 *
196 ******************************************************************************/
bta_pan_api_enable(tBTA_PAN_DATA * p_data)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));
200
201 /* store callback function */
202 bta_pan_cb.p_cback = p_data->api_enable.p_cback;
203 bta_pan_enable(p_data);
204 }
205
206 /*******************************************************************************
207 *
208 * Function bta_pan_api_disable
209 *
210 * Description Handle an API disable event.
211 *
212 *
213 * Returns void
214 *
215 ******************************************************************************/
bta_pan_api_disable(UNUSED_ATTR tBTA_PAN_DATA * p_data)216 static void bta_pan_api_disable(UNUSED_ATTR tBTA_PAN_DATA* p_data) {
217 bta_pan_disable();
218 }
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 static 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 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));
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 APPL_TRACE_WARNING("No scb for handle %d", 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(BT_HDR_RIGID * p_msg)310 bool bta_pan_hdl_event(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 #endif /* BTA_PAN_INCLUDED */
355