btif_pan.c revision 5cd8bff2dd0337cb52bf48f312e3d2d55a8882fb
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, 0, 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 err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr); 308 309 if (err < 0) { 310 BTIF_TRACE_ERROR2("Could not set bt address for interface:%s, errno:%s", devname, strerror(errno)); 311 close(sk); 312 return -1; 313 } 314 315 //bring it up 316 memset(&ifr, 0, sizeof(ifr)); 317 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); 318 319 ifr.ifr_flags |= IFF_UP; 320 ifr.ifr_flags |= IFF_MULTICAST; 321 322 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); 323 324 325 if (err < 0) { 326 BTIF_TRACE_ERROR2("Could not bring up network interface:%s, errno:%d", devname, errno); 327 close(sk); 328 return -1; 329 } 330 close(sk); 331 BTIF_TRACE_DEBUG1("network interface: %s is up", devname); 332 return 0; 333} 334 335static int tap_if_down(const char *devname) 336{ 337 struct ifreq ifr; 338 int sk, err; 339 340 sk = socket(AF_INET, SOCK_DGRAM, 0); 341 342 memset(&ifr, 0, sizeof(ifr)); 343 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); 344 345 ifr.ifr_flags &= ~IFF_UP; 346 347 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); 348 349 close(sk); 350 351 return 0; 352} 353int btpan_tap_open() 354{ 355 struct ifreq ifr; 356 int fd, err; 357 const char *clonedev = "/dev/tun"; 358 359 /* open the clone device */ 360 361 //system("insmod /system/lib/modules/tun.ko"); 362 if( (fd = open(clonedev, O_RDWR)) < 0 ) { 363 364 BTIF_TRACE_DEBUG2("could not open %s, err:%d", clonedev, errno); 365 return fd; 366 } 367 368 memset(&ifr, 0, sizeof(ifr)); 369 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 370 371 strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ); 372 373 /* try to create the device */ 374 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 )//|| tap_setup_ip(TAP_IF_NAME) == FALSE) 375 { 376 BTIF_TRACE_DEBUG2("ioctl error:%d, errno:%s", err, strerror(errno)); 377 close(fd); 378 return err; 379 } 380 BTM_GetLocalDeviceAddr (local_addr); 381 if(tap_if_up(TAP_IF_NAME, local_addr) == 0) 382 { 383 return fd; 384 } 385 BTIF_TRACE_ERROR1("can not bring up tap interface:%s", TAP_IF_NAME); 386 close(fd); 387 return -1; 388} 389int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, UINT16 proto, const char* buf, 390 UINT16 len, BOOLEAN ext, BOOLEAN forward) 391{ 392 UNUSED(ext); 393 UNUSED(forward); 394 if(tap_fd != -1) 395 { 396 tETH_HDR eth_hdr; 397 //if(is_empty_eth_addr(dst)) 398 // memcpy(ð_hdr.h_dest, local_addr, ETH_ADDR_LEN); 399 //else 400 memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN); 401 memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN); 402 eth_hdr.h_proto = htons(proto); 403 char packet[2000]; 404 memcpy(packet, ð_hdr, sizeof(tETH_HDR)); 405 if(len > 2000) 406 { 407 ALOGE("btpan_tap_send eth packet size:%d is exceeded limit!", len); 408 return -1; 409 } 410 memcpy(packet + sizeof(tETH_HDR), buf, len); 411 412 /* Send data to network interface */ 413 //btnet_send(btpan_cb.conn[i].sock.sock, &buffer, (len + sizeof(tETH_HDR))); 414 //dump_bin("packet to network", packet, len + sizeof(tETH_HDR)); 415 int ret = write(tap_fd, packet, len + sizeof(tETH_HDR)); 416 BTIF_TRACE_DEBUG1("ret:%d", ret); 417 return ret; 418 } 419 return -1; 420 421} 422int btpan_tap_close(int fd) 423{ 424 tap_if_down(TAP_IF_NAME); 425 close(fd); 426 return 0; 427} 428btpan_conn_t * btpan_find_conn_handle(UINT16 handle) 429{ 430 int i; 431 for(i = 0; i < MAX_PAN_CONNS; i++) 432 if(btpan_cb.conns[i].handle == handle) 433 return &btpan_cb.conns[i]; 434 return NULL; 435} 436btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr) 437{ 438 int i; 439 for(i = 0; i < MAX_PAN_CONNS; i++) 440 if(memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0) 441 return &btpan_cb.conns[i]; 442 return NULL; 443} 444static void btpan_cleanup_conn(btpan_conn_t* conn) 445{ 446 if(conn) 447 { 448 conn->handle = -1; 449 conn->state = -1; 450 memset(&conn->peer, 0, sizeof(conn->peer)); 451 memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); 452 conn->local_role = conn->remote_role = 0; 453 } 454} 455btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int remote_role ) 456{ 457 int i; 458 for(i = 0; i < MAX_PAN_CONNS; i++) 459 { 460 BTIF_TRACE_DEBUG2("conns[%d]:%d", i, btpan_cb.conns[i].handle); 461 if(btpan_cb.conns[i].handle == -1) 462 { 463 BTIF_TRACE_DEBUG3("handle:%d, local_role:%d, remote_role:%d", handle, local_role, remote_role); 464 465 btpan_cb.conns[i].handle = handle; 466 bdcpy(btpan_cb.conns[i].peer, addr); 467 btpan_cb.conns[i].local_role = local_role; 468 btpan_cb.conns[i].remote_role = remote_role; 469 return &btpan_cb.conns[i]; 470 } 471 } 472 BTIF_TRACE_DEBUG1("MAX_PAN_CONNS:%d exceeded, return NULL as failed", MAX_PAN_CONNS); 473 return NULL; 474} 475 476void btpan_close_handle(btpan_conn_t *p) 477{ 478 BTIF_TRACE_DEBUG1("btpan_close_handle : close handle %d", p->handle); 479 p->handle = -1; 480 p->local_role = -1; 481 p->remote_role = -1; 482 memset(&p->peer, 0, 6); 483} 484static inline int should_forward(tETH_HDR* hdr) 485{ 486 if(ntohs(hdr->h_proto) == ETH_P_IP || ntohs(hdr->h_proto) == ETH_P_ARP) 487 return TRUE; 488 BTIF_TRACE_DEBUG1("unknown proto:%x", ntohs(hdr->h_proto)); 489 return FALSE; 490} 491extern void bta_pan_ci_rx_write(UINT16 handle, BD_ADDR dst, BD_ADDR src, UINT16 protocol, 492 UINT8 *p_data, UINT16 len, BOOLEAN ext); 493static void forward_bnep(tETH_HDR* eth_hdr, char * packet, int size) 494{ 495 int broadcast = eth_hdr->h_dest[0] & 1; 496 int i; 497 for(i = 0; i < MAX_PAN_CONNS; i++) 498 { 499 UINT16 handle = btpan_cb.conns[i].handle; 500 if(handle != (UINT16)-1 && 501 (broadcast || memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0 502 || memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0)) 503 { 504 BTIF_TRACE_DEBUG1("calling bta_pan_ci_rx_write, handle:%d", handle); 505 bta_pan_ci_rx_write(handle, eth_hdr->h_dest, eth_hdr->h_src, 506 ntohs(eth_hdr->h_proto), (UINT8*)packet, size, 0); 507 break; 508 } 509 } 510} 511 512static void bta_pan_callback_transfer(UINT16 event, char *p_param) 513{ 514 tBTA_PAN *p_data = (tBTA_PAN *)p_param; 515 switch(event) 516 { 517 case BTA_PAN_ENABLE_EVT: 518 BTIF_TRACE_DEBUG0("BTA_PAN_ENABLE_EVT"); 519 break; 520 case BTA_PAN_SET_ROLE_EVT: 521 { 522 int btpan_role = bta_role_to_btpan(p_data->set_role.role); 523 bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 524 btpan_control_state_t state = btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; 525 callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); 526 break; 527 } 528 case BTA_PAN_OPENING_EVT: 529 { 530 btpan_conn_t* conn; 531 bdstr_t bds; 532 bd2str((bt_bdaddr_t*)p_data->opening.bd_addr, &bds); 533 BTIF_TRACE_DEBUG2("BTA_PAN_OPENING_EVT handle %d, addr: %s", p_data->opening.handle, bds); 534 conn = btpan_find_conn_addr(p_data->opening.bd_addr); 535 536 asrt(conn != NULL); 537 if (conn) 538 { 539 conn->handle = p_data->opening.handle; 540 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 541 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 542 callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, 543 (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, btpan_remote_role); 544 } 545 else 546 BTIF_TRACE_ERROR0("connection not found"); 547 break; 548 } 549 case BTA_PAN_OPEN_EVT: 550 { 551 /* debug("BTA_PAN_OPEN_EVT, open status:%d, bd_addr = [%02X:%02X:%02X:%02X:%02X:%02X]", */ 552 /* p_data->open.status, */ 553 /* p_data->open.bd_addr[0], p_data->open.bd_addr[1], p_data->open.bd_addr[2], */ 554 /* p_data->open.bd_addr[3], p_data->open.bd_addr[4], p_data->open.bd_addr[5]); */ 555 btpan_connection_state_t state; 556 bt_status_t status; 557 if(p_data->open.status == BTA_PAN_SUCCESS) 558 { 559 state = BTPAN_STATE_CONNECTED; 560 status = BT_STATUS_SUCCESS; 561 } 562 else 563 { 564 state = BTPAN_STATE_DISCONNECTED; 565 status = BT_STATUS_FAIL; 566 } 567 btpan_conn_t* conn = btpan_find_conn_handle(p_data->open.handle); 568 /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, conn); */ 569 /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, conn->remote_role); */ 570 int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); 571 /* debug("bta local_role:%d, bta remote role:%d", p_data->open.local_role, p_data->open.peer_role); */ 572 int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); 573 callback.connection_state_cb(state, status, (const bt_bdaddr_t*)p_data->open.bd_addr, 574 btpan_conn_local_role, btpan_remote_role); 575 break; 576 } 577 case BTA_PAN_CLOSE_EVT: 578 { 579 btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); 580 581 ALOGI("%s: event = BTA_PAN_CLOSE_EVT handle %d", __FUNCTION__, p_data->close.handle); 582 583 if(conn && conn->handle >= 0) 584 { 585 /* debug("BTA_PAN_CLOSE_EVT, conn local_role:%d, remote_role:%d", conn->local_role, conn->remote_role); */ 586 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 587 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 588 callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, 0, (const bt_bdaddr_t*)conn->peer, 589 btpan_conn_local_role, btpan_remote_role); 590 btpan_cleanup_conn(conn); 591 } 592 else 593 BTIF_TRACE_ERROR1("pan handle not found (%d)", p_data->close.handle); 594 break; 595 } 596 default: 597 BTIF_TRACE_WARNING1("Unknown pan event %d", event); 598 break; 599 } 600} 601 602static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data) 603{ 604 btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, sizeof(tBTA_PAN), NULL); 605} 606#define MAX_PACKET_SIZE 2000 607static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id) 608{ 609 char packet[MAX_PACKET_SIZE]; 610 tETH_HDR eth_hdr; 611 UNUSED(type); 612 UNUSED(user_id); 613 614 if(flags & SOCK_THREAD_FD_EXCEPTION) 615 { 616 BTIF_TRACE_ERROR1("pan tap fd:%d exception", fd); 617 } 618 else if(flags & SOCK_THREAD_FD_RD) 619 { 620 /* debug("tab fd read trigged, data"); */ 621 int size = read(fd, packet, MAX_PACKET_SIZE); 622 /* debug("tap fd read trigged, read size:%d", size); */ 623 memcpy(ð_hdr, &packet, sizeof(tETH_HDR)); 624 /* debug("eth src = %02x:%02x:%02x:%02x:%02x:%02x", */ 625 /* eth_hdr.h_src[0], eth_hdr.h_src[1], eth_hdr.h_src[2], eth_hdr.h_src[3], */ 626 /* eth_hdr.h_src[4], eth_hdr.h_src[5]); */ 627 /* debug("eth dest = %02x:%02x:%02x:%02x:%02x:%02x", */ 628 /* eth_hdr.h_dest[0], eth_hdr.h_dest[1], eth_hdr.h_dest[2], eth_hdr.h_dest[3], */ 629 /* eth_hdr.h_dest[4], eth_hdr.h_dest[5]); */ 630 //dump_bin("eth packet received", packet, size); 631 if(should_forward(ð_hdr)) 632 { 633 forward_bnep(ð_hdr, packet + sizeof(tETH_HDR), size - sizeof(tETH_HDR)); 634 } 635 btsock_thread_add_fd(pth, fd, 0, SOCK_THREAD_FD_RD | SOCK_THREAD_ADD_FD_SYNC, 0); 636 } 637} 638 639 640