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 "bt_common.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 <string.h>
38 #include "btif_util.h"
39 #include "btcore/include/bdaddr.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     BD_ADDR            src;
163     BD_ADDR            dst;
164     UINT16            protocol;
165     BOOLEAN            ext;
166     BOOLEAN         forward;
167 
168     BTIF_TRACE_API("%s, handle:%d, app_id:%d", __func__, handle, app_id);
169 
170     btpan_conn_t* conn = btpan_find_conn_handle(handle);
171     if (!conn)
172     {
173         BTIF_TRACE_ERROR("%s: cannot find pan connection", __func__);
174         return;
175     }
176     else if(conn->state != PAN_STATE_OPEN)
177     {
178         BTIF_TRACE_ERROR("%s: conn is not opened, conn:%p, conn->state:%d",
179             __func__, conn, conn->state);
180         return;
181     }
182 
183     do
184     {
185         /* read next data buffer from pan */
186         if ((p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol,
187                                  &ext, &forward)))
188         {
189             bdstr_t bdstr;
190             BTIF_TRACE_DEBUG("%s, calling btapp_tap_send, "
191                 "p_buf->len:%d, offset:%d", __func__, p_buf->len, p_buf->offset);
192             if(is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src))
193             {
194                 BTIF_TRACE_DEBUG("%s pan bt peer addr: %s", __func__,
195                     bdaddr_to_string((bt_bdaddr_t *)conn->peer, bdstr, sizeof(bdstr)));
196                 bdaddr_to_string((bt_bdaddr_t *)src, bdstr, sizeof(bdstr));
197                 BTIF_TRACE_DEBUG("%s:     update its ethernet addr: %s", __func__,
198                     bdaddr_to_string((bt_bdaddr_t *)src, bdstr, sizeof(bdstr)));
199                 memcpy(conn->eth_addr, src, sizeof(conn->eth_addr));
200 
201             }
202             btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol,
203                 (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext, forward);
204             osi_free(p_buf);
205         }
206 
207     } while (p_buf != NULL);
208 }
209 
210 /*******************************************************************************
211 **
212 ** Function         bta_pan_co_rx_path
213 **
214 ** Description
215 **
216 **
217 **
218 **
219 ** Returns          void
220 **
221 *******************************************************************************/
bta_pan_co_rx_path(UINT16 handle,UINT8 app_id)222 void bta_pan_co_rx_path(UINT16 handle, UINT8 app_id)
223 {
224     UNUSED(handle);
225     UNUSED(app_id);
226 
227     BTIF_TRACE_API("bta_pan_co_rx_path not used");
228 }
229 
230 /*******************************************************************************
231 **
232 ** Function         bta_pan_co_tx_write
233 **
234 ** Description      This function is called by PAN to send data to the phone
235 **                  when the TX path is configured to use a push interface.
236 **                  The implementation of this function must copy the data to
237 **                  the phone's memory.
238 **
239 **
240 ** Returns          void
241 **
242 *******************************************************************************/
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)243 void bta_pan_co_tx_write(UINT16 handle, UINT8 app_id, BD_ADDR src, BD_ADDR dst,
244                                 UINT16 protocol, UINT8 *p_data,
245                                 UINT16 len, BOOLEAN ext, BOOLEAN forward)
246 {
247     UNUSED(handle);
248     UNUSED(app_id);
249     UNUSED(src);
250     UNUSED(dst);
251     UNUSED(protocol);
252     UNUSED(p_data);
253     UNUSED(len);
254     UNUSED(ext);
255     UNUSED(forward);
256 
257     BTIF_TRACE_API("bta_pan_co_tx_write not used");
258 }
259 
260 /*******************************************************************************
261 **
262 ** Function         bta_pan_co_tx_writebuf
263 **
264 ** Description      This function is called by PAN to send data to the phone
265 **                  when the TX path is configured to use a push interface with
266 **                  zero copy.  The phone must free the buffer using function
267 **                  osi_free() when it is through processing the buffer.
268 **
269 **
270 ** Returns          TRUE if flow enabled
271 **
272 *******************************************************************************/
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)273 void  bta_pan_co_tx_writebuf(UINT16 handle, UINT8 app_id, BD_ADDR src,
274                                     BD_ADDR dst, UINT16 protocol, BT_HDR *p_buf,
275                                     BOOLEAN ext, BOOLEAN forward)
276 {
277     UNUSED(handle);
278     UNUSED(app_id);
279     UNUSED(src);
280     UNUSED(dst);
281     UNUSED(protocol);
282     UNUSED(p_buf);
283     UNUSED(ext);
284     UNUSED(forward);
285 
286     BTIF_TRACE_API("bta_pan_co_tx_writebuf not used");
287 }
288 
289 /*******************************************************************************
290 **
291 ** Function         bta_pan_co_rx_flow
292 **
293 ** Description      This function is called by PAN to enable or disable
294 **                  data flow on the RX path when it is configured to use
295 **                  a push interface.  If data flow is disabled the phone must
296 **                  not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf()
297 **                  until data flow is enabled again.
298 **
299 **
300 ** Returns          void
301 **
302 *******************************************************************************/
bta_pan_co_rx_flow(UINT16 handle,UINT8 app_id,BOOLEAN enable)303 void bta_pan_co_rx_flow(UINT16 handle, UINT8 app_id, BOOLEAN enable)
304 {
305     UNUSED(handle);
306     UNUSED(app_id);
307     UNUSED(enable);
308 
309     BTIF_TRACE_API("bta_pan_co_rx_flow, enabled:%d, not used", enable);
310     btpan_conn_t* conn = btpan_find_conn_handle(handle);
311     if(!conn || conn->state != PAN_STATE_OPEN)
312         return;
313     btpan_set_flow_control(enable);
314 }
315 
316 /*******************************************************************************
317 **
318 ** Function         bta_pan_co_filt_ind
319 **
320 ** Description      protocol filter indication from peer device
321 **
322 ** Returns          void
323 **
324 *******************************************************************************/
bta_pan_co_pfilt_ind(UINT16 handle,BOOLEAN indication,tBTA_PAN_STATUS result,UINT16 len,UINT8 * p_filters)325 void bta_pan_co_pfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
326                                     UINT16 len, UINT8 *p_filters)
327 {
328     UNUSED(handle);
329     UNUSED(indication);
330     UNUSED(result);
331     UNUSED(len);
332     UNUSED(p_filters);
333 
334     BTIF_TRACE_API("bta_pan_co_pfilt_ind");
335 }
336 
337 /*******************************************************************************
338 **
339 ** Function         bta_pan_co_mfilt_ind
340 **
341 ** Description      multicast filter indication from peer device
342 **
343 ** Returns          void
344 **
345 *******************************************************************************/
bta_pan_co_mfilt_ind(UINT16 handle,BOOLEAN indication,tBTA_PAN_STATUS result,UINT16 len,UINT8 * p_filters)346 void bta_pan_co_mfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
347                                     UINT16 len, UINT8 *p_filters)
348 {
349     UNUSED(handle);
350     UNUSED(indication);
351     UNUSED(result);
352     UNUSED(len);
353     UNUSED(p_filters);
354 
355     BTIF_TRACE_API("bta_pan_co_mfilt_ind");
356 }
357 
358