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