1 /******************************************************************************
2 *
3 * Copyright (C) 2009-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 * Filename: bta_pan_co.c
22 *
23 * Description: PAN stack callout api
24 *
25 *
26 ******************************************************************************/
27 #include "bta_api.h"
28 #include "bta_pan_api.h"
29 #include "bta_pan_ci.h"
30 #include "bta_pan_co.h"
31 #include "pan_api.h"
32 #include "gki.h"
33 #include <hardware/bluetooth.h>
34 #include <hardware/bt_pan.h>
35 #include "btif_pan_internal.h"
36 #include "btif_sock_thread.h"
37 #include "bd.h"
38 #include <string.h>
39 #include "btif_util.h"
40
41 /*******************************************************************************
42 **
43 ** Function bta_pan_co_init
44 **
45 ** Description
46 **
47 **
48 ** Returns Data flow mask.
49 **
50 *******************************************************************************/
bta_pan_co_init(UINT8 * q_level)51 UINT8 bta_pan_co_init(UINT8 *q_level)
52 {
53 BTIF_TRACE_API("bta_pan_co_init");
54
55 /* set the q_level to 30 buffers */
56 *q_level = 30;
57
58 //return (BTA_PAN_RX_PULL | BTA_PAN_TX_PULL);
59 return (BTA_PAN_RX_PUSH_BUF | BTA_PAN_RX_PUSH | BTA_PAN_TX_PULL);
60 }
61
62 /******************************************************************************
63 **
64 ** Function bta_pan_co_open
65 **
66 ** Description
67 **
68 **
69 **
70 **
71 **
72 ** Returns void
73 **
74 *******************************************************************************/
bta_pan_co_open(UINT16 handle,UINT8 app_id,tBTA_PAN_ROLE local_role,tBTA_PAN_ROLE peer_role,BD_ADDR peer_addr)75 void bta_pan_co_open(UINT16 handle, UINT8 app_id, tBTA_PAN_ROLE local_role,
76 tBTA_PAN_ROLE peer_role, BD_ADDR peer_addr)
77 {
78 BTIF_TRACE_API("bta_pan_co_open:app_id:%d, local_role:%d, peer_role:%d, "
79 "handle:%d", app_id, local_role, peer_role, handle);
80 btpan_conn_t* conn = btpan_find_conn_addr(peer_addr);
81 if(conn == NULL)
82 conn = btpan_new_conn(handle, peer_addr, local_role, peer_role);
83 if(conn)
84 {
85 BTIF_TRACE_DEBUG("bta_pan_co_open:tap_fd:%d, open_count:%d, "
86 "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d",
87 btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, handle,
88 conn->local_role, conn->remote_role);
89 //refresh the role & bt address
90
91 btpan_cb.open_count++;
92 conn->handle = handle;
93 //bdcpy(conn->peer, peer_addr);
94 if(btpan_cb.tap_fd < 0)
95 {
96 btpan_cb.tap_fd = btpan_tap_open();
97 if(btpan_cb.tap_fd >= 0)
98 create_tap_read_thread(btpan_cb.tap_fd);
99 }
100 if(btpan_cb.tap_fd >= 0)
101 {
102 btpan_cb.flow = 1;
103 conn->state = PAN_STATE_OPEN;
104 bta_pan_ci_rx_ready(handle);
105 }
106 }
107 }
108
109 /*******************************************************************************
110 **
111 ** Function bta_pan_co_close
112 **
113 ** Description This function is called by PAN when a connection to a
114 ** peer is closed.
115 **
116 **
117 ** Returns void
118 **
119 *******************************************************************************/
bta_pan_co_close(UINT16 handle,UINT8 app_id)120 void bta_pan_co_close(UINT16 handle, UINT8 app_id)
121 {
122 BTIF_TRACE_API("bta_pan_co_close:app_id:%d, handle:%d", app_id, handle);
123 btpan_conn_t* conn = btpan_find_conn_handle(handle);
124 if(conn && conn->state == PAN_STATE_OPEN)
125 {
126 BTIF_TRACE_DEBUG("bta_pan_co_close");
127
128 // let bta close event reset this handle as it needs
129 // the handle to find the connection upon CLOSE
130 //conn->handle = -1;
131 conn->state = PAN_STATE_CLOSE;
132 btpan_cb.open_count--;
133
134 if(btpan_cb.open_count == 0 && btpan_cb.tap_fd != -1)
135 {
136 btpan_tap_close(btpan_cb.tap_fd);
137 btpan_cb.tap_fd = -1;
138 }
139 }
140 }
141
142 /*******************************************************************************
143 **
144 ** Function bta_pan_co_tx_path
145 **
146 ** Description This function is called by PAN to transfer data on the
147 ** TX path; that is, data being sent from BTA to the phone.
148 ** This function is used when the TX data path is configured
149 ** to use the pull interface. The implementation of this
150 ** function will typically call Bluetooth stack functions
151 ** PORT_Read() or PORT_ReadData() to read data from RFCOMM
152 ** and then a platform-specific function to send data that
153 ** data to the phone.
154 **
155 **
156 ** Returns void
157 **
158 *******************************************************************************/
bta_pan_co_tx_path(UINT16 handle,UINT8 app_id)159 void bta_pan_co_tx_path(UINT16 handle, UINT8 app_id)
160 {
161 BT_HDR *p_buf;
162 UINT8 i;
163 BD_ADDR src;
164 BD_ADDR dst;
165 UINT16 protocol;
166 BOOLEAN ext;
167 BOOLEAN forward;
168
169 BTIF_TRACE_API("%s, handle:%d, app_id:%d", __func__, handle, app_id);
170
171 btpan_conn_t* conn = btpan_find_conn_handle(handle);
172 if (!conn)
173 {
174 BTIF_TRACE_ERROR("%s: cannot find pan connection", __func__);
175 return;
176 }
177 else if(conn->state != PAN_STATE_OPEN)
178 {
179 BTIF_TRACE_ERROR("%s: conn is not opened, conn:%p, conn->state:%d",
180 __func__, conn, conn->state);
181 return;
182 }
183
184 do
185 {
186 /* read next data buffer from pan */
187 if ((p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol,
188 &ext, &forward)))
189 {
190 bdstr_t bdstr;
191 BTIF_TRACE_DEBUG("%s, calling btapp_tap_send, "
192 "p_buf->len:%d, offset:%d", __func__, p_buf->len, p_buf->offset);
193 if(is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src))
194 {
195 BTIF_TRACE_DEBUG("%s pan bt peer addr: %s", __func__,
196 bd2str((bt_bdaddr_t *)conn->peer, &bdstr));
197 bd2str((bt_bdaddr_t *)src, &bdstr);
198 BTIF_TRACE_DEBUG("%s: update its ethernet addr: %s", __func__,
199 bd2str((bt_bdaddr_t *)src, &bdstr));
200 memcpy(conn->eth_addr, src, sizeof(conn->eth_addr));
201
202 }
203 btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol,
204 (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext, forward);
205 GKI_freebuf(p_buf);
206 }
207
208 } while (p_buf != NULL);
209 }
210
211 /*******************************************************************************
212 **
213 ** Function bta_pan_co_rx_path
214 **
215 ** Description
216 **
217 **
218 **
219 **
220 ** Returns void
221 **
222 *******************************************************************************/
bta_pan_co_rx_path(UINT16 handle,UINT8 app_id)223 void bta_pan_co_rx_path(UINT16 handle, UINT8 app_id)
224 {
225 UNUSED(handle);
226 UNUSED(app_id);
227
228 BTIF_TRACE_API("bta_pan_co_rx_path not used");
229 }
230
231 /*******************************************************************************
232 **
233 ** Function bta_pan_co_tx_write
234 **
235 ** Description This function is called by PAN to send data to the phone
236 ** when the TX path is configured to use a push interface.
237 ** The implementation of this function must copy the data to
238 ** the phone's memory.
239 **
240 **
241 ** Returns void
242 **
243 *******************************************************************************/
bta_pan_co_tx_write(UINT16 handle,UINT8 app_id,BD_ADDR src,BD_ADDR dst,UINT16 protocol,UINT8 * p_data,UINT16 len,BOOLEAN ext,BOOLEAN forward)244 void bta_pan_co_tx_write(UINT16 handle, UINT8 app_id, BD_ADDR src, BD_ADDR dst,
245 UINT16 protocol, UINT8 *p_data,
246 UINT16 len, BOOLEAN ext, BOOLEAN forward)
247 {
248 UNUSED(handle);
249 UNUSED(app_id);
250 UNUSED(src);
251 UNUSED(dst);
252 UNUSED(protocol);
253 UNUSED(p_data);
254 UNUSED(len);
255 UNUSED(ext);
256 UNUSED(forward);
257
258 BTIF_TRACE_API("bta_pan_co_tx_write not used");
259 }
260
261 /*******************************************************************************
262 **
263 ** Function bta_pan_co_tx_writebuf
264 **
265 ** Description This function is called by PAN to send data to the phone
266 ** when the TX path is configured to use a push interface with
267 ** zero copy. The phone must free the buffer using function
268 ** GKI_freebuf() when it is through processing the buffer.
269 **
270 **
271 ** Returns TRUE if flow enabled
272 **
273 *******************************************************************************/
bta_pan_co_tx_writebuf(UINT16 handle,UINT8 app_id,BD_ADDR src,BD_ADDR dst,UINT16 protocol,BT_HDR * p_buf,BOOLEAN ext,BOOLEAN forward)274 void bta_pan_co_tx_writebuf(UINT16 handle, UINT8 app_id, BD_ADDR src,
275 BD_ADDR dst, UINT16 protocol, BT_HDR *p_buf,
276 BOOLEAN ext, BOOLEAN forward)
277 {
278 UNUSED(handle);
279 UNUSED(app_id);
280 UNUSED(src);
281 UNUSED(dst);
282 UNUSED(protocol);
283 UNUSED(p_buf);
284 UNUSED(ext);
285 UNUSED(forward);
286
287 BTIF_TRACE_API("bta_pan_co_tx_writebuf not used");
288 }
289
290 /*******************************************************************************
291 **
292 ** Function bta_pan_co_rx_flow
293 **
294 ** Description This function is called by PAN to enable or disable
295 ** data flow on the RX path when it is configured to use
296 ** a push interface. If data flow is disabled the phone must
297 ** not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf()
298 ** until data flow is enabled again.
299 **
300 **
301 ** Returns void
302 **
303 *******************************************************************************/
bta_pan_co_rx_flow(UINT16 handle,UINT8 app_id,BOOLEAN enable)304 void bta_pan_co_rx_flow(UINT16 handle, UINT8 app_id, BOOLEAN enable)
305 {
306 UNUSED(handle);
307 UNUSED(app_id);
308 UNUSED(enable);
309
310 BTIF_TRACE_API("bta_pan_co_rx_flow, enabled:%d, not used", enable);
311 btpan_conn_t* conn = btpan_find_conn_handle(handle);
312 if(!conn || conn->state != PAN_STATE_OPEN)
313 return;
314 btpan_set_flow_control(enable);
315 }
316
317 /*******************************************************************************
318 **
319 ** Function bta_pan_co_filt_ind
320 **
321 ** Description protocol filter indication from peer device
322 **
323 ** Returns void
324 **
325 *******************************************************************************/
bta_pan_co_pfilt_ind(UINT16 handle,BOOLEAN indication,tBTA_PAN_STATUS result,UINT16 len,UINT8 * p_filters)326 void bta_pan_co_pfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
327 UINT16 len, UINT8 *p_filters)
328 {
329 UNUSED(handle);
330 UNUSED(indication);
331 UNUSED(result);
332 UNUSED(len);
333 UNUSED(p_filters);
334
335 BTIF_TRACE_API("bta_pan_co_pfilt_ind");
336 }
337
338 /*******************************************************************************
339 **
340 ** Function bta_pan_co_mfilt_ind
341 **
342 ** Description multicast filter indication from peer device
343 **
344 ** Returns void
345 **
346 *******************************************************************************/
bta_pan_co_mfilt_ind(UINT16 handle,BOOLEAN indication,tBTA_PAN_STATUS result,UINT16 len,UINT8 * p_filters)347 void bta_pan_co_mfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
348 UINT16 len, UINT8 *p_filters)
349 {
350 UNUSED(handle);
351 UNUSED(indication);
352 UNUSED(result);
353 UNUSED(len);
354 UNUSED(p_filters);
355
356 BTIF_TRACE_API("bta_pan_co_mfilt_ind");
357 }
358
359