blob: a872236725d1c461d5b79b4625fe195855dc4df8 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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
Myles Watsonee96a3c2016-11-23 14:49:54 -080019/*******************************************************************************
The Android Open Source Project5738f832012-12-12 16:00:35 -080020 *
21 * Filename: btif_pan.c
22 *
23 * Description: PAN Profile Bluetooth Interface
24 *
25 *
Myles Watsonee96a3c2016-11-23 14:49:54 -080026 ******************************************************************************/
Marie Janssen49120dc2015-07-07 16:47:20 -070027
28#define LOG_TAG "bt_btif_pan"
29
Jack Hef2af1c42016-12-13 01:59:12 -080030#include <base/logging.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include <ctype.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080032#include <errno.h>
33#include <fcntl.h>
Marie Janssendb554582015-06-26 14:53:46 -070034#include <linux/if_ether.h>
35#include <linux/if_tun.h>
36#include <linux/sockios.h>
37#include <net/if.h>
38#include <netdb.h>
39#include <netinet/in.h>
40#include <signal.h>
41#include <stdio.h>
42#include <string.h>
Marie Janssendb554582015-06-26 14:53:46 -070043#include <sys/ioctl.h>
44#include <sys/poll.h>
45#include <sys/prctl.h>
46#include <sys/select.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080047#include <sys/socket.h>
48#include <sys/wait.h>
Arman Ugurayd30195c2015-05-29 15:27:58 -070049#include <unistd.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080050
Marie Janssendb554582015-06-26 14:53:46 -070051#include <hardware/bluetooth.h>
52#include <hardware/bt_pan.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080053
Myles Watson6bd442f2016-10-19 09:50:22 -070054#include "bt_common.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080055#include "bta_api.h"
56#include "bta_pan_api.h"
Marie Janssendb554582015-06-26 14:53:46 -070057#include "btcore/include/bdaddr.h"
58#include "btif_common.h"
59#include "btif_pan_internal.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080060#include "btif_sock_thread.h"
61#include "btif_sock_util.h"
Marie Janssendb554582015-06-26 14:53:46 -070062#include "btif_util.h"
63#include "btm_api.h"
64#include "device/include/controller.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080065#include "osi/include/log.h"
Marie Janssendb554582015-06-26 14:53:46 -070066#include "osi/include/osi.h"
67
Myles Watson6bd442f2016-10-19 09:50:22 -070068#define FORWARD_IGNORE 1
69#define FORWARD_SUCCESS 0
70#define FORWARD_FAILURE (-1)
71#define FORWARD_CONGEST (-2)
The Android Open Source Project5738f832012-12-12 16:00:35 -080072
Marie Janssenb7f64bc2016-06-22 12:52:19 -070073#if (PAN_NAP_DISABLED == TRUE && PANU_DISABLED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -080074#define BTPAN_LOCAL_ROLE BTPAN_ROLE_NONE
75#elif PAN_NAP_DISABLED == TRUE
76#define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANU
77#elif PANU_DISABLED == TRUE
78#define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANNAP
79#else
80#define BTPAN_LOCAL_ROLE (BTPAN_ROLE_PANU | BTPAN_ROLE_PANNAP)
81#endif
82
Myles Watson40cde562016-10-21 09:39:13 -070083#define asrt(s) \
84 do { \
85 if (!(s)) \
86 BTIF_TRACE_ERROR("btif_pan: ## %s assert %s failed at line:%d ##", \
87 __func__, #s, __LINE__) \
88 } while (0)
The Android Open Source Project5738f832012-12-12 16:00:35 -080089
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -070090#define MIN(x, y) (((x) < (y)) ? (x) : (y))
91
The Android Open Source Project5738f832012-12-12 16:00:35 -080092btpan_cb_t btpan_cb;
93
Andre Eisenbach59606cc2015-05-05 11:39:28 -070094static bool jni_initialized;
95static bool stack_initialized;
96
The Android Open Source Project5738f832012-12-12 16:00:35 -080097static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks);
98static void btpan_jni_cleanup();
Jakub Pawlowskia484a882017-06-24 17:30:18 -070099static bt_status_t btpan_connect(const RawAddress* bd_addr, int local_role,
Myles Watson6bd442f2016-10-19 09:50:22 -0700100 int remote_role);
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700101static bt_status_t btpan_disconnect(const RawAddress* bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800102static bt_status_t btpan_enable(int local_role);
103static int btpan_get_local_role(void);
104
Myles Watson6bd442f2016-10-19 09:50:22 -0700105static void btpan_tap_fd_signaled(int fd, int type, int flags,
106 uint32_t user_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800107static void btpan_cleanup_conn(btpan_conn_t* conn);
Myles Watson6bd442f2016-10-19 09:50:22 -0700108static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN* p_data);
109static void btu_exec_tap_fd_read(void* p_param);
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700110
The Android Open Source Project5738f832012-12-12 16:00:35 -0800111static btpan_interface_t pan_if = {
Myles Watson6bd442f2016-10-19 09:50:22 -0700112 sizeof(pan_if), btpan_jni_init, btpan_enable, btpan_get_local_role,
113 btpan_connect, btpan_disconnect, btpan_jni_cleanup};
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700114
Myles Watson6bd442f2016-10-19 09:50:22 -0700115btpan_interface_t* btif_pan_get_interface() { return &pan_if; }
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700116
117/*******************************************************************************
118 **
119 ** Function btif_pan_init
120 **
121 ** Description initializes the pan interface
122 **
123 ** Returns bt_status_t
124 **
Myles Watsonee96a3c2016-11-23 14:49:54 -0800125 ******************************************************************************/
Myles Watson6bd442f2016-10-19 09:50:22 -0700126void btif_pan_init() {
127 BTIF_TRACE_DEBUG("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized,
128 btpan_cb.enabled);
129 stack_initialized = true;
Andre Eisenbach59606cc2015-05-05 11:39:28 -0700130
Myles Watson6bd442f2016-10-19 09:50:22 -0700131 if (jni_initialized && !btpan_cb.enabled) {
132 BTIF_TRACE_DEBUG("Enabling PAN....");
133 memset(&btpan_cb, 0, sizeof(btpan_cb));
134 btpan_cb.tap_fd = INVALID_FD;
135 btpan_cb.flow = 1;
Andre Eisenbach59606cc2015-05-05 11:39:28 -0700136 for (int i = 0; i < MAX_PAN_CONNS; i++)
Myles Watson6bd442f2016-10-19 09:50:22 -0700137 btpan_cleanup_conn(&btpan_cb.conns[i]);
138 BTA_PanEnable(bta_pan_callback);
139 btpan_cb.enabled = 1;
140 btpan_enable(BTPAN_LOCAL_ROLE);
141 }
142}
Andre Eisenbach59606cc2015-05-05 11:39:28 -0700143
Myles Watson6bd442f2016-10-19 09:50:22 -0700144static void pan_disable() {
145 if (btpan_cb.enabled) {
146 btpan_cb.enabled = 0;
147 BTA_PanDisable();
148 if (btpan_cb.tap_fd != INVALID_FD) {
149 btpan_tap_close(btpan_cb.tap_fd);
150 btpan_cb.tap_fd = INVALID_FD;
151 }
152 }
153}
154
155void btif_pan_cleanup() {
156 if (!stack_initialized) return;
157
158 // Bluetooth is shuting down, invalidate all BTA PAN handles
159 for (int i = 0; i < MAX_PAN_CONNS; i++)
160 btpan_cleanup_conn(&btpan_cb.conns[i]);
161
162 pan_disable();
163 stack_initialized = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800164}
165
166static btpan_callbacks_t callback;
Myles Watson6bd442f2016-10-19 09:50:22 -0700167static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks) {
168 BTIF_TRACE_DEBUG("stack_initialized = %d, btpan_cb.enabled:%d",
169 stack_initialized, btpan_cb.enabled);
170 callback = *callbacks;
171 jni_initialized = true;
172 if (stack_initialized && !btpan_cb.enabled) btif_pan_init();
173 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174}
175
Myles Watson6bd442f2016-10-19 09:50:22 -0700176static void btpan_jni_cleanup() {
177 pan_disable();
178 jni_initialized = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800179}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700180
Myles Watson6bd442f2016-10-19 09:50:22 -0700181static inline int bta_role_to_btpan(int bta_pan_role) {
182 int btpan_role = 0;
183 BTIF_TRACE_DEBUG("bta_pan_role:0x%x", bta_pan_role);
184 if (bta_pan_role & PAN_ROLE_NAP_SERVER) btpan_role |= BTPAN_ROLE_PANNAP;
185 if (bta_pan_role & PAN_ROLE_CLIENT) btpan_role |= BTPAN_ROLE_PANU;
186 return btpan_role;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800187}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700188
Myles Watson6bd442f2016-10-19 09:50:22 -0700189static inline int btpan_role_to_bta(int btpan_role) {
190 int bta_pan_role = PAN_ROLE_INACTIVE;
191 BTIF_TRACE_DEBUG("btpan_role:0x%x", btpan_role);
192 if (btpan_role & BTPAN_ROLE_PANNAP) bta_pan_role |= PAN_ROLE_NAP_SERVER;
193 if (btpan_role & BTPAN_ROLE_PANU) bta_pan_role |= PAN_ROLE_CLIENT;
194 return bta_pan_role;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800195}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700196
The Android Open Source Project5738f832012-12-12 16:00:35 -0800197static volatile int btpan_dev_local_role;
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700198#if (BTA_PAN_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY};
Myles Watson6bd442f2016-10-19 09:50:22 -0700200static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1,
201 PAN_SECURITY};
Pavlin Radoslavova9ea43b2016-02-15 11:47:37 -0800202#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203
Myles Watson6bd442f2016-10-19 09:50:22 -0700204static bt_status_t btpan_enable(int local_role) {
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700205#if (BTA_PAN_INCLUDED == TRUE)
Myles Watson6bd442f2016-10-19 09:50:22 -0700206 BTIF_TRACE_DEBUG("%s - local_role: %d", __func__, local_role);
207 int bta_pan_role = btpan_role_to_bta(local_role);
208 BTA_PanSetRole(bta_pan_role, &bta_panu_info, NULL, &bta_pan_nap_info);
209 btpan_dev_local_role = local_role;
210 return BT_STATUS_SUCCESS;
Mike J. Chen3576c562014-04-01 12:54:27 -0700211#else
Myles Watson6bd442f2016-10-19 09:50:22 -0700212 return BT_STATUS_FAIL;
Mike J. Chen3576c562014-04-01 12:54:27 -0700213#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800214}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700215
Myles Watson6bd442f2016-10-19 09:50:22 -0700216static int btpan_get_local_role() {
217 BTIF_TRACE_DEBUG("btpan_dev_local_role:%d", btpan_dev_local_role);
218 return btpan_dev_local_role;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700220
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700221static bt_status_t btpan_connect(const RawAddress* bd_addr, int local_role,
Myles Watson6bd442f2016-10-19 09:50:22 -0700222 int remote_role) {
223 BTIF_TRACE_DEBUG("local_role:%d, remote_role:%d", local_role, remote_role);
224 int bta_local_role = btpan_role_to_bta(local_role);
225 int bta_remote_role = btpan_role_to_bta(remote_role);
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700226 btpan_new_conn(-1, *bd_addr, bta_local_role, bta_remote_role);
227 BTA_PanOpen(*bd_addr, bta_local_role, bta_remote_role);
Myles Watson6bd442f2016-10-19 09:50:22 -0700228 return BT_STATUS_SUCCESS;
229}
230
231static void btif_in_pan_generic_evt(uint16_t event, char* p_param) {
232 BTIF_TRACE_EVENT("%s: event=%d", __func__, event);
233 switch (event) {
234 case BTIF_PAN_CB_DISCONNECTING: {
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700235 RawAddress* bd_addr = (RawAddress*)p_param;
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700236 btpan_conn_t* conn = btpan_find_conn_addr(*bd_addr);
Myles Watson6bd442f2016-10-19 09:50:22 -0700237 int btpan_conn_local_role;
238 int btpan_remote_role;
239 asrt(conn != NULL);
240 if (conn) {
241 btpan_conn_local_role = bta_role_to_btpan(conn->local_role);
242 btpan_remote_role = bta_role_to_btpan(conn->remote_role);
243 callback.connection_state_cb(BTPAN_STATE_DISCONNECTING,
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700244 BT_STATUS_SUCCESS, &conn->peer,
Myles Watson6bd442f2016-10-19 09:50:22 -0700245 btpan_conn_local_role, btpan_remote_role);
246 }
247 } break;
248 default: {
249 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event);
250 } break;
251 }
252}
253
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700254static bt_status_t btpan_disconnect(const RawAddress* bd_addr) {
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700255 btpan_conn_t* conn = btpan_find_conn_addr(*bd_addr);
Myles Watson6bd442f2016-10-19 09:50:22 -0700256 if (conn && conn->handle >= 0) {
257 /* Inform the application that the disconnect has been initiated
258 * successfully */
259 btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700260 (char*)bd_addr, sizeof(RawAddress), NULL);
Myles Watson6bd442f2016-10-19 09:50:22 -0700261 BTA_PanClose(conn->handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800262 return BT_STATUS_SUCCESS;
Myles Watson6bd442f2016-10-19 09:50:22 -0700263 }
264 return BT_STATUS_FAIL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700266
267static int pan_pth = -1;
Myles Watson6bd442f2016-10-19 09:50:22 -0700268void create_tap_read_thread(int tap_fd) {
269 if (pan_pth < 0) pan_pth = btsock_thread_create(btpan_tap_fd_signaled, NULL);
270 if (pan_pth >= 0)
271 btsock_thread_add_fd(pan_pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700273
Myles Watson6bd442f2016-10-19 09:50:22 -0700274void destroy_tap_read_thread(void) {
275 if (pan_pth >= 0) {
276 btsock_thread_exit(pan_pth);
277 pan_pth = -1;
278 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800279}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700280
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700281static int tap_if_up(const char* devname, const RawAddress* addr) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700282 struct ifreq ifr;
283 int sk, err;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800284
Myles Watson6bd442f2016-10-19 09:50:22 -0700285 sk = socket(AF_INET, SOCK_DGRAM, 0);
286 if (sk < 0) return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800287
Myles Watson6bd442f2016-10-19 09:50:22 -0700288 // set mac addr
289 memset(&ifr, 0, sizeof(ifr));
290 strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1);
291 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
292 if (err < 0) {
293 BTIF_TRACE_ERROR(
294 "Could not get network hardware for interface:%s, errno:%s", devname,
295 strerror(errno));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296 close(sk);
Myles Watson6bd442f2016-10-19 09:50:22 -0700297 return -1;
298 }
299
300 strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1);
301 memcpy(ifr.ifr_hwaddr.sa_data, addr->address, 6);
302
303 /* The IEEE has specified that the most significant bit of the most
304 * significant byte is used to
305 * determine a multicast address. If its a 1, that means multicast, 0 means
306 * unicast.
307 * Kernel returns an error if we try to set a multicast address for the
308 * tun-tap ethernet interface.
309 * Mask this bit to avoid any issue with auto generated address.
310 */
311 if (ifr.ifr_hwaddr.sa_data[0] & 0x01) {
312 BTIF_TRACE_WARNING(
313 "Not a unicast MAC address, force multicast bit flipping");
314 ifr.ifr_hwaddr.sa_data[0] &= ~0x01;
315 }
316
317 err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr);
318
319 if (err < 0) {
320 BTIF_TRACE_ERROR("Could not set bt address for interface:%s, errno:%s",
321 devname, strerror(errno));
322 close(sk);
323 return -1;
324 }
325
326 // bring it up
327 memset(&ifr, 0, sizeof(ifr));
328 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
329
330 ifr.ifr_flags |= IFF_UP;
331 ifr.ifr_flags |= IFF_MULTICAST;
332
333 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t)&ifr);
334
335 if (err < 0) {
336 BTIF_TRACE_ERROR("Could not bring up network interface:%s, errno:%d",
337 devname, errno);
338 close(sk);
339 return -1;
340 }
341 close(sk);
342 BTIF_TRACE_DEBUG("network interface: %s is up", devname);
343 return 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800344}
345
Myles Watson6bd442f2016-10-19 09:50:22 -0700346static int tap_if_down(const char* devname) {
347 struct ifreq ifr;
348 int sk;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800349
Myles Watson6bd442f2016-10-19 09:50:22 -0700350 sk = socket(AF_INET, SOCK_DGRAM, 0);
351 if (sk < 0) return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352
Myles Watson6bd442f2016-10-19 09:50:22 -0700353 memset(&ifr, 0, sizeof(ifr));
354 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355
Myles Watson6bd442f2016-10-19 09:50:22 -0700356 ifr.ifr_flags &= ~IFF_UP;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800357
Myles Watson6bd442f2016-10-19 09:50:22 -0700358 ioctl(sk, SIOCSIFFLAGS, (caddr_t)&ifr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359
Myles Watson6bd442f2016-10-19 09:50:22 -0700360 close(sk);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361
Myles Watson6bd442f2016-10-19 09:50:22 -0700362 return 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800363}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700364
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700365void btpan_set_flow_control(bool enable) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700366 if (btpan_cb.tap_fd == -1) return;
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700367
Myles Watson6bd442f2016-10-19 09:50:22 -0700368 btpan_cb.flow = enable;
369 if (enable) {
370 btsock_thread_add_fd(pan_pth, btpan_cb.tap_fd, 0, SOCK_THREAD_FD_RD, 0);
371 bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(btpan_cb.tap_fd));
372 }
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700373}
374
Myles Watson6bd442f2016-10-19 09:50:22 -0700375int btpan_tap_open() {
376 struct ifreq ifr;
377 int fd, err;
378 const char* clonedev = "/dev/tun";
The Android Open Source Project5738f832012-12-12 16:00:35 -0800379
Myles Watson6bd442f2016-10-19 09:50:22 -0700380 /* open the clone device */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800381
Marie Janssenf33b6f42016-11-22 15:01:42 -0800382 fd = open(clonedev, O_RDWR);
383 if (fd < 0) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700384 BTIF_TRACE_DEBUG("could not open %s, err:%d", clonedev, errno);
385 return fd;
386 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800387
Myles Watson6bd442f2016-10-19 09:50:22 -0700388 memset(&ifr, 0, sizeof(ifr));
389 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800390
Myles Watson6bd442f2016-10-19 09:50:22 -0700391 strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800392
Myles Watson6bd442f2016-10-19 09:50:22 -0700393 /* try to create the device */
Myles Watson911d1ae2016-11-28 16:44:40 -0800394 err = ioctl(fd, TUNSETIFF, (void*)&ifr);
Marie Janssenf33b6f42016-11-22 15:01:42 -0800395 if (err < 0) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700396 BTIF_TRACE_DEBUG("ioctl error:%d, errno:%s", err, strerror(errno));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800397 close(fd);
Myles Watson6bd442f2016-10-19 09:50:22 -0700398 return err;
399 }
400 if (tap_if_up(TAP_IF_NAME, controller_get_interface()->get_address()) == 0) {
401 int flags = fcntl(fd, F_GETFL, 0);
402 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
403 return fd;
404 }
405 BTIF_TRACE_ERROR("can not bring up tap interface:%s", TAP_IF_NAME);
406 close(fd);
407 return INVALID_FD;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800408}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700409
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700410int btpan_tap_send(int tap_fd, const RawAddress& src, const RawAddress& dst,
Myles Watson90088882016-11-15 16:33:22 -0800411 uint16_t proto, const char* buf, uint16_t len,
412 UNUSED_ATTR bool ext, UNUSED_ATTR bool forward) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700413 if (tap_fd != INVALID_FD) {
414 tETH_HDR eth_hdr;
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700415 eth_hdr.h_dest = dst;
416 eth_hdr.h_src = src;
Myles Watson6bd442f2016-10-19 09:50:22 -0700417 eth_hdr.h_proto = htons(proto);
418 char packet[TAP_MAX_PKT_WRITE_LEN + sizeof(tETH_HDR)];
419 memcpy(packet, &eth_hdr, sizeof(tETH_HDR));
420 if (len > TAP_MAX_PKT_WRITE_LEN) {
421 LOG_ERROR(LOG_TAG, "btpan_tap_send eth packet size:%d is exceeded limit!",
422 len);
423 return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800424 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700425 memcpy(packet + sizeof(tETH_HDR), buf, len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800426
Myles Watson6bd442f2016-10-19 09:50:22 -0700427 /* Send data to network interface */
428 ssize_t ret;
429 OSI_NO_INTR(ret = write(tap_fd, packet, len + sizeof(tETH_HDR)));
430 BTIF_TRACE_DEBUG("ret:%d", ret);
431 return (int)ret;
432 }
433 return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700435
Myles Watson6bd442f2016-10-19 09:50:22 -0700436int btpan_tap_close(int fd) {
437 if (tap_if_down(TAP_IF_NAME) == 0) close(fd);
438 if (pan_pth >= 0) btsock_thread_wakeup(pan_pth);
439 return 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800440}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700441
Myles Watson6bd442f2016-10-19 09:50:22 -0700442btpan_conn_t* btpan_find_conn_handle(uint16_t handle) {
443 for (int i = 0; i < MAX_PAN_CONNS; i++) {
444 if (btpan_cb.conns[i].handle == handle) return &btpan_cb.conns[i];
445 }
446 return NULL;
447}
448
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700449btpan_conn_t* btpan_find_conn_addr(const RawAddress& addr) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700450 for (int i = 0; i < MAX_PAN_CONNS; i++) {
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700451 if (btpan_cb.conns[i].peer == addr) return &btpan_cb.conns[i];
Myles Watson6bd442f2016-10-19 09:50:22 -0700452 }
453 return NULL;
454}
455
456static void btpan_open_conn(btpan_conn_t* conn, tBTA_PAN* p_data) {
457 BTIF_TRACE_API(
458 "btpan_open_conn: local_role:%d, peer_role: %d, handle:%d, conn: %p",
459 p_data->open.local_role, p_data->open.peer_role, p_data->open.handle,
460 conn);
461
462 if (conn == NULL)
463 conn = btpan_new_conn(p_data->open.handle, p_data->open.bd_addr,
464 p_data->open.local_role, p_data->open.peer_role);
465 if (conn) {
466 BTIF_TRACE_DEBUG(
467 "btpan_open_conn:tap_fd:%d, open_count:%d, "
468 "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d",
469 btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, p_data->open.handle,
470 conn->local_role, conn->remote_role);
471
472 btpan_cb.open_count++;
473 conn->handle = p_data->open.handle;
474 if (btpan_cb.tap_fd < 0) {
475 btpan_cb.tap_fd = btpan_tap_open();
476 if (btpan_cb.tap_fd >= 0) create_tap_read_thread(btpan_cb.tap_fd);
Andre Eisenbach59606cc2015-05-05 11:39:28 -0700477 }
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700478
Myles Watson6bd442f2016-10-19 09:50:22 -0700479 if (btpan_cb.tap_fd >= 0) {
480 btpan_cb.flow = 1;
481 conn->state = PAN_STATE_OPEN;
Andre Eisenbach59606cc2015-05-05 11:39:28 -0700482 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700483 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800484}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700485
Myles Watson6bd442f2016-10-19 09:50:22 -0700486static void btpan_close_conn(btpan_conn_t* conn) {
487 BTIF_TRACE_API("btpan_close_conn: %p", conn);
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530488
Myles Watson6bd442f2016-10-19 09:50:22 -0700489 if (conn && conn->state == PAN_STATE_OPEN) {
490 BTIF_TRACE_DEBUG("btpan_close_conn: PAN_STATE_OPEN");
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530491
Myles Watson6bd442f2016-10-19 09:50:22 -0700492 conn->state = PAN_STATE_CLOSE;
493 btpan_cb.open_count--;
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530494
Myles Watson6bd442f2016-10-19 09:50:22 -0700495 if (btpan_cb.open_count == 0) {
496 destroy_tap_read_thread();
497 if (btpan_cb.tap_fd != INVALID_FD) {
498 btpan_tap_close(btpan_cb.tap_fd);
499 btpan_cb.tap_fd = INVALID_FD;
500 }
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530501 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700502 }
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530503}
504
Myles Watson6bd442f2016-10-19 09:50:22 -0700505static void btpan_cleanup_conn(btpan_conn_t* conn) {
506 if (conn) {
507 conn->handle = -1;
508 conn->state = -1;
509 memset(&conn->peer, 0, sizeof(conn->peer));
510 memset(&conn->eth_addr, 0, sizeof(conn->eth_addr));
511 conn->local_role = conn->remote_role = 0;
512 }
513}
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530514
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700515btpan_conn_t* btpan_new_conn(int handle, const RawAddress& addr, int local_role,
516 int remote_role) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700517 for (int i = 0; i < MAX_PAN_CONNS; i++) {
518 BTIF_TRACE_DEBUG("conns[%d]:%d", i, btpan_cb.conns[i].handle);
519 if (btpan_cb.conns[i].handle == -1) {
520 BTIF_TRACE_DEBUG("handle:%d, local_role:%d, remote_role:%d", handle,
521 local_role, remote_role);
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530522
Myles Watson6bd442f2016-10-19 09:50:22 -0700523 btpan_cb.conns[i].handle = handle;
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700524 btpan_cb.conns[i].peer = addr;
Myles Watson6bd442f2016-10-19 09:50:22 -0700525 btpan_cb.conns[i].local_role = local_role;
526 btpan_cb.conns[i].remote_role = remote_role;
527 return &btpan_cb.conns[i];
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530528 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700529 }
530 BTIF_TRACE_DEBUG("MAX_PAN_CONNS:%d exceeded, return NULL as failed",
531 MAX_PAN_CONNS);
532 return NULL;
Nitin Shivpurebb2a4fa2015-09-06 15:04:01 +0530533}
534
Myles Watson6bd442f2016-10-19 09:50:22 -0700535void btpan_close_handle(btpan_conn_t* p) {
536 BTIF_TRACE_DEBUG("btpan_close_handle : close handle %d", p->handle);
537 p->handle = -1;
538 p->local_role = -1;
539 p->remote_role = -1;
540 memset(&p->peer, 0, 6);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800541}
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700542
Myles Watson6bd442f2016-10-19 09:50:22 -0700543static inline bool should_forward(tETH_HDR* hdr) {
544 uint16_t proto = ntohs(hdr->h_proto);
545 if (proto == ETH_P_IP || proto == ETH_P_ARP || proto == ETH_P_IPV6)
546 return true;
547 BTIF_TRACE_DEBUG("unknown proto:%x", proto);
548 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800549}
550
Myles Watson6bd442f2016-10-19 09:50:22 -0700551static int forward_bnep(tETH_HDR* eth_hdr, BT_HDR* hdr) {
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700552 int broadcast = eth_hdr->h_dest.address[0] & 1;
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700553
Myles Watson6bd442f2016-10-19 09:50:22 -0700554 // Find the right connection to send this frame over.
555 for (int i = 0; i < MAX_PAN_CONNS; i++) {
556 uint16_t handle = btpan_cb.conns[i].handle;
557 if (handle != (uint16_t)-1 &&
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700558 (broadcast || btpan_cb.conns[i].eth_addr == eth_hdr->h_dest ||
559 btpan_cb.conns[i].peer == eth_hdr->h_dest)) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700560 int result = PAN_WriteBuf(handle, eth_hdr->h_dest, eth_hdr->h_src,
561 ntohs(eth_hdr->h_proto), hdr, 0);
562 switch (result) {
563 case PAN_Q_SIZE_EXCEEDED:
564 return FORWARD_CONGEST;
565 case PAN_SUCCESS:
566 return FORWARD_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567 default:
Myles Watson6bd442f2016-10-19 09:50:22 -0700568 return FORWARD_FAILURE;
569 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800570 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700571 }
572 osi_free(hdr);
573 return FORWARD_IGNORE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800574}
575
Myles Watson6bd442f2016-10-19 09:50:22 -0700576static void bta_pan_callback_transfer(uint16_t event, char* p_param) {
577 tBTA_PAN* p_data = (tBTA_PAN*)p_param;
578
579 switch (event) {
580 case BTA_PAN_ENABLE_EVT:
581 BTIF_TRACE_DEBUG("BTA_PAN_ENABLE_EVT");
582 break;
583 case BTA_PAN_SET_ROLE_EVT: {
584 int btpan_role = bta_role_to_btpan(p_data->set_role.role);
585 bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS
586 ? BT_STATUS_SUCCESS
587 : BT_STATUS_FAIL;
588 btpan_control_state_t state =
589 btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED;
590 callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME);
591 break;
592 }
593 case BTA_PAN_OPENING_EVT: {
594 btpan_conn_t* conn;
595 bdstr_t bds;
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700596 bdaddr_to_string(&p_data->opening.bd_addr, bds, sizeof(bds));
Myles Watson6bd442f2016-10-19 09:50:22 -0700597 BTIF_TRACE_DEBUG("BTA_PAN_OPENING_EVT handle %d, addr: %s",
598 p_data->opening.handle, bds);
599 conn = btpan_find_conn_addr(p_data->opening.bd_addr);
600
601 asrt(conn != NULL);
602 if (conn) {
603 conn->handle = p_data->opening.handle;
604 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role);
605 int btpan_remote_role = bta_role_to_btpan(conn->remote_role);
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700606 callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS,
607 &p_data->opening.bd_addr,
608 btpan_conn_local_role, btpan_remote_role);
Myles Watson6bd442f2016-10-19 09:50:22 -0700609 } else
610 BTIF_TRACE_ERROR("connection not found");
611 break;
612 }
613 case BTA_PAN_OPEN_EVT: {
614 btpan_connection_state_t state;
615 bt_status_t status;
616 btpan_conn_t* conn = btpan_find_conn_handle(p_data->open.handle);
617
618 LOG_VERBOSE(LOG_TAG, "%s pan connection open status: %d", __func__,
619 p_data->open.status);
620 if (p_data->open.status == BTA_PAN_SUCCESS) {
621 state = BTPAN_STATE_CONNECTED;
622 status = BT_STATUS_SUCCESS;
623 btpan_open_conn(conn, p_data);
624 } else {
625 state = BTPAN_STATE_DISCONNECTED;
626 status = BT_STATUS_FAIL;
627 btpan_cleanup_conn(conn);
628 }
629 /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle,
630 * conn); */
631 /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role,
632 * conn->remote_role); */
633 int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role);
634 int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role);
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700635 callback.connection_state_cb(state, status, &p_data->open.bd_addr,
Myles Watson6bd442f2016-10-19 09:50:22 -0700636 btpan_conn_local_role, btpan_remote_role);
637 break;
638 }
639 case BTA_PAN_CLOSE_EVT: {
640 LOG_INFO(LOG_TAG, "%s: event = BTA_PAN_CLOSE_EVT handle %d", __func__,
641 p_data->close.handle);
642 btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle);
643 btpan_close_conn(conn);
644
645 if (conn && conn->handle >= 0) {
646 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role);
647 int btpan_remote_role = bta_role_to_btpan(conn->remote_role);
648 callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, (bt_status_t)0,
Jakub Pawlowskia83ac122017-06-15 11:53:33 -0700649 &conn->peer, btpan_conn_local_role,
650 btpan_remote_role);
Myles Watson6bd442f2016-10-19 09:50:22 -0700651 btpan_cleanup_conn(conn);
652 } else
653 BTIF_TRACE_ERROR("pan handle not found (%d)", p_data->close.handle);
654 break;
655 }
656 default:
657 BTIF_TRACE_WARNING("Unknown pan event %d", event);
658 break;
659 }
660}
661
662static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN* p_data) {
663 btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data,
664 sizeof(tBTA_PAN), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800665}
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800666
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700667#define IS_EXCEPTION(e) ((e) & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL))
Myles Watson6bd442f2016-10-19 09:50:22 -0700668static void btu_exec_tap_fd_read(void* p_param) {
669 struct pollfd ufd;
670 int fd = PTR_TO_INT(p_param);
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700671
Myles Watson6bd442f2016-10-19 09:50:22 -0700672 if (fd == INVALID_FD || fd != btpan_cb.tap_fd) return;
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700673
Myles Watson6bd442f2016-10-19 09:50:22 -0700674 // Don't occupy BTU context too long, avoid buffer overruns and
675 // give other profiles a chance to run by limiting the amount of memory
676 // PAN can use.
677 for (int i = 0; i < PAN_BUF_MAX && btif_is_enabled() && btpan_cb.flow; i++) {
678 BT_HDR* buffer = (BT_HDR*)osi_malloc(PAN_BUF_SIZE);
679 buffer->offset = PAN_MINIMUM_OFFSET;
680 buffer->len = PAN_BUF_SIZE - sizeof(BT_HDR) - buffer->offset;
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700681
Myles Watson6bd442f2016-10-19 09:50:22 -0700682 uint8_t* packet = (uint8_t*)buffer + sizeof(BT_HDR) + buffer->offset;
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700683
Myles Watson6bd442f2016-10-19 09:50:22 -0700684 // If we don't have an undelivered packet left over, pull one from the TAP
685 // driver.
686 // We save it in the congest_packet right away in case we can't deliver it
687 // in this
688 // attempt.
689 if (!btpan_cb.congest_packet_size) {
690 ssize_t ret;
691 OSI_NO_INTR(ret = read(fd, btpan_cb.congest_packet,
692 sizeof(btpan_cb.congest_packet)));
693 switch (ret) {
694 case -1:
695 BTIF_TRACE_ERROR("%s unable to read from driver: %s", __func__,
696 strerror(errno));
697 osi_free(buffer);
698 // add fd back to monitor thread to try it again later
699 btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0);
700 return;
701 case 0:
702 BTIF_TRACE_WARNING("%s end of file reached.", __func__);
703 osi_free(buffer);
704 // add fd back to monitor thread to process the exception
705 btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0);
706 return;
707 default:
708 btpan_cb.congest_packet_size = ret;
709 break;
710 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800711 }
Srinu Jella7baa1e12015-08-11 19:16:52 +0530712
Myles Watson6bd442f2016-10-19 09:50:22 -0700713 memcpy(packet, btpan_cb.congest_packet,
714 MIN(btpan_cb.congest_packet_size, buffer->len));
715 buffer->len = MIN(btpan_cb.congest_packet_size, buffer->len);
716
717 if (buffer->len > sizeof(tETH_HDR) && should_forward((tETH_HDR*)packet)) {
718 // Extract the ethernet header from the buffer since the PAN_WriteBuf
719 // inside
720 // forward_bnep can't handle two pointers that point inside the same GKI
721 // buffer.
722 tETH_HDR hdr;
723 memcpy(&hdr, packet, sizeof(tETH_HDR));
724
725 // Skip the ethernet header.
726 buffer->len -= sizeof(tETH_HDR);
727 buffer->offset += sizeof(tETH_HDR);
728 if (forward_bnep(&hdr, buffer) != FORWARD_CONGEST)
729 btpan_cb.congest_packet_size = 0;
730 } else {
731 BTIF_TRACE_WARNING("%s dropping packet of length %d", __func__,
732 buffer->len);
733 btpan_cb.congest_packet_size = 0;
734 osi_free(buffer);
Srinu Jella7baa1e12015-08-11 19:16:52 +0530735 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700736
737 // Bail out of the loop if reading from the TAP fd would block.
738 ufd.fd = fd;
739 ufd.events = POLLIN;
740 ufd.revents = 0;
741
742 int ret;
743 OSI_NO_INTR(ret = poll(&ufd, 1, 0));
744 if (ret <= 0 || IS_EXCEPTION(ufd.revents)) break;
745 }
746
747 if (btpan_cb.flow) {
748 // add fd back to monitor thread when the flow is on
749 btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0);
750 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800751}
752
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700753static void btif_pan_close_all_conns() {
Myles Watson6bd442f2016-10-19 09:50:22 -0700754 if (!stack_initialized) return;
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700755
Myles Watson6bd442f2016-10-19 09:50:22 -0700756 for (int i = 0; i < MAX_PAN_CONNS; ++i) {
757 if (btpan_cb.conns[i].handle != -1) BTA_PanClose(btpan_cb.conns[i].handle);
758 }
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700759}
760
Myles Watson6bd442f2016-10-19 09:50:22 -0700761static void btpan_tap_fd_signaled(int fd, int type, int flags,
762 uint32_t user_id) {
Jack Hef2af1c42016-12-13 01:59:12 -0800763 CHECK(btpan_cb.tap_fd == INVALID_FD || btpan_cb.tap_fd == fd);
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700764
Myles Watson6bd442f2016-10-19 09:50:22 -0700765 if (btpan_cb.tap_fd != fd) {
766 BTIF_TRACE_WARNING("%s Signaled on mismatched fds exp:%d act:%d\n",
767 __func__, btpan_cb.tap_fd, fd);
768 return;
769 }
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700770
Myles Watson6bd442f2016-10-19 09:50:22 -0700771 if (flags & SOCK_THREAD_FD_EXCEPTION) {
772 btpan_cb.tap_fd = INVALID_FD;
773 btpan_tap_close(fd);
774 btif_pan_close_all_conns();
775 } else if (flags & SOCK_THREAD_FD_RD)
776 bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(fd));
Sharvil Nanavati4186b0e2014-04-07 13:52:37 -0700777}