OSDN Git Service

merge in m-wireless-internal-release history after reset to d232721ae5e5b6949a5249f0d...
[android-x86/system-bt.git] / btif / co / bta_pan_co.c
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 <string.h>
38 #include "btif_util.h"
39
40 /*******************************************************************************
41 **
42 ** Function         bta_pan_co_init
43 **
44 ** Description
45 **
46 **
47 ** Returns          Data flow mask.
48 **
49 *******************************************************************************/
50 UINT8 bta_pan_co_init(UINT8 *q_level)
51 {
52     BTIF_TRACE_API("bta_pan_co_init");
53
54     /* set the q_level to 30 buffers */
55     *q_level = 30;
56
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);
59 }
60
61 /******************************************************************************
62 **
63 ** Function         bta_pan_co_open
64 **
65 ** Description
66 **
67 **
68 **
69 **
70 **
71 ** Returns          void
72 **
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)
76 {
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);
80     if(conn == NULL)
81         conn = btpan_new_conn(handle, peer_addr, local_role, peer_role);
82     if(conn)
83     {
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
89
90         btpan_cb.open_count++;
91         conn->handle = handle;
92         //bdcpy(conn->peer, peer_addr);
93         if(btpan_cb.tap_fd < 0)
94         {
95             btpan_cb.tap_fd = btpan_tap_open();
96             if(btpan_cb.tap_fd >= 0)
97                 create_tap_read_thread(btpan_cb.tap_fd);
98         }
99         if(btpan_cb.tap_fd >= 0)
100         {
101             btpan_cb.flow = 1;
102             conn->state = PAN_STATE_OPEN;
103             bta_pan_ci_rx_ready(handle);
104         }
105     }
106 }
107
108 /*******************************************************************************
109 **
110 ** Function         bta_pan_co_close
111 **
112 ** Description      This function is called by PAN when a connection to a
113 **                  peer is closed.
114 **
115 **
116 ** Returns          void
117 **
118 *******************************************************************************/
119 void bta_pan_co_close(UINT16 handle, UINT8 app_id)
120 {
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)
124     {
125         BTIF_TRACE_DEBUG("bta_pan_co_close");
126
127         // let bta close event reset this handle as it needs
128         // the handle to find the connection upon CLOSE
129         //conn->handle = -1;
130         conn->state = PAN_STATE_CLOSE;
131         btpan_cb.open_count--;
132
133         if(btpan_cb.open_count == 0 && btpan_cb.tap_fd != -1)
134         {
135             btpan_tap_close(btpan_cb.tap_fd);
136             btpan_cb.tap_fd = -1;
137         }
138     }
139 }
140
141 /*******************************************************************************
142 **
143 ** Function         bta_pan_co_tx_path
144 **
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.
153 **
154 **
155 ** Returns          void
156 **
157 *******************************************************************************/
158 void bta_pan_co_tx_path(UINT16 handle, UINT8 app_id)
159 {
160     BT_HDR          *p_buf;
161     UINT8           i;
162     BD_ADDR            src;
163     BD_ADDR            dst;
164     UINT16            protocol;
165     BOOLEAN            ext;
166     BOOLEAN         forward;
167
168     BTIF_TRACE_API("bta_pan_co_tx_path, handle:%d, app_id:%d", handle, app_id);
169
170     btpan_conn_t* conn = btpan_find_conn_handle(handle);
171     if(!conn || conn->state != PAN_STATE_OPEN)
172     {
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);
175         return;
176     }
177     do
178     {
179         /* read next data buffer from pan */
180         if ((p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol,
181                                  &ext, &forward)))
182         {
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))
186             {
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));
194
195             }
196             btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol,
197                 (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext, forward);
198             GKI_freebuf(p_buf);
199         }
200
201     } while (p_buf != NULL);
202 }
203
204 /*******************************************************************************
205 **
206 ** Function         bta_pan_co_rx_path
207 **
208 ** Description
209 **
210 **
211 **
212 **
213 ** Returns          void
214 **
215 *******************************************************************************/
216 void bta_pan_co_rx_path(UINT16 handle, UINT8 app_id)
217 {
218     UNUSED(handle);
219     UNUSED(app_id);
220
221     BTIF_TRACE_API("bta_pan_co_rx_path not used");
222 }
223
224 /*******************************************************************************
225 **
226 ** Function         bta_pan_co_tx_write
227 **
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.
232 **
233 **
234 ** Returns          void
235 **
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)
240 {
241     UNUSED(handle);
242     UNUSED(app_id);
243     UNUSED(src);
244     UNUSED(dst);
245     UNUSED(protocol);
246     UNUSED(p_data);
247     UNUSED(len);
248     UNUSED(ext);
249     UNUSED(forward);
250
251     BTIF_TRACE_API("bta_pan_co_tx_write not used");
252 }
253
254 /*******************************************************************************
255 **
256 ** Function         bta_pan_co_tx_writebuf
257 **
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.
262 **
263 **
264 ** Returns          TRUE if flow enabled
265 **
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)
270 {
271     UNUSED(handle);
272     UNUSED(app_id);
273     UNUSED(src);
274     UNUSED(dst);
275     UNUSED(protocol);
276     UNUSED(p_buf);
277     UNUSED(ext);
278     UNUSED(forward);
279
280     BTIF_TRACE_API("bta_pan_co_tx_writebuf not used");
281 }
282
283 /*******************************************************************************
284 **
285 ** Function         bta_pan_co_rx_flow
286 **
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.
292 **
293 **
294 ** Returns          void
295 **
296 *******************************************************************************/
297 void bta_pan_co_rx_flow(UINT16 handle, UINT8 app_id, BOOLEAN enable)
298 {
299     UNUSED(handle);
300     UNUSED(app_id);
301     UNUSED(enable);
302
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)
306         return;
307     btpan_set_flow_control(enable);
308 }
309
310 /*******************************************************************************
311 **
312 ** Function         bta_pan_co_filt_ind
313 **
314 ** Description      protocol filter indication from peer device
315 **
316 ** Returns          void
317 **
318 *******************************************************************************/
319 void bta_pan_co_pfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
320                                     UINT16 len, UINT8 *p_filters)
321 {
322     UNUSED(handle);
323     UNUSED(indication);
324     UNUSED(result);
325     UNUSED(len);
326     UNUSED(p_filters);
327
328     BTIF_TRACE_API("bta_pan_co_pfilt_ind");
329 }
330
331 /*******************************************************************************
332 **
333 ** Function         bta_pan_co_mfilt_ind
334 **
335 ** Description      multicast filter indication from peer device
336 **
337 ** Returns          void
338 **
339 *******************************************************************************/
340 void bta_pan_co_mfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
341                                     UINT16 len, UINT8 *p_filters)
342 {
343     UNUSED(handle);
344     UNUSED(indication);
345     UNUSED(result);
346     UNUSED(len);
347     UNUSED(p_filters);
348
349     BTIF_TRACE_API("bta_pan_co_mfilt_ind");
350 }
351