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