btif_pan.c revision d30195cbf09735da593e186c4c99cce6232b072e
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: btif_pan.c 22 * 23 * Description: PAN Profile Bluetooth Interface 24 * 25 * 26 ***********************************************************************************/ 27#include <hardware/bluetooth.h> 28#include <hardware/bt_pan.h> 29#include <assert.h> 30#include <string.h> 31#include <signal.h> 32#include <ctype.h> 33#include <sys/select.h> 34#include <sys/poll.h> 35#include <sys/ioctl.h> 36#include <netinet/in.h> 37#include <netdb.h> 38#include <stdio.h> 39#include <string.h> 40#include <errno.h> 41#include <fcntl.h> 42#include <sys/socket.h> 43#include <sys/wait.h> 44#include <net/if.h> 45#include <linux/sockios.h> 46#include <sys/prctl.h> 47#include <linux/if_tun.h> 48#include <linux/if_ether.h> 49#include <unistd.h> 50 51#define LOG_TAG "bt_btif_pan" 52#include "btif_common.h" 53#include "btif_util.h" 54#include "btm_api.h" 55#include "btcore/include/bdaddr.h" 56#include "device/include/controller.h" 57 58#include "bta_api.h" 59#include "bta_pan_api.h" 60#include "btif_sock_thread.h" 61#include "btif_sock_util.h" 62#include "btif_pan_internal.h" 63#include "gki.h" 64#include "osi/include/osi.h" 65#include "osi/include/log.h" 66 67#define FORWARD_IGNORE 1 68#define FORWARD_SUCCESS 0 69#define FORWARD_FAILURE (-1) 70#define FORWARD_CONGEST (-2) 71//#define PANU_DISABLED TRUE 72 73#if (PAN_NAP_DISABLED == TRUE) && (PANU_DISABLED == TRUE) 74#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 83#define asrt(s) if (!(s)) BTIF_TRACE_ERROR("btif_pan: ## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__) 84 85#define MIN(x, y) (((x) < (y)) ? (x) : (y)) 86 87btpan_cb_t btpan_cb; 88 89static bool jni_initialized; 90static bool stack_initialized; 91 92static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks); 93static void btpan_jni_cleanup(); 94static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role); 95static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr); 96static bt_status_t btpan_enable(int local_role); 97static int btpan_get_local_role(void); 98 99static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id); 100static void btpan_cleanup_conn(btpan_conn_t* conn); 101static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data); 102static void btu_exec_tap_fd_read(void *p_param); 103 104static btpan_interface_t pan_if = { 105 sizeof(pan_if), 106 btpan_jni_init, 107 btpan_enable, 108 btpan_get_local_role, 109 btpan_connect, 110 btpan_disconnect, 111 btpan_jni_cleanup 112}; 113 114btpan_interface_t *btif_pan_get_interface() 115{ 116 return &pan_if; 117} 118 119/******************************************************************************* 120 ** 121 ** Function btif_pan_init 122 ** 123 ** Description initializes the pan interface 124 ** 125 ** Returns bt_status_t 126 ** 127 *******************************************************************************/ 128void btif_pan_init() 129{ 130 BTIF_TRACE_DEBUG("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized, btpan_cb.enabled); 131 stack_initialized = true; 132 133 if (jni_initialized && !btpan_cb.enabled) 134 { 135 BTIF_TRACE_DEBUG("Enabling PAN...."); 136 memset(&btpan_cb, 0, sizeof(btpan_cb)); 137 btpan_cb.tap_fd = INVALID_FD; 138 btpan_cb.flow = 1; 139 for (int i = 0; i < MAX_PAN_CONNS; i++) 140 btpan_cleanup_conn(&btpan_cb.conns[i]); 141 BTA_PanEnable(bta_pan_callback); 142 btpan_cb.enabled = 1; 143 btpan_enable(BTPAN_LOCAL_ROLE); 144 } 145} 146 147static void pan_disable() 148{ 149 if (btpan_cb.enabled) 150 { 151 btpan_cb.enabled = 0; 152 BTA_PanDisable(); 153 if (btpan_cb.tap_fd != INVALID_FD) 154 { 155 btpan_tap_close(btpan_cb.tap_fd); 156 btpan_cb.tap_fd = INVALID_FD; 157 } 158 } 159} 160 161void btif_pan_cleanup() 162{ 163 if (!stack_initialized) 164 return; 165 166 // Bluetooth is shuting down, invalidate all BTA PAN handles 167 for (int i = 0; i < MAX_PAN_CONNS; i++) 168 btpan_cleanup_conn(&btpan_cb.conns[i]); 169 170 pan_disable(); 171 stack_initialized = false; 172} 173 174static btpan_callbacks_t callback; 175static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks) 176{ 177 BTIF_TRACE_DEBUG("stack_initialized = %d, btpan_cb.enabled:%d", stack_initialized, btpan_cb.enabled); 178 jni_initialized = true; 179 if (!stack_initialized && !btpan_cb.enabled) 180 btif_pan_init(); 181 callback = *callbacks; 182 return BT_STATUS_SUCCESS; 183} 184 185static void btpan_jni_cleanup() 186{ 187 pan_disable(); 188 jni_initialized = false; 189} 190 191static inline int bta_role_to_btpan(int bta_pan_role) 192{ 193 int btpan_role = 0; 194 BTIF_TRACE_DEBUG("bta_pan_role:0x%x", bta_pan_role); 195 if (bta_pan_role & PAN_ROLE_NAP_SERVER) 196 btpan_role |= BTPAN_ROLE_PANNAP; 197 if (bta_pan_role & PAN_ROLE_CLIENT) 198 btpan_role |= BTPAN_ROLE_PANU; 199 return btpan_role; 200} 201 202static inline int btpan_role_to_bta(int btpan_role) 203{ 204 int bta_pan_role = PAN_ROLE_INACTIVE; 205 BTIF_TRACE_DEBUG("btpan_role:0x%x", btpan_role); 206 if (btpan_role & BTPAN_ROLE_PANNAP) 207 bta_pan_role |= PAN_ROLE_NAP_SERVER; 208 if (btpan_role & BTPAN_ROLE_PANU) 209 bta_pan_role |= PAN_ROLE_CLIENT; 210 return bta_pan_role; 211} 212 213static volatile int btpan_dev_local_role; 214static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY}; 215static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1, PAN_SECURITY}; 216 217static bt_status_t btpan_enable(int local_role) 218{ 219#if BTA_PAN_INCLUDED == TRUE 220 BTIF_TRACE_DEBUG("%s - local_role: %d", __func__, local_role); 221 int bta_pan_role = btpan_role_to_bta(local_role); 222 BTA_PanSetRole(bta_pan_role, &bta_panu_info, NULL, &bta_pan_nap_info); 223 btpan_dev_local_role = local_role; 224 return BT_STATUS_SUCCESS; 225#else 226 return BT_STATUS_FAIL; 227#endif 228} 229 230static int btpan_get_local_role() 231{ 232 BTIF_TRACE_DEBUG("btpan_dev_local_role:%d", btpan_dev_local_role); 233 return btpan_dev_local_role; 234} 235 236static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role) 237{ 238 BTIF_TRACE_DEBUG("local_role:%d, remote_role:%d", local_role, remote_role); 239 int bta_local_role = btpan_role_to_bta(local_role); 240 int bta_remote_role = btpan_role_to_bta(remote_role); 241 btpan_new_conn(-1, bd_addr->address, bta_local_role, bta_remote_role); 242 BTA_PanOpen((UINT8*)bd_addr->address, bta_local_role, bta_remote_role); 243 return BT_STATUS_SUCCESS; 244} 245 246static void btif_in_pan_generic_evt(UINT16 event, char *p_param) 247{ 248 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event); 249 switch (event) { 250 case BTIF_PAN_CB_DISCONNECTING: 251 { 252 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)p_param; 253 btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); 254 int btpan_conn_local_role; 255 int btpan_remote_role; 256 asrt(conn != NULL); 257 if (conn) { 258 btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 259 btpan_remote_role = bta_role_to_btpan(conn->remote_role); 260 callback.connection_state_cb(BTPAN_STATE_DISCONNECTING, BT_STATUS_SUCCESS, 261 (const bt_bdaddr_t*)conn->peer, btpan_conn_local_role, btpan_remote_role); 262 } 263 } break; 264 default: 265 { 266 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event); 267 } 268 break; 269 } 270} 271 272static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr) 273{ 274 btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); 275 if (conn && conn->handle >= 0) 276 { 277 BTA_PanClose(conn->handle); 278 /* Inform the application that the disconnect has been initiated successfully */ 279 btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING, 280 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); 281 return BT_STATUS_SUCCESS; 282 } 283 return BT_STATUS_FAIL; 284} 285 286static int pan_pth = -1; 287void create_tap_read_thread(int tap_fd) 288{ 289 if (pan_pth < 0) 290 pan_pth = btsock_thread_create(btpan_tap_fd_signaled, NULL); 291 if (pan_pth >= 0) 292 btsock_thread_add_fd(pan_pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0); 293} 294 295void destroy_tap_read_thread(void) 296{ 297 if (pan_pth >= 0) 298 { 299 btsock_thread_exit(pan_pth); 300 pan_pth = -1; 301 } 302} 303 304static int tap_if_up(const char *devname, const bt_bdaddr_t *addr) 305{ 306 struct ifreq ifr; 307 int sk, err; 308 309 sk = socket(AF_INET, SOCK_DGRAM, 0); 310 if (sk < 0) 311 return -1; 312 313 //set mac addr 314 memset(&ifr, 0, sizeof(ifr)); 315 strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); 316 err = ioctl(sk, SIOCGIFHWADDR, &ifr); 317 if (err < 0) 318 { 319 BTIF_TRACE_ERROR("Could not get network hardware for interface:%s, errno:%s", devname, strerror(errno)); 320 close(sk); 321 return -1; 322 } 323 324 strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); 325 memcpy(ifr.ifr_hwaddr.sa_data, addr->address, 6); 326 327 /* The IEEE has specified that the most significant bit of the most significant byte is used to 328 * determine a multicast address. If its a 1, that means multicast, 0 means unicast. 329 * Kernel returns an error if we try to set a multicast address for the tun-tap ethernet interface. 330 * Mask this bit to avoid any issue with auto generated address. 331 */ 332 if (ifr.ifr_hwaddr.sa_data[0] & 0x01) { 333 BTIF_TRACE_WARNING("Not a unicast MAC address, force multicast bit flipping"); 334 ifr.ifr_hwaddr.sa_data[0] &= ~0x01; 335 } 336 337 err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr); 338 339 if (err < 0) { 340 BTIF_TRACE_ERROR("Could not set bt address for interface:%s, errno:%s", devname, strerror(errno)); 341 close(sk); 342 return -1; 343 } 344 345 //bring it up 346 memset(&ifr, 0, sizeof(ifr)); 347 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); 348 349 ifr.ifr_flags |= IFF_UP; 350 ifr.ifr_flags |= IFF_MULTICAST; 351 352 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); 353 354 355 if (err < 0) { 356 BTIF_TRACE_ERROR("Could not bring up network interface:%s, errno:%d", devname, errno); 357 close(sk); 358 return -1; 359 } 360 close(sk); 361 BTIF_TRACE_DEBUG("network interface: %s is up", devname); 362 return 0; 363} 364 365static int tap_if_down(const char *devname) 366{ 367 struct ifreq ifr; 368 int sk; 369 370 sk = socket(AF_INET, SOCK_DGRAM, 0); 371 if (sk < 0) 372 return -1; 373 374 memset(&ifr, 0, sizeof(ifr)); 375 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); 376 377 ifr.ifr_flags &= ~IFF_UP; 378 379 ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); 380 381 close(sk); 382 383 return 0; 384} 385 386void btpan_set_flow_control(BOOLEAN enable) { 387 if (btpan_cb.tap_fd == -1) 388 return; 389 390 btpan_cb.flow = enable; 391 if (enable) { 392 btsock_thread_add_fd(pan_pth, btpan_cb.tap_fd, 0, SOCK_THREAD_FD_RD, 0); 393 bta_dmexecutecallback(btu_exec_tap_fd_read, (void *)btpan_cb.tap_fd); 394 } 395} 396 397int btpan_tap_open() 398{ 399 struct ifreq ifr; 400 int fd, err; 401 const char *clonedev = "/dev/tun"; 402 403 /* open the clone device */ 404 405 if ((fd = open(clonedev, O_RDWR)) < 0) 406 { 407 BTIF_TRACE_DEBUG("could not open %s, err:%d", clonedev, errno); 408 return fd; 409 } 410 411 memset(&ifr, 0, sizeof(ifr)); 412 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 413 414 strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ); 415 416 /* try to create the device */ 417 if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) 418 { 419 BTIF_TRACE_DEBUG("ioctl error:%d, errno:%s", err, strerror(errno)); 420 close(fd); 421 return err; 422 } 423 if (tap_if_up(TAP_IF_NAME, controller_get_interface()->get_address()) == 0) 424 { 425 int flags = fcntl(fd, F_GETFL, 0); 426 fcntl(fd, F_SETFL, flags | O_NONBLOCK); 427 return fd; 428 } 429 BTIF_TRACE_ERROR("can not bring up tap interface:%s", TAP_IF_NAME); 430 close(fd); 431 return INVALID_FD; 432} 433 434int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, UINT16 proto, const char* buf, 435 UINT16 len, BOOLEAN ext, BOOLEAN forward) 436{ 437 UNUSED(ext); 438 UNUSED(forward); 439 if (tap_fd != INVALID_FD) 440 { 441 tETH_HDR eth_hdr; 442 memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN); 443 memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN); 444 eth_hdr.h_proto = htons(proto); 445 char packet[2000]; 446 memcpy(packet, ð_hdr, sizeof(tETH_HDR)); 447 if (len > 2000) 448 { 449 LOG_ERROR("btpan_tap_send eth packet size:%d is exceeded limit!", len); 450 return -1; 451 } 452 memcpy(packet + sizeof(tETH_HDR), buf, len); 453 454 /* Send data to network interface */ 455 int ret = write(tap_fd, packet, len + sizeof(tETH_HDR)); 456 BTIF_TRACE_DEBUG("ret:%d", ret); 457 return ret; 458 } 459 return -1; 460 461} 462 463int btpan_tap_close(int fd) 464{ 465 if (tap_if_down(TAP_IF_NAME) == 0) 466 close(fd); 467 if (pan_pth >= 0) 468 btsock_thread_wakeup(pan_pth); 469 return 0; 470} 471 472btpan_conn_t * btpan_find_conn_handle(UINT16 handle) 473{ 474 for (int i = 0; i < MAX_PAN_CONNS; i++) 475 { 476 if (btpan_cb.conns[i].handle == handle) 477 return &btpan_cb.conns[i]; 478 } 479 return NULL; 480} 481 482btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr) 483{ 484 for (int i = 0; i < MAX_PAN_CONNS; i++) 485 { 486 if (memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0) 487 return &btpan_cb.conns[i]; 488 } 489 return NULL; 490} 491 492static void btpan_cleanup_conn(btpan_conn_t* conn) 493{ 494 if (conn) 495 { 496 conn->handle = -1; 497 conn->state = -1; 498 memset(&conn->peer, 0, sizeof(conn->peer)); 499 memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); 500 conn->local_role = conn->remote_role = 0; 501 } 502} 503 504btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int remote_role) 505{ 506 for (int i = 0; i < MAX_PAN_CONNS; i++) 507 { 508 BTIF_TRACE_DEBUG("conns[%d]:%d", i, btpan_cb.conns[i].handle); 509 if (btpan_cb.conns[i].handle == -1) 510 { 511 BTIF_TRACE_DEBUG("handle:%d, local_role:%d, remote_role:%d", handle, local_role, remote_role); 512 513 btpan_cb.conns[i].handle = handle; 514 bdcpy(btpan_cb.conns[i].peer, addr); 515 btpan_cb.conns[i].local_role = local_role; 516 btpan_cb.conns[i].remote_role = remote_role; 517 return &btpan_cb.conns[i]; 518 } 519 } 520 BTIF_TRACE_DEBUG("MAX_PAN_CONNS:%d exceeded, return NULL as failed", MAX_PAN_CONNS); 521 return NULL; 522} 523 524void btpan_close_handle(btpan_conn_t *p) 525{ 526 BTIF_TRACE_DEBUG("btpan_close_handle : close handle %d", p->handle); 527 p->handle = -1; 528 p->local_role = -1; 529 p->remote_role = -1; 530 memset(&p->peer, 0, 6); 531} 532 533static inline bool should_forward(tETH_HDR* hdr) 534{ 535 uint16_t proto = ntohs(hdr->h_proto); 536 if (proto == ETH_P_IP || proto == ETH_P_ARP || proto == ETH_P_IPV6) 537 return true; 538 BTIF_TRACE_DEBUG("unknown proto:%x", proto); 539 return false; 540} 541 542static int forward_bnep(tETH_HDR* eth_hdr, BT_HDR *hdr) { 543 int broadcast = eth_hdr->h_dest[0] & 1; 544 545 // Find the right connection to send this frame over. 546 for (int i = 0; i < MAX_PAN_CONNS; i++) 547 { 548 UINT16 handle = btpan_cb.conns[i].handle; 549 if (handle != (UINT16)-1 && 550 (broadcast || memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0 551 || memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0)) { 552 int result = PAN_WriteBuf(handle, eth_hdr->h_dest, eth_hdr->h_src, ntohs(eth_hdr->h_proto), hdr, 0); 553 switch (result) { 554 case PAN_Q_SIZE_EXCEEDED: 555 return FORWARD_CONGEST; 556 case PAN_SUCCESS: 557 return FORWARD_SUCCESS; 558 default: 559 return FORWARD_FAILURE; 560 } 561 } 562 } 563 GKI_freebuf(hdr); 564 return FORWARD_IGNORE; 565} 566 567static void bta_pan_callback_transfer(UINT16 event, char *p_param) 568{ 569 tBTA_PAN *p_data = (tBTA_PAN *)p_param; 570 571 switch(event) 572 { 573 case BTA_PAN_ENABLE_EVT: 574 BTIF_TRACE_DEBUG("BTA_PAN_ENABLE_EVT"); 575 break; 576 case BTA_PAN_SET_ROLE_EVT: 577 { 578 int btpan_role = bta_role_to_btpan(p_data->set_role.role); 579 bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 580 btpan_control_state_t state = btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; 581 callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); 582 break; 583 } 584 case BTA_PAN_OPENING_EVT: 585 { 586 btpan_conn_t* conn; 587 bdstr_t bds; 588 bdaddr_to_string((bt_bdaddr_t *)p_data->opening.bd_addr, bds, sizeof(bds)); 589 BTIF_TRACE_DEBUG("BTA_PAN_OPENING_EVT handle %d, addr: %s", p_data->opening.handle, bds); 590 conn = btpan_find_conn_addr(p_data->opening.bd_addr); 591 592 asrt(conn != NULL); 593 if (conn) 594 { 595 conn->handle = p_data->opening.handle; 596 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 597 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 598 callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, 599 (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, btpan_remote_role); 600 } 601 else 602 BTIF_TRACE_ERROR("connection not found"); 603 break; 604 } 605 case BTA_PAN_OPEN_EVT: 606 { 607 btpan_connection_state_t state; 608 bt_status_t status; 609 btpan_conn_t *conn = btpan_find_conn_handle(p_data->open.handle); 610 611 LOG_VERBOSE("%s pan connection open status: %d", __func__, p_data->open.status); 612 if (p_data->open.status == BTA_PAN_SUCCESS) 613 { 614 state = BTPAN_STATE_CONNECTED; 615 status = BT_STATUS_SUCCESS; 616 } 617 else 618 { 619 state = BTPAN_STATE_DISCONNECTED; 620 status = BT_STATUS_FAIL; 621 btpan_cleanup_conn(conn); 622 } 623 /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, conn); */ 624 /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, conn->remote_role); */ 625 int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); 626 int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); 627 callback.connection_state_cb(state, status, (const bt_bdaddr_t*)p_data->open.bd_addr, 628 btpan_conn_local_role, btpan_remote_role); 629 break; 630 } 631 case BTA_PAN_CLOSE_EVT: 632 { 633 btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); 634 635 LOG_INFO("%s: event = BTA_PAN_CLOSE_EVT handle %d", __FUNCTION__, p_data->close.handle); 636 637 if (conn && conn->handle >= 0) 638 { 639 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 640 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 641 callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, 0, (const bt_bdaddr_t*)conn->peer, 642 btpan_conn_local_role, btpan_remote_role); 643 btpan_cleanup_conn(conn); 644 } 645 else 646 BTIF_TRACE_ERROR("pan handle not found (%d)", p_data->close.handle); 647 break; 648 } 649 default: 650 BTIF_TRACE_WARNING("Unknown pan event %d", event); 651 break; 652 } 653} 654 655static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data) 656{ 657 btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, sizeof(tBTA_PAN), NULL); 658} 659 660#define IS_EXCEPTION(e) ((e) & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)) 661static void btu_exec_tap_fd_read(void *p_param) { 662 struct pollfd ufd; 663 int fd = (int)p_param; 664 665 if (fd == INVALID_FD || fd != btpan_cb.tap_fd) 666 return; 667 668 // Don't occupy BTU context too long, avoid GKI buffer overruns and 669 // give other profiles a chance to run by limiting the amount of memory 670 // PAN can use from the shared pool buffer. 671 for (int i = 0; i < PAN_POOL_MAX && btif_is_enabled() && btpan_cb.flow; i++) { 672 BT_HDR *buffer = (BT_HDR *)GKI_getpoolbuf(PAN_POOL_ID); 673 if (!buffer) { 674 BTIF_TRACE_WARNING("%s unable to allocate buffer for packet.", __func__); 675 break; 676 } 677 buffer->offset = PAN_MINIMUM_OFFSET; 678 buffer->len = GKI_get_buf_size(buffer) - sizeof(BT_HDR) - buffer->offset; 679 680 UINT8 *packet = (UINT8 *)buffer + sizeof(BT_HDR) + buffer->offset; 681 682 // If we don't have an undelivered packet left over, pull one from the TAP driver. 683 // We save it in the congest_packet right away in case we can't deliver it in this 684 // attempt. 685 if (!btpan_cb.congest_packet_size) { 686 ssize_t ret = read(fd, btpan_cb.congest_packet, sizeof(btpan_cb.congest_packet)); 687 switch (ret) { 688 case -1: 689 BTIF_TRACE_ERROR("%s unable to read from driver: %s", __func__, strerror(errno)); 690 GKI_freebuf(buffer); 691 //add fd back to monitor thread to try it again later 692 btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); 693 return; 694 case 0: 695 BTIF_TRACE_WARNING("%s end of file reached.", __func__); 696 GKI_freebuf(buffer); 697 //add fd back to monitor thread to process the exception 698 btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); 699 return; 700 default: 701 btpan_cb.congest_packet_size = ret; 702 break; 703 } 704 } 705 706 memcpy(packet, btpan_cb.congest_packet, MIN(btpan_cb.congest_packet_size, buffer->len)); 707 buffer->len = MIN(btpan_cb.congest_packet_size, buffer->len); 708 709 if (buffer->len > sizeof(tETH_HDR) && should_forward((tETH_HDR *)packet)) { 710 // Extract the ethernet header from the buffer since the PAN_WriteBuf inside 711 // forward_bnep can't handle two pointers that point inside the same GKI buffer. 712 tETH_HDR hdr; 713 memcpy(&hdr, packet, sizeof(tETH_HDR)); 714 715 // Skip the ethernet header. 716 buffer->len -= sizeof(tETH_HDR); 717 buffer->offset += sizeof(tETH_HDR); 718 if (forward_bnep(&hdr, buffer) != FORWARD_CONGEST) 719 btpan_cb.congest_packet_size = 0; 720 } else { 721 BTIF_TRACE_WARNING("%s dropping packet of length %d", __func__, buffer->len); 722 btpan_cb.congest_packet_size = 0; 723 GKI_freebuf(buffer); 724 } 725 726 // Bail out of the loop if reading from the TAP fd would block. 727 ufd.fd = fd; 728 ufd.events = POLLIN; 729 ufd.revents = 0; 730 if (poll(&ufd, 1, 0) <= 0 || IS_EXCEPTION(ufd.revents)) 731 break; 732 } 733 //add fd back to monitor thread 734 btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); 735} 736 737static void btif_pan_close_all_conns() { 738 if (!stack_initialized) 739 return; 740 741 for (int i = 0; i < MAX_PAN_CONNS; ++i) 742 { 743 if (btpan_cb.conns[i].handle != -1) 744 BTA_PanClose(btpan_cb.conns[i].handle); 745 } 746} 747 748static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id) { 749 assert(btpan_cb.tap_fd == INVALID_FD || btpan_cb.tap_fd == fd); 750 751 if (btpan_cb.tap_fd != fd) { 752 BTIF_TRACE_WARNING("%s Signaled on mismatched fds exp:%d act:%d\n", 753 __func__, btpan_cb.tap_fd, fd); 754 return; 755 } 756 757 if (flags & SOCK_THREAD_FD_EXCEPTION) { 758 btpan_cb.tap_fd = INVALID_FD; 759 btpan_tap_close(fd); 760 btif_pan_close_all_conns(); 761 } else if (flags & SOCK_THREAD_FD_RD) 762 bta_dmexecutecallback(btu_exec_tap_fd_read, (void *)fd); 763} 764