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