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 action functions for the state machine.
23 ******************************************************************************/
25 #define LOG_TAG "bluetooth"
30 #include "bt_target.h" // Must be first to define build configuration
31 #if (PAN_INCLUDED == TRUE)
33 #include "bta/include/bta_pan_co.h"
34 #include "bta/pan/bta_pan_int.h"
35 #include "osi/include/allocator.h"
36 #include "osi/include/fixed_queue.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h" // UNUSED_ATTR
39 #include "stack/include/bt_types.h"
40 #include "stack/include/pan_api.h"
41 #include "types/raw_address.h"
43 /* RX and TX data flow mask */
44 #define BTA_PAN_RX_MASK 0x0F
45 #define BTA_PAN_TX_MASK 0xF0
47 /*******************************************************************************
49 * Function bta_pan_pm_conn_busy
51 * Description set pan pm connection busy state
53 * Params p_scb: state machine control block of pan connection
57 ******************************************************************************/
58 static void bta_pan_pm_conn_busy(tBTA_PAN_SCB* p_scb) {
59 if ((p_scb != NULL) && (p_scb->state != BTA_PAN_IDLE_ST))
60 bta_sys_busy(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
63 /*******************************************************************************
65 * Function bta_pan_pm_conn_idle
67 * Description set pan pm connection idle state
69 * Params p_scb: state machine control block of pan connection
73 ******************************************************************************/
74 static void bta_pan_pm_conn_idle(tBTA_PAN_SCB* p_scb) {
75 if ((p_scb != NULL) && (p_scb->state != BTA_PAN_IDLE_ST))
76 bta_sys_idle(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
79 /*******************************************************************************
81 * Function bta_pan_conn_state_cback
83 * Description Connection state callback from Pan profile
88 ******************************************************************************/
89 static void bta_pan_conn_state_cback(uint16_t handle, const RawAddress& bd_addr,
90 tPAN_RESULT state, bool is_role_change,
91 uint8_t src_role, uint8_t dst_role) {
93 tBTA_PAN_CONN* p_buf = (tBTA_PAN_CONN*)osi_malloc(sizeof(tBTA_PAN_CONN));
95 if ((state == PAN_SUCCESS) && !is_role_change) {
96 p_buf->hdr.event = BTA_PAN_CONN_OPEN_EVT;
97 p_scb = bta_pan_scb_by_handle(handle);
100 p_scb = bta_pan_scb_alloc();
102 /* we have exceeded maximum number of connections */
104 PAN_Disconnect(handle);
108 p_scb->handle = handle;
109 p_scb->local_role = src_role;
110 p_scb->peer_role = dst_role;
111 p_scb->pan_flow_enable = true;
112 p_scb->bd_addr = bd_addr;
113 p_scb->data_queue = fixed_queue_new(SIZE_MAX);
115 if (src_role == PAN_ROLE_CLIENT)
116 p_scb->app_id = bta_pan_cb.app_id[0];
117 else if (src_role == PAN_ROLE_NAP_SERVER)
118 p_scb->app_id = bta_pan_cb.app_id[2];
119 } else if ((state != PAN_SUCCESS) && !is_role_change) {
120 p_buf->hdr.event = BTA_PAN_CONN_CLOSE_EVT;
125 p_buf->result = state;
126 p_buf->hdr.layer_specific = handle;
128 bta_sys_sendmsg(p_buf);
131 /*******************************************************************************
133 * Function bta_pan_data_flow_cb
135 * Description Data flow status callback from PAN
140 ******************************************************************************/
141 static void bta_pan_data_flow_cb(uint16_t handle, tPAN_RESULT result) {
144 p_scb = bta_pan_scb_by_handle(handle);
145 if (p_scb == NULL) return;
147 if (result == PAN_TX_FLOW_ON) {
148 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
149 p_buf->layer_specific = handle;
150 p_buf->event = BTA_PAN_BNEP_FLOW_ENABLE_EVT;
151 bta_sys_sendmsg(p_buf);
152 bta_pan_co_rx_flow(handle, p_scb->app_id, true);
153 } else if (result == PAN_TX_FLOW_OFF) {
154 p_scb->pan_flow_enable = false;
155 bta_pan_co_rx_flow(handle, p_scb->app_id, false);
159 /*******************************************************************************
161 * Function bta_pan_data_buf_ind_cback
163 * Description data indication callback from pan profile
168 ******************************************************************************/
169 static void bta_pan_data_buf_ind_cback(uint16_t handle, const RawAddress& src,
170 const RawAddress& dst, uint16_t protocol,
171 BT_HDR* p_buf, bool ext, bool forward) {
172 tBTA_PAN_SCB* p_scb = bta_pan_scb_by_handle(handle);
177 if (sizeof(BT_HDR) + sizeof(tBTA_PAN_DATA_PARAMS) + p_buf->len >
179 android_errorWriteLog(0x534e4554, "63146237");
180 APPL_TRACE_ERROR("%s: received buffer length too large: %d", __func__,
185 BT_HDR* p_new_buf = (BT_HDR*)osi_malloc(PAN_BUF_SIZE);
186 memcpy((uint8_t*)(p_new_buf + 1) + sizeof(tBTA_PAN_DATA_PARAMS),
187 (uint8_t*)(p_buf + 1) + p_buf->offset, p_buf->len);
188 p_new_buf->len = p_buf->len;
189 p_new_buf->offset = sizeof(tBTA_PAN_DATA_PARAMS);
191 /* copy params into the space before the data */
192 ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->src = src;
193 ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->dst = dst;
194 ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->protocol = protocol;
195 ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->ext = ext;
196 ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->forward = forward;
198 fixed_queue_enqueue(p_scb->data_queue, p_new_buf);
199 BT_HDR_RIGID* p_event = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
200 p_event->layer_specific = handle;
201 p_event->event = BTA_PAN_RX_FROM_BNEP_READY_EVT;
202 bta_sys_sendmsg(p_event);
205 /*******************************************************************************
207 * Function bta_pan_pfilt_ind_cback
214 ******************************************************************************/
215 static void bta_pan_pfilt_ind_cback(uint16_t handle, bool indication,
216 tBNEP_RESULT result, uint16_t num_filters,
217 uint8_t* p_filters) {
218 bta_pan_co_pfilt_ind(
220 (tBTA_PAN_STATUS)((result == BNEP_SUCCESS) ? BTA_PAN_SUCCESS
222 num_filters, p_filters);
225 /*******************************************************************************
227 * Function bta_pan_mfilt_ind_cback
234 ******************************************************************************/
235 static void bta_pan_mfilt_ind_cback(uint16_t handle, bool indication,
236 tBNEP_RESULT result, uint16_t num_mfilters,
237 uint8_t* p_mfilters) {
238 bta_pan_co_mfilt_ind(
240 (tBTA_PAN_STATUS)((result == BNEP_SUCCESS) ? BTA_PAN_SUCCESS
242 num_mfilters, p_mfilters);
245 /*******************************************************************************
247 * Function bta_pan_has_multiple_connections
249 * Description Check whether there are multiple GN/NAP connections to
255 ******************************************************************************/
256 static bool bta_pan_has_multiple_connections(uint8_t app_id) {
257 tBTA_PAN_SCB* p_scb = NULL;
261 for (uint8_t index = 0; index < BTA_PAN_NUM_CONN; index++) {
262 p_scb = &bta_pan_cb.scb[index];
263 if (p_scb->in_use && app_id == p_scb->app_id) {
264 /* save temp bd_addr */
265 bd_addr = p_scb->bd_addr;
271 /* If cannot find a match then there is no connection at all */
272 if (!found) return false;
274 /* Find whether there is another connection with different device other than
276 Could be same service or different service */
277 for (uint8_t index = 0; index < BTA_PAN_NUM_CONN; index++) {
278 p_scb = &bta_pan_cb.scb[index];
279 if (p_scb->in_use && p_scb->app_id != bta_pan_cb.app_id[0] &&
280 bd_addr != p_scb->bd_addr) {
287 /*******************************************************************************
289 * Function bta_pan_enable
297 ******************************************************************************/
298 void bta_pan_enable(tBTA_PAN_DATA* p_data) {
299 tPAN_REGISTER reg_data;
301 bta_pan_cb.p_cback = p_data->api_enable.p_cback;
303 reg_data.pan_conn_state_cb = bta_pan_conn_state_cback;
304 reg_data.pan_bridge_req_cb = NULL;
305 reg_data.pan_data_buf_ind_cb = bta_pan_data_buf_ind_cback;
306 reg_data.pan_data_ind_cb = NULL;
307 reg_data.pan_pfilt_ind_cb = bta_pan_pfilt_ind_cback;
308 reg_data.pan_mfilt_ind_cb = bta_pan_mfilt_ind_cback;
309 reg_data.pan_tx_data_flow_cb = bta_pan_data_flow_cb;
311 PAN_Register(®_data);
313 bta_pan_cb.flow_mask = bta_pan_co_init(&bta_pan_cb.q_level);
314 bta_pan_cb.p_cback(BTA_PAN_ENABLE_EVT, NULL);
317 /*******************************************************************************
319 * Function bta_pan_set_role
325 ******************************************************************************/
326 void bta_pan_set_role(tBTA_PAN_DATA* p_data) {
330 bta_pan_cb.app_id[0] = p_data->api_set_role.user_app_id;
331 bta_pan_cb.app_id[2] = p_data->api_set_role.nap_app_id;
333 /* set security correctly in api and here */
335 PAN_SetRole(p_data->api_set_role.role, p_data->api_set_role.user_name,
336 p_data->api_set_role.nap_name);
338 bta_pan.set_role.role = p_data->api_set_role.role;
339 if (status == PAN_SUCCESS) {
340 if (p_data->api_set_role.role & PAN_ROLE_NAP_SERVER)
341 bta_sys_add_uuid(UUID_SERVCLASS_NAP);
343 bta_sys_remove_uuid(UUID_SERVCLASS_NAP);
345 if (p_data->api_set_role.role & PAN_ROLE_CLIENT)
346 bta_sys_add_uuid(UUID_SERVCLASS_PANU);
348 bta_sys_remove_uuid(UUID_SERVCLASS_PANU);
350 bta_pan.set_role.status = BTA_PAN_SUCCESS;
352 /* if status is not success clear everything */
354 PAN_SetRole(0, NULL, NULL);
355 bta_sys_remove_uuid(UUID_SERVCLASS_NAP);
356 bta_sys_remove_uuid(UUID_SERVCLASS_GN);
357 bta_sys_remove_uuid(UUID_SERVCLASS_PANU);
358 bta_pan.set_role.status = BTA_PAN_FAIL;
360 bta_pan_cb.p_cback(BTA_PAN_SET_ROLE_EVT, &bta_pan);
363 /*******************************************************************************
365 * Function bta_pan_disable
373 ******************************************************************************/
374 void bta_pan_disable(void) {
376 tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
379 /* close all connections */
380 PAN_SetRole(0, NULL, NULL);
382 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
383 bta_sys_remove_uuid(UUID_SERVCLASS_NAP);
384 bta_sys_remove_uuid(UUID_SERVCLASS_GN);
385 bta_sys_remove_uuid(UUID_SERVCLASS_PANU);
386 #endif // BTA_EIR_CANNED_UUID_LIST
387 /* free all queued up data buffers */
388 for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
390 while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_scb->data_queue)) !=
394 bta_pan_co_close(p_scb->handle, p_scb->app_id);
401 /*******************************************************************************
403 * Function bta_pan_open
409 ******************************************************************************/
410 void bta_pan_open(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
414 status = PAN_Connect(p_data->api_open.bd_addr, p_data->api_open.local_role,
415 p_data->api_open.peer_role, &p_scb->handle);
416 APPL_TRACE_DEBUG("%s pan connect status: %d", __func__, status);
418 if (status == PAN_SUCCESS) {
419 p_scb->bd_addr = p_data->api_open.bd_addr;
420 p_scb->local_role = p_data->api_open.local_role;
421 p_scb->peer_role = p_data->api_open.peer_role;
422 bta_pan.opening.bd_addr = p_data->api_open.bd_addr;
423 bta_pan.opening.handle = p_scb->handle;
424 bta_pan_cb.p_cback(BTA_PAN_OPENING_EVT, &bta_pan);
427 bta_pan_scb_dealloc(p_scb);
428 bta_pan.open.bd_addr = p_data->api_open.bd_addr;
429 bta_pan.open.status = BTA_PAN_FAIL;
430 bta_pan.open.local_role = p_data->api_open.local_role;
431 bta_pan.open.peer_role = p_data->api_open.peer_role;
432 bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, &bta_pan);
436 /*******************************************************************************
438 * Function bta_pan_close
446 ******************************************************************************/
447 void bta_pan_api_close(tBTA_PAN_SCB* p_scb, UNUSED_ATTR tBTA_PAN_DATA* p_data) {
448 tBTA_PAN_CONN* p_buf = (tBTA_PAN_CONN*)osi_malloc(sizeof(tBTA_PAN_CONN));
450 PAN_Disconnect(p_scb->handle);
453 * Send an event to BTA so that application will get the connection
456 p_buf->hdr.event = BTA_PAN_CONN_CLOSE_EVT;
457 p_buf->hdr.layer_specific = p_scb->handle;
459 bta_sys_sendmsg(p_buf);
462 /*******************************************************************************
464 * Function bta_pan_conn_open
466 * Description process connection open event
470 ******************************************************************************/
471 void bta_pan_conn_open(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
474 APPL_TRACE_DEBUG("%s pan connection result: %d", __func__,
475 p_data->conn.result);
477 bta_pan.open.bd_addr = p_scb->bd_addr;
478 bta_pan.open.handle = p_scb->handle;
479 bta_pan.open.local_role = p_scb->local_role;
480 bta_pan.open.peer_role = p_scb->peer_role;
482 if (p_data->conn.result == PAN_SUCCESS) {
483 bta_pan.open.status = BTA_PAN_SUCCESS;
484 p_scb->pan_flow_enable = true;
485 p_scb->app_flow_enable = true;
486 bta_sys_conn_open(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
488 bta_pan_scb_dealloc(p_scb);
489 bta_pan.open.status = BTA_PAN_FAIL;
492 p_scb->pan_flow_enable = true;
493 p_scb->app_flow_enable = true;
495 /* If app_id is NAP/GN, check whether there are multiple connections.
496 If there are, provide a special app_id to dm to enforce central role only.
498 if (p_scb->app_id == bta_pan_cb.app_id[2] &&
499 bta_pan_has_multiple_connections(p_scb->app_id)) {
500 p_scb->app_id = BTA_APP_ID_PAN_MULTI;
503 bta_sys_conn_open(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
504 bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, &bta_pan);
507 /*******************************************************************************
509 * Function bta_pan_conn_close
511 * Description process connection close event
517 ******************************************************************************/
518 void bta_pan_conn_close(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
522 bta_pan.close.handle = p_data->hdr.layer_specific;
524 bta_sys_conn_close(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
526 /* free all queued up data buffers */
527 while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_scb->data_queue)) != NULL)
530 bta_pan_scb_dealloc(p_scb);
532 bta_pan_cb.p_cback(BTA_PAN_CLOSE_EVT, &bta_pan);
535 /*******************************************************************************
537 * Function bta_pan_rx_path
539 * Description Handle data on the RX path (data sent from the phone to
545 ******************************************************************************/
546 void bta_pan_rx_path(tBTA_PAN_SCB* p_scb, UNUSED_ATTR tBTA_PAN_DATA* p_data) {
547 /* if data path configured for rx pull */
548 if ((bta_pan_cb.flow_mask & BTA_PAN_RX_MASK) == BTA_PAN_RX_PULL) {
549 /* if we can accept data */
550 if (p_scb->pan_flow_enable) {
551 /* call application callout function for rx path */
552 bta_pan_co_rx_path(p_scb->handle, p_scb->app_id);
555 /* else data path configured for rx push */
560 /*******************************************************************************
562 * Function bta_pan_tx_path
564 * Description Handle the TX data path (data sent from BTA to the phone).
569 ******************************************************************************/
570 void bta_pan_tx_path(tBTA_PAN_SCB* p_scb, UNUSED_ATTR tBTA_PAN_DATA* p_data) {
571 bta_pan_pm_conn_busy(p_scb);
572 /* call application callout function for tx path */
573 bta_pan_co_tx_path(p_scb->handle, p_scb->app_id);
575 /* free data that exceeds queue level */
576 while (fixed_queue_length(p_scb->data_queue) > bta_pan_cb.q_level) {
577 BT_HDR* p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_scb->data_queue);
578 if (p_buf != nullptr) {
583 bta_pan_pm_conn_idle(p_scb);
586 /*******************************************************************************
588 * Function bta_pan_tx_flow
590 * Description Set the application flow control state.
595 ******************************************************************************/
596 void bta_pan_tx_flow(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
597 p_scb->app_flow_enable = p_data->ci_tx_flow.enable;
600 /*******************************************************************************
602 * Function bta_pan_write_buf
604 * Description Handle a bta_pan_ci_rx_writebuf() and send data to PAN.
609 ******************************************************************************/
610 void bta_pan_write_buf(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
611 if ((bta_pan_cb.flow_mask & BTA_PAN_RX_MASK) == BTA_PAN_RX_PUSH_BUF) {
612 bta_pan_pm_conn_busy(p_scb);
614 PAN_WriteBuf(p_scb->handle, ((tBTA_PAN_DATA_PARAMS*)p_data)->dst,
615 ((tBTA_PAN_DATA_PARAMS*)p_data)->src,
616 ((tBTA_PAN_DATA_PARAMS*)p_data)->protocol, (BT_HDR*)p_data,
617 ((tBTA_PAN_DATA_PARAMS*)p_data)->ext);
618 bta_pan_pm_conn_idle(p_scb);
622 /*******************************************************************************
624 * Function bta_pan_free_buf
626 * Description Frees the data buffer during closing state
631 ******************************************************************************/
632 void bta_pan_free_buf(UNUSED_ATTR tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
636 #endif /* PAN_INCLUDED */