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