• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 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_pan_co.h"
28 
29 #include <base/logging.h>
30 #include <hardware/bluetooth.h>
31 #include <hardware/bt_pan.h>
32 #include <string.h>
33 
34 #include "bta_api.h"
35 #include "bta_pan_api.h"
36 #include "bta_pan_ci.h"
37 #include "btif_pan_internal.h"
38 #include "btif_sock_thread.h"
39 #include "btif_util.h"
40 #include "osi/include/allocator.h"
41 #include "osi/include/osi.h"
42 #include "pan_api.h"
43 #include "stack/include/bt_hdr.h"
44 #include "types/raw_address.h"
45 
46 /*******************************************************************************
47  *
48  * Function         bta_pan_co_init
49  *
50  * Description
51  *
52  *
53  * Returns          Data flow mask.
54  *
55  ******************************************************************************/
bta_pan_co_init(uint8_t * q_level)56 uint8_t bta_pan_co_init(uint8_t* q_level) {
57   BTIF_TRACE_API("bta_pan_co_init");
58 
59   /* set the q_level to 30 buffers */
60   *q_level = 30;
61 
62   // return (BTA_PAN_RX_PULL | BTA_PAN_TX_PULL);
63   return (BTA_PAN_RX_PUSH_BUF | BTA_PAN_RX_PUSH | BTA_PAN_TX_PULL);
64 }
65 
66 /*******************************************************************************
67  *
68  * Function         bta_pan_co_close
69  *
70  * Description      This function is called by PAN when a connection to a
71  *                  peer is closed.
72  *
73  *
74  * Returns          void
75  *
76  ******************************************************************************/
bta_pan_co_close(uint16_t handle,uint8_t app_id)77 void bta_pan_co_close(uint16_t handle, uint8_t app_id) {
78   BTIF_TRACE_API("bta_pan_co_close:app_id:%d, handle:%d", app_id, handle);
79   btpan_conn_t* conn = btpan_find_conn_handle(handle);
80   if (conn && conn->state == PAN_STATE_OPEN) {
81     BTIF_TRACE_DEBUG("bta_pan_co_close");
82 
83     // let bta close event reset this handle as it needs
84     // the handle to find the connection upon CLOSE
85     // conn->handle = -1;
86     conn->state = PAN_STATE_CLOSE;
87     btpan_cb.open_count--;
88 
89     if (btpan_cb.open_count == 0 && btpan_cb.tap_fd != -1) {
90       btpan_tap_close(btpan_cb.tap_fd);
91       btpan_cb.tap_fd = -1;
92     }
93   }
94 }
95 
96 /*******************************************************************************
97  *
98  * Function         bta_pan_co_tx_path
99  *
100  * Description      This function is called by PAN to transfer data on the
101  *                  TX path; that is, data being sent from BTA to the phone.
102  *                  This function is used when the TX data path is configured
103  *                  to use the pull interface.  The implementation of this
104  *                  function will typically call Bluetooth stack functions
105  *                  PORT_Read() or PORT_ReadData() to read data from RFCOMM
106  *                  and then a platform-specific function to send data that
107  *                  data to the phone.
108  *
109  *
110  * Returns          void
111  *
112  ******************************************************************************/
bta_pan_co_tx_path(uint16_t handle,uint8_t app_id)113 void bta_pan_co_tx_path(uint16_t handle, uint8_t app_id) {
114   BT_HDR* p_buf;
115   RawAddress src;
116   RawAddress dst;
117   uint16_t protocol;
118   bool ext;
119   bool forward;
120 
121   BTIF_TRACE_API("%s, handle:%d, app_id:%d", __func__, handle, app_id);
122 
123   btpan_conn_t* conn = btpan_find_conn_handle(handle);
124   if (!conn) {
125     BTIF_TRACE_ERROR("%s: cannot find pan connection", __func__);
126     return;
127   } else if (conn->state != PAN_STATE_OPEN) {
128     BTIF_TRACE_ERROR("%s: conn is not opened, conn:%p, conn->state:%d",
129                      __func__, conn, conn->state);
130     return;
131   }
132 
133   do {
134     /* read next data buffer from pan */
135     p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol, &ext, &forward);
136     if (p_buf) {
137       BTIF_TRACE_DEBUG(
138           "%s, calling btapp_tap_send, "
139           "p_buf->len:%d, offset:%d",
140           __func__, p_buf->len, p_buf->offset);
141       if (is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src)) {
142         VLOG(1) << __func__ << " pan bt peer addr: " << conn->peer
143                 << " update its ethernet addr: " << src;
144         conn->eth_addr = src;
145       }
146       btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol,
147                      (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext,
148                      forward);
149       osi_free(p_buf);
150     }
151 
152   } while (p_buf != NULL);
153 }
154 
155 /*******************************************************************************
156  *
157  * Function         bta_pan_co_rx_path
158  *
159  * Description
160  *
161  *
162  *
163  *
164  * Returns          void
165  *
166  ******************************************************************************/
bta_pan_co_rx_path(UNUSED_ATTR uint16_t handle,UNUSED_ATTR uint8_t app_id)167 void bta_pan_co_rx_path(UNUSED_ATTR uint16_t handle,
168                         UNUSED_ATTR uint8_t app_id) {
169   BTIF_TRACE_API("bta_pan_co_rx_path not used");
170 }
171 
172 /*******************************************************************************
173  *
174  * Function         bta_pan_co_rx_flow
175  *
176  * Description      This function is called by PAN to enable or disable
177  *                  data flow on the RX path when it is configured to use
178  *                  a push interface.  If data flow is disabled the phone must
179  *                  not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf()
180  *                  until data flow is enabled again.
181  *
182  *
183  * Returns          void
184  *
185  ******************************************************************************/
bta_pan_co_rx_flow(UNUSED_ATTR uint16_t handle,UNUSED_ATTR uint8_t app_id,UNUSED_ATTR bool enable)186 void bta_pan_co_rx_flow(UNUSED_ATTR uint16_t handle, UNUSED_ATTR uint8_t app_id,
187                         UNUSED_ATTR bool enable) {
188   BTIF_TRACE_API("bta_pan_co_rx_flow, enabled:%d, not used", enable);
189   btpan_conn_t* conn = btpan_find_conn_handle(handle);
190   if (!conn || conn->state != PAN_STATE_OPEN) return;
191   btpan_set_flow_control(enable);
192 }
193 
194 /*******************************************************************************
195  *
196  * Function         bta_pan_co_filt_ind
197  *
198  * Description      protocol filter indication from peer device
199  *
200  * Returns          void
201  *
202  ******************************************************************************/
bta_pan_co_pfilt_ind(UNUSED_ATTR uint16_t handle,UNUSED_ATTR bool indication,UNUSED_ATTR tBTA_PAN_STATUS result,UNUSED_ATTR uint16_t len,UNUSED_ATTR uint8_t * p_filters)203 void bta_pan_co_pfilt_ind(UNUSED_ATTR uint16_t handle,
204                           UNUSED_ATTR bool indication,
205                           UNUSED_ATTR tBTA_PAN_STATUS result,
206                           UNUSED_ATTR uint16_t len,
207                           UNUSED_ATTR uint8_t* p_filters) {
208   BTIF_TRACE_API("bta_pan_co_pfilt_ind");
209 }
210 
211 /*******************************************************************************
212  *
213  * Function         bta_pan_co_mfilt_ind
214  *
215  * Description      multicast filter indication from peer device
216  *
217  * Returns          void
218  *
219  ******************************************************************************/
bta_pan_co_mfilt_ind(UNUSED_ATTR uint16_t handle,UNUSED_ATTR bool indication,UNUSED_ATTR tBTA_PAN_STATUS result,UNUSED_ATTR uint16_t len,UNUSED_ATTR uint8_t * p_filters)220 void bta_pan_co_mfilt_ind(UNUSED_ATTR uint16_t handle,
221                           UNUSED_ATTR bool indication,
222                           UNUSED_ATTR tBTA_PAN_STATUS result,
223                           UNUSED_ATTR uint16_t len,
224                           UNUSED_ATTR uint8_t* p_filters) {
225   BTIF_TRACE_API("bta_pan_co_mfilt_ind");
226 }
227