btif_pan.c revision 01364c729dd8f94be9959300188c94324960e0df
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 <signal.h> 30#include <ctype.h> 31#include <sys/select.h> 32#include <sys/poll.h> 33#include <sys/ioctl.h> 34#include <netinet/in.h> 35#include <netdb.h> 36#include <stdio.h> 37#include <errno.h> 38#include <fcntl.h> 39#include <sys/socket.h> 40#include <sys/wait.h> 41#include <net/if.h> 42#include <linux/sockios.h> 43#include <sys/prctl.h> 44#include <linux/if.h> 45#include <linux/if_tun.h> 46#include <linux/if_ether.h> 47 48#define LOG_TAG "BTIF_PAN" 49#include "btif_common.h" 50#include "btif_util.h" 51#include "btm_api.h" 52#include "bd.h" 53 54#include "bta_api.h" 55#include "bta_pan_api.h" 56#include "btif_sock_thread.h" 57#include "btif_sock_util.h" 58#include "btif_pan_internal.h" 59 60//#define PANU_DISABLED TRUE 61 62#if (PAN_NAP_DISABLED == TRUE) && (PANU_DISABLED == TRUE) 63#define BTPAN_LOCAL_ROLE BTPAN_ROLE_NONE 64#elif PAN_NAP_DISABLED == TRUE 65#define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANU 66#elif PANU_DISABLED == TRUE 67#define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANNAP 68#else 69#define BTPAN_LOCAL_ROLE (BTPAN_ROLE_PANU | BTPAN_ROLE_PANNAP) 70#endif 71 72#define asrt(s) if(!(s)) BTIF_TRACE_ERROR3("btif_pan: ## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__) 73 74btpan_cb_t btpan_cb; 75 76BD_ADDR local_addr; 77static int jni_initialized, stack_initialized; 78static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks); 79static void btpan_jni_cleanup(); 80static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role); 81static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr); 82static bt_status_t btpan_enable(int local_role); 83static int btpan_get_local_role(void); 84 85static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id); 86static void btpan_cleanup_conn(btpan_conn_t* conn); 87static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data); 88/******************************************************************************* 89 ** 90 ** Function btpan_ini 91 ** 92 ** Description initializes the pan interface 93 ** 94 ** Returns bt_status_t 95 ** 96 *******************************************************************************/ 97static btpan_interface_t pan_if = { 98 sizeof(pan_if), 99 btpan_jni_init, 100 btpan_enable, 101 btpan_get_local_role, 102 btpan_connect, 103 btpan_disconnect, 104 btpan_jni_cleanup 105}; 106btpan_interface_t *btif_pan_get_interface() 107{ 108 return &pan_if; 109} 110void btif_pan_init() 111{ 112 BTIF_TRACE_DEBUG2("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized, btpan_cb.enabled); 113 stack_initialized = TRUE; 114 if (jni_initialized && !btpan_cb.enabled) 115 { 116 BTIF_TRACE_DEBUG0("Enabling PAN...."); 117 memset(&btpan_cb, 0, sizeof(btpan_cb)); 118 btpan_cb.tap_fd = -1; 119 int i; 120 for(i = 0; i < MAX_PAN_CONNS; i++) 121 btpan_cleanup_conn(&btpan_cb.conns[i]); 122 BTA_PanEnable(bta_pan_callback); 123 btpan_cb.enabled = 1; 124 btpan_enable(BTPAN_LOCAL_ROLE); 125 } 126} 127static void pan_disable() 128{ 129 if(btpan_cb.enabled) 130 { 131 btpan_cb.enabled = 0; 132 BTA_PanDisable(); 133 if(btpan_cb.tap_fd != -1) 134 { 135 destroy_tap_read_thread(); 136 btpan_tap_close(btpan_cb.tap_fd); 137 btpan_cb.tap_fd = -1; 138 } 139 } 140} 141void btif_pan_cleanup() 142{ 143 if(stack_initialized) 144 { 145 //bt is shuting down, invalid all bta pan handles 146 int i; 147 for(i = 0; i < MAX_PAN_CONNS; i++) 148 btpan_cleanup_conn(&btpan_cb.conns[i]); 149 pan_disable(); 150 } 151 stack_initialized = FALSE; 152} 153 154static btpan_callbacks_t callback; 155static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks) 156{ 157 BTIF_TRACE_DEBUG2("stack_initialized = %d, btpan_cb.enabled:%d", stack_initialized, btpan_cb.enabled); 158 jni_initialized = TRUE; 159 if(stack_initialized && !btpan_cb.enabled) 160 btif_pan_init(); 161 callback = *callbacks; 162 return BT_STATUS_SUCCESS; 163} 164 165static void btpan_jni_cleanup() 166{ 167 pan_disable(); 168 jni_initialized = FALSE; 169} 170static inline int bta_role_to_btpan(int bta_pan_role) 171{ 172 int btpan_role = 0; 173 BTIF_TRACE_DEBUG1("bta_pan_role:0x%x", bta_pan_role); 174 if(bta_pan_role & PAN_ROLE_NAP_SERVER) 175 { 176 btpan_role |= BTPAN_ROLE_PANNAP; 177 } 178 if(bta_pan_role & PAN_ROLE_CLIENT) 179 { 180 btpan_role |= BTPAN_ROLE_PANU; 181 } 182 return btpan_role; 183} 184static inline int btpan_role_to_bta(int btpan_role) 185{ 186 int bta_pan_role = PAN_ROLE_INACTIVE; 187 BTIF_TRACE_DEBUG1("btpan_role:0x%x", btpan_role); 188 if(btpan_role & BTPAN_ROLE_PANNAP) 189 { 190 bta_pan_role |= PAN_ROLE_NAP_SERVER; 191 } 192 if(btpan_role & BTPAN_ROLE_PANU) 193 { 194 bta_pan_role |= PAN_ROLE_CLIENT; 195 } 196 return bta_pan_role; 197} 198static volatile int btpan_dev_local_role; 199static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY}; 200static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1, PAN_SECURITY}; 201 202static bt_status_t btpan_enable(int local_role) 203{ 204 int bta_pan_role; 205 BTIF_TRACE_DEBUG1("local_role:%d", local_role); 206 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} 211static int btpan_get_local_role() 212{ 213 BTIF_TRACE_DEBUG1("btpan_dev_local_role:%d", btpan_dev_local_role); 214 return btpan_dev_local_role; 215} 216static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role) 217{ 218 BTIF_TRACE_DEBUG2("local_role:%d, remote_role:%d", local_role, remote_role); 219 int bta_local_role = btpan_role_to_bta(local_role); 220 int bta_remote_role = btpan_role_to_bta(remote_role); 221 btpan_new_conn(-1, bd_addr->address, bta_local_role, bta_remote_role); 222 BTA_PanOpen((UINT8*)bd_addr->address, bta_local_role, bta_remote_role); 223 return BT_STATUS_SUCCESS; 224} 225static void btif_in_pan_generic_evt(UINT16 event, char *p_param) 226{ 227 BTIF_TRACE_EVENT2("%s: event=%d", __FUNCTION__, event); 228 switch (event) { 229 case BTIF_PAN_CB_DISCONNECTING: 230 { 231 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)p_param; 232 btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); 233 int btpan_conn_local_role; 234 int btpan_remote_role; 235 asrt(conn != NULL); 236 if (conn) { 237 btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 238 btpan_remote_role = bta_role_to_btpan(conn->remote_role); 239 callback.connection_state_cb(BTPAN_STATE_DISCONNECTING, BT_STATUS_SUCCESS, 240 (const bt_bdaddr_t*)conn->peer, btpan_conn_local_role, btpan_remote_role); 241 } 242 } break; 243 default: 244 { 245 BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event); 246 } 247 break; 248 } 249} 250static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr) 251{ 252 btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); 253 if(conn && conn->handle >= 0) 254 { 255 BTA_PanClose(conn->handle); 256 /* Inform the application that the disconnect has been initiated successfully */ 257 btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING, 258 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); 259 return BT_STATUS_SUCCESS; 260 } 261 return BT_STATUS_FAIL; 262} 263static int pth = -1; 264void create_tap_read_thread(int tap_fd) 265{ 266 if(pth < 0) 267 { 268 pth = btsock_thread_create(btpan_tap_fd_signaled, NULL); 269 if(pth >= 0) 270 btsock_thread_add_fd(pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0); 271 } 272} 273void destroy_tap_read_thread(void) 274{ 275 if(pth >= 0) 276 { 277 btsock_thread_exit(pth); 278 pth = -1; 279 } 280} 281static int tap_if_up(const char *devname, BD_ADDR addr) 282{ 283 struct ifreq ifr; 284 int sk, err; 285 286 sk = socket(AF_INET, SOCK_DGRAM, 0); 287 288 //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 { 294 BTIF_TRACE_ERROR2("Could not get network hardware for interface:%s, errno:%s", devname, strerror(errno)); 295 close(sk); 296 return -1; 297 } 298 /* debug("found mac address for interface:%s = %02x:%02x:%02x:%02x:%02x:%02x", devname, */ 299 /* ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2], */ 300 /* ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); */ 301 strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); 302 memcpy(ifr.ifr_hwaddr.sa_data, addr, 6); 303 /* debug("setting bt address for interface:%s = %02x:%02x:%02x:%02x:%02x:%02x", devname, */ 304 /* ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2], */ 305 /* ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); */ 306 307 /* The IEEE has specified that the most significant bit of the most significant byte is used to 308 * determine a multicast address. If its a 1, that means multicast, 0 means unicast. 309 * Kernel returns an error if we try to set a multicast address for the tun-tap ethernet interface. 310 * Mask this bit to avoid any issue with auto generated address. 311 */ 312 if (ifr.ifr_hwaddr.sa_data[0] & 0x01) { 313 BTIF_TRACE_WARNING0("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_ERROR2("Could not set bt address for interface:%s, errno:%s", devname, strerror(errno)); 321 close(sk); 322 return -1; 323 } 324 325 //bring it up 326 memset(&ifr, 0, sizeof(ifr)); 327 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); 328 329 ifr.ifr_flags |= IFF_UP; 330 ifr.ifr_flags |= IFF_MULTICAST; 331 332 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); 333 334 335 if (err < 0) { 336 BTIF_TRACE_ERROR2("Could not bring up network interface:%s, errno:%d", devname, errno); 337 close(sk); 338 return -1; 339 } 340 close(sk); 341 BTIF_TRACE_DEBUG1("network interface: %s is up", devname); 342 return 0; 343} 344 345static int tap_if_down(const char *devname) 346{ 347 struct ifreq ifr; 348 int sk, err; 349 350 sk = socket(AF_INET, SOCK_DGRAM, 0); 351 352 memset(&ifr, 0, sizeof(ifr)); 353 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); 354 355 ifr.ifr_flags &= ~IFF_UP; 356 357 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); 358 359 close(sk); 360 361 return 0; 362} 363int btpan_tap_open() 364{ 365 struct ifreq ifr; 366 int fd, err; 367 const char *clonedev = "/dev/tun"; 368 369 /* open the clone device */ 370 371 //system("insmod /system/lib/modules/tun.ko"); 372 if( (fd = open(clonedev, O_RDWR)) < 0 ) { 373 374 BTIF_TRACE_DEBUG2("could not open %s, err:%d", clonedev, errno); 375 return fd; 376 } 377 378 memset(&ifr, 0, sizeof(ifr)); 379 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 380 381 strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ); 382 383 /* try to create the device */ 384 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 )//|| tap_setup_ip(TAP_IF_NAME) == FALSE) 385 { 386 BTIF_TRACE_DEBUG2("ioctl error:%d, errno:%s", err, strerror(errno)); 387 close(fd); 388 return err; 389 } 390 BTM_GetLocalDeviceAddr (local_addr); 391 if(tap_if_up(TAP_IF_NAME, local_addr) == 0) 392 { 393 return fd; 394 } 395 BTIF_TRACE_ERROR1("can not bring up tap interface:%s", TAP_IF_NAME); 396 close(fd); 397 return -1; 398} 399int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, UINT16 proto, const char* buf, 400 UINT16 len, BOOLEAN ext, BOOLEAN forward) 401{ 402 if(tap_fd != -1) 403 { 404 tETH_HDR eth_hdr; 405 //if(is_empty_eth_addr(dst)) 406 // memcpy(ð_hdr.h_dest, local_addr, ETH_ADDR_LEN); 407 //else 408 memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN); 409 memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN); 410 eth_hdr.h_proto = htons(proto); 411 char packet[2000]; 412 memcpy(packet, ð_hdr, sizeof(tETH_HDR)); 413 if(len > 2000) 414 { 415 ALOGE("btpan_tap_send eth packet size:%d is exceeded limit!", len); 416 return -1; 417 } 418 memcpy(packet + sizeof(tETH_HDR), buf, len); 419 420 /* Send data to network interface */ 421 //btnet_send(btpan_cb.conn[i].sock.sock, &buffer, (len + sizeof(tETH_HDR))); 422 //dump_bin("packet to network", packet, len + sizeof(tETH_HDR)); 423 int ret = write(tap_fd, packet, len + sizeof(tETH_HDR)); 424 BTIF_TRACE_DEBUG1("ret:%d", ret); 425 return ret; 426 } 427 return -1; 428 429} 430int btpan_tap_close(int fd) 431{ 432 tap_if_down(TAP_IF_NAME); 433 close(fd); 434 return 0; 435} 436btpan_conn_t * btpan_find_conn_handle(UINT16 handle) 437{ 438 int i; 439 for(i = 0; i < MAX_PAN_CONNS; i++) 440 if(btpan_cb.conns[i].handle == handle) 441 return &btpan_cb.conns[i]; 442 return NULL; 443} 444btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr) 445{ 446 int i; 447 for(i = 0; i < MAX_PAN_CONNS; i++) 448 if(memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0) 449 return &btpan_cb.conns[i]; 450 return NULL; 451} 452static void btpan_cleanup_conn(btpan_conn_t* conn) 453{ 454 if(conn) 455 { 456 conn->handle = -1; 457 conn->state = -1; 458 memset(&conn->peer, 0, sizeof(conn->peer)); 459 memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); 460 conn->local_role = conn->remote_role = 0; 461 } 462} 463btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int remote_role ) 464{ 465 int i; 466 for(i = 0; i < MAX_PAN_CONNS; i++) 467 { 468 BTIF_TRACE_DEBUG2("conns[%d]:%d", i, btpan_cb.conns[i].handle); 469 if(btpan_cb.conns[i].handle == -1) 470 { 471 BTIF_TRACE_DEBUG3("handle:%d, local_role:%d, remote_role:%d", handle, local_role, remote_role); 472 473 btpan_cb.conns[i].handle = handle; 474 bdcpy(btpan_cb.conns[i].peer, addr); 475 btpan_cb.conns[i].local_role = local_role; 476 btpan_cb.conns[i].remote_role = remote_role; 477 return &btpan_cb.conns[i]; 478 } 479 } 480 BTIF_TRACE_DEBUG1("MAX_PAN_CONNS:%d exceeded, return NULL as failed", MAX_PAN_CONNS); 481 return NULL; 482} 483 484void btpan_close_handle(btpan_conn_t *p) 485{ 486 BTIF_TRACE_DEBUG1("btpan_close_handle : close handle %d", p->handle); 487 p->handle = -1; 488 p->local_role = -1; 489 p->remote_role = -1; 490 memset(&p->peer, 0, 6); 491} 492static inline int should_forward(tETH_HDR* hdr) 493{ 494 if(ntohs(hdr->h_proto) == ETH_P_IP || ntohs(hdr->h_proto) == ETH_P_ARP) 495 return TRUE; 496 BTIF_TRACE_DEBUG1("unknown proto:%x", ntohs(hdr->h_proto)); 497 return FALSE; 498} 499extern void bta_pan_ci_rx_write(UINT16 handle, BD_ADDR dst, BD_ADDR src, UINT16 protocol, 500 UINT8 *p_data, UINT16 len, BOOLEAN ext); 501static void forward_bnep(tETH_HDR* eth_hdr, char * packet, int size) 502{ 503 int broadcast = eth_hdr->h_dest[0] & 1; 504 int i; 505 for(i = 0; i < MAX_PAN_CONNS; i++) 506 { 507 UINT16 handle = btpan_cb.conns[i].handle; 508 if(handle != (UINT16)-1 && 509 (broadcast || memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0 510 || memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0)) 511 { 512 BTIF_TRACE_DEBUG1("calling bta_pan_ci_rx_write, handle:%d", handle); 513 bta_pan_ci_rx_write(handle, eth_hdr->h_dest, eth_hdr->h_src, 514 ntohs(eth_hdr->h_proto), (UINT8*)packet, size, 0); 515 break; 516 } 517 } 518} 519 520static void bta_pan_callback_transfer(UINT16 event, char *p_param) 521{ 522 tBTA_PAN *p_data = (tBTA_PAN *)p_param; 523 switch(event) 524 { 525 case BTA_PAN_ENABLE_EVT: 526 BTIF_TRACE_DEBUG0("BTA_PAN_ENABLE_EVT"); 527 break; 528 case BTA_PAN_SET_ROLE_EVT: 529 { 530 int btpan_role = bta_role_to_btpan(p_data->set_role.role); 531 bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 532 btpan_control_state_t state = btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; 533 callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); 534 break; 535 } 536 case BTA_PAN_OPENING_EVT: 537 { 538 btpan_conn_t* conn; 539 bdstr_t bds; 540 bd2str((bt_bdaddr_t*)p_data->opening.bd_addr, &bds); 541 BTIF_TRACE_DEBUG2("BTA_PAN_OPENING_EVT handle %d, addr: %s", p_data->opening.handle, bds); 542 conn = btpan_find_conn_addr(p_data->opening.bd_addr); 543 544 asrt(conn != NULL); 545 if (conn) 546 { 547 conn->handle = p_data->opening.handle; 548 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 549 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 550 callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, 551 (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, btpan_remote_role); 552 } 553 else 554 BTIF_TRACE_ERROR0("connection not found"); 555 break; 556 } 557 case BTA_PAN_OPEN_EVT: 558 { 559 /* debug("BTA_PAN_OPEN_EVT, open status:%d, bd_addr = [%02X:%02X:%02X:%02X:%02X:%02X]", */ 560 /* p_data->open.status, */ 561 /* p_data->open.bd_addr[0], p_data->open.bd_addr[1], p_data->open.bd_addr[2], */ 562 /* p_data->open.bd_addr[3], p_data->open.bd_addr[4], p_data->open.bd_addr[5]); */ 563 btpan_connection_state_t state; 564 bt_status_t status; 565 if(p_data->open.status == BTA_PAN_SUCCESS) 566 { 567 state = BTPAN_STATE_CONNECTED; 568 status = BT_STATUS_SUCCESS; 569 } 570 else 571 { 572 state = BTPAN_STATE_DISCONNECTED; 573 status = BT_STATUS_FAIL; 574 } 575 btpan_conn_t* conn = btpan_find_conn_handle(p_data->open.handle); 576 /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, conn); */ 577 /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, conn->remote_role); */ 578 int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); 579 /* debug("bta local_role:%d, bta remote role:%d", p_data->open.local_role, p_data->open.peer_role); */ 580 int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); 581 callback.connection_state_cb(state, status, (const bt_bdaddr_t*)p_data->open.bd_addr, 582 btpan_conn_local_role, btpan_remote_role); 583 break; 584 } 585 case BTA_PAN_CLOSE_EVT: 586 { 587 btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); 588 589 ALOGI("%s: event = BTA_PAN_CLOSE_EVT handle %d", __FUNCTION__, p_data->close.handle); 590 591 if(conn && conn->handle >= 0) 592 { 593 /* debug("BTA_PAN_CLOSE_EVT, conn local_role:%d, remote_role:%d", conn->local_role, conn->remote_role); */ 594 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 595 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 596 callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, 0, (const bt_bdaddr_t*)conn->peer, 597 btpan_conn_local_role, btpan_remote_role); 598 btpan_cleanup_conn(conn); 599 } 600 else 601 BTIF_TRACE_ERROR1("pan handle not found (%d)", p_data->close.handle); 602 break; 603 } 604 default: 605 BTIF_TRACE_WARNING1("Unknown pan event %d", event); 606 break; 607 } 608} 609 610static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data) 611{ 612 btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, sizeof(tBTA_PAN), NULL); 613} 614#define MAX_PACKET_SIZE 2000 615static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id) 616{ 617 char packet[MAX_PACKET_SIZE]; 618 tETH_HDR eth_hdr; 619 if(flags & SOCK_THREAD_FD_EXCEPTION) 620 { 621 BTIF_TRACE_ERROR1("pan tap fd:%d exception", fd); 622 } 623 else if(flags & SOCK_THREAD_FD_RD) 624 { 625 /* debug("tab fd read trigged, data"); */ 626 int size = read(fd, packet, MAX_PACKET_SIZE); 627 /* debug("tap fd read trigged, read size:%d", size); */ 628 memcpy(ð_hdr, &packet, sizeof(tETH_HDR)); 629 /* debug("eth src = %02x:%02x:%02x:%02x:%02x:%02x", */ 630 /* eth_hdr.h_src[0], eth_hdr.h_src[1], eth_hdr.h_src[2], eth_hdr.h_src[3], */ 631 /* eth_hdr.h_src[4], eth_hdr.h_src[5]); */ 632 /* debug("eth dest = %02x:%02x:%02x:%02x:%02x:%02x", */ 633 /* eth_hdr.h_dest[0], eth_hdr.h_dest[1], eth_hdr.h_dest[2], eth_hdr.h_dest[3], */ 634 /* eth_hdr.h_dest[4], eth_hdr.h_dest[5]); */ 635 //dump_bin("eth packet received", packet, size); 636 if(should_forward(ð_hdr)) 637 { 638 forward_bnep(ð_hdr, packet + sizeof(tETH_HDR), size - sizeof(tETH_HDR)); 639 } 640 btsock_thread_add_fd(pth, fd, 0, SOCK_THREAD_FD_RD | SOCK_THREAD_ADD_FD_SYNC, 0); 641 } 642} 643 644 645