1 /******************************************************************************
3 * Copyright (C) 2009-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 * Filename: bta_pan_co.c
23 * Description: PAN stack callout api
26 ******************************************************************************/
28 #include "bta_pan_api.h"
29 #include "bta_pan_ci.h"
30 #include "bta_pan_co.h"
33 #include <hardware/bluetooth.h>
34 #include <hardware/bt_pan.h>
35 #include "btif_pan_internal.h"
36 #include "btif_sock_thread.h"
38 #include "btif_util.h"
40 /*******************************************************************************
42 ** Function bta_pan_co_init
47 ** Returns Data flow mask.
49 *******************************************************************************/
50 UINT8 bta_pan_co_init(UINT8 *q_level)
52 BTIF_TRACE_API("bta_pan_co_init");
54 /* set the q_level to 30 buffers */
57 //return (BTA_PAN_RX_PULL | BTA_PAN_TX_PULL);
58 return (BTA_PAN_RX_PUSH_BUF | BTA_PAN_RX_PUSH | BTA_PAN_TX_PULL);
61 /******************************************************************************
63 ** Function bta_pan_co_open
73 *******************************************************************************/
74 void bta_pan_co_open(UINT16 handle, UINT8 app_id, tBTA_PAN_ROLE local_role,
75 tBTA_PAN_ROLE peer_role, BD_ADDR peer_addr)
77 BTIF_TRACE_API("bta_pan_co_open:app_id:%d, local_role:%d, peer_role:%d, "
78 "handle:%d", app_id, local_role, peer_role, handle);
79 btpan_conn_t* conn = btpan_find_conn_addr(peer_addr);
81 conn = btpan_new_conn(handle, peer_addr, local_role, peer_role);
84 BTIF_TRACE_DEBUG("bta_pan_co_open:tap_fd:%d, open_count:%d, "
85 "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d",
86 btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, handle,
87 conn->local_role, conn->remote_role);
88 //refresh the role & bt address
90 btpan_cb.open_count++;
91 conn->handle = handle;
92 //bdcpy(conn->peer, peer_addr);
93 if(btpan_cb.tap_fd < 0)
95 btpan_cb.tap_fd = btpan_tap_open();
96 if(btpan_cb.tap_fd >= 0)
97 create_tap_read_thread(btpan_cb.tap_fd);
99 if(btpan_cb.tap_fd >= 0)
102 conn->state = PAN_STATE_OPEN;
103 bta_pan_ci_rx_ready(handle);
108 /*******************************************************************************
110 ** Function bta_pan_co_close
112 ** Description This function is called by PAN when a connection to a
118 *******************************************************************************/
119 void bta_pan_co_close(UINT16 handle, UINT8 app_id)
121 BTIF_TRACE_API("bta_pan_co_close:app_id:%d, handle:%d", app_id, handle);
122 btpan_conn_t* conn = btpan_find_conn_handle(handle);
123 if(conn && conn->state == PAN_STATE_OPEN)
125 BTIF_TRACE_DEBUG("bta_pan_co_close");
127 // let bta close event reset this handle as it needs
128 // the handle to find the connection upon CLOSE
130 conn->state = PAN_STATE_CLOSE;
131 btpan_cb.open_count--;
133 if(btpan_cb.open_count == 0 && btpan_cb.tap_fd != -1)
135 btpan_tap_close(btpan_cb.tap_fd);
136 btpan_cb.tap_fd = -1;
141 /*******************************************************************************
143 ** Function bta_pan_co_tx_path
145 ** Description This function is called by PAN to transfer data on the
146 ** TX path; that is, data being sent from BTA to the phone.
147 ** This function is used when the TX data path is configured
148 ** to use the pull interface. The implementation of this
149 ** function will typically call Bluetooth stack functions
150 ** PORT_Read() or PORT_ReadData() to read data from RFCOMM
151 ** and then a platform-specific function to send data that
152 ** data to the phone.
157 *******************************************************************************/
158 void bta_pan_co_tx_path(UINT16 handle, UINT8 app_id)
168 BTIF_TRACE_API("bta_pan_co_tx_path, handle:%d, app_id:%d", handle, app_id);
170 btpan_conn_t* conn = btpan_find_conn_handle(handle);
171 if(!conn || conn->state != PAN_STATE_OPEN)
173 BTIF_TRACE_ERROR("bta_pan_co_tx_path: cannot find pan connction or conn"
174 "is not opened, conn:%p, conn->state:%d", conn, conn ? conn->state : -1);
179 /* read next data buffer from pan */
180 if ((p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol,
183 BTIF_TRACE_DEBUG("bta_pan_co_tx_path, calling btapp_tap_send, "
184 "p_buf->len:%d, offset:%d", p_buf->len, p_buf->offset);
185 if(is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src))
187 BTIF_TRACE_DEBUG("pan bt peer addr: %02x:%02x:%02x:%02x:%02x:%02x",
188 conn->peer[0], conn->peer[1], conn->peer[2],
189 conn->peer[3],conn->peer[4], conn->peer[5]);
190 BTIF_TRACE_DEBUG(" update its ethernet addr: "
191 "%02x:%02x:%02x:%02x:%02x:%02x", src[0], src[1], src[2],
192 src[3],src[4], src[5]);
193 memcpy(conn->eth_addr, src, sizeof(conn->eth_addr));
196 btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol,
197 (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext, forward);
201 } while (p_buf != NULL);
204 /*******************************************************************************
206 ** Function bta_pan_co_rx_path
215 *******************************************************************************/
216 void bta_pan_co_rx_path(UINT16 handle, UINT8 app_id)
221 BTIF_TRACE_API("bta_pan_co_rx_path not used");
224 /*******************************************************************************
226 ** Function bta_pan_co_tx_write
228 ** Description This function is called by PAN to send data to the phone
229 ** when the TX path is configured to use a push interface.
230 ** The implementation of this function must copy the data to
231 ** the phone's memory.
236 *******************************************************************************/
237 void bta_pan_co_tx_write(UINT16 handle, UINT8 app_id, BD_ADDR src, BD_ADDR dst,
238 UINT16 protocol, UINT8 *p_data,
239 UINT16 len, BOOLEAN ext, BOOLEAN forward)
251 BTIF_TRACE_API("bta_pan_co_tx_write not used");
254 /*******************************************************************************
256 ** Function bta_pan_co_tx_writebuf
258 ** Description This function is called by PAN to send data to the phone
259 ** when the TX path is configured to use a push interface with
260 ** zero copy. The phone must free the buffer using function
261 ** GKI_freebuf() when it is through processing the buffer.
264 ** Returns TRUE if flow enabled
266 *******************************************************************************/
267 void bta_pan_co_tx_writebuf(UINT16 handle, UINT8 app_id, BD_ADDR src,
268 BD_ADDR dst, UINT16 protocol, BT_HDR *p_buf,
269 BOOLEAN ext, BOOLEAN forward)
280 BTIF_TRACE_API("bta_pan_co_tx_writebuf not used");
283 /*******************************************************************************
285 ** Function bta_pan_co_rx_flow
287 ** Description This function is called by PAN to enable or disable
288 ** data flow on the RX path when it is configured to use
289 ** a push interface. If data flow is disabled the phone must
290 ** not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf()
291 ** until data flow is enabled again.
296 *******************************************************************************/
297 void bta_pan_co_rx_flow(UINT16 handle, UINT8 app_id, BOOLEAN enable)
303 BTIF_TRACE_API("bta_pan_co_rx_flow, enabled:%d, not used", enable);
304 btpan_conn_t* conn = btpan_find_conn_handle(handle);
305 if(!conn || conn->state != PAN_STATE_OPEN)
307 btpan_set_flow_control(enable);
310 /*******************************************************************************
312 ** Function bta_pan_co_filt_ind
314 ** Description protocol filter indication from peer device
318 *******************************************************************************/
319 void bta_pan_co_pfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
320 UINT16 len, UINT8 *p_filters)
328 BTIF_TRACE_API("bta_pan_co_pfilt_ind");
331 /*******************************************************************************
333 ** Function bta_pan_co_mfilt_ind
335 ** Description multicast filter indication from peer device
339 *******************************************************************************/
340 void bta_pan_co_mfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
341 UINT16 len, UINT8 *p_filters)
349 BTIF_TRACE_API("bta_pan_co_mfilt_ind");