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 74 btpan_cb_t btpan_cb; 75 76 BD_ADDR local_addr; 77 static int jni_initialized, stack_initialized; 78 static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks); 79 static void btpan_jni_cleanup(); 80 static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role); 81 static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr); 82 static bt_status_t btpan_enable(int local_role); 83 static int btpan_get_local_role(void); 84 85 static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id); 86 static void btpan_cleanup_conn(btpan_conn_t* conn); 87 static 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 *******************************************************************************/ 97 static 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 }; 106 btpan_interface_t *btif_pan_get_interface() 107 { 108 return &pan_if; 109 } 110 void 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 } 127 static 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 } 141 void 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 154 static btpan_callbacks_t callback; 155 static 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 165 static void btpan_jni_cleanup() 166 { 167 pan_disable(); 168 jni_initialized = FALSE; 169 } 170 static 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 } 184 static 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 } 198 static volatile int btpan_dev_local_role; 199 static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY}; 200 static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 0, PAN_SECURITY}; 201 202 static 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 } 211 static 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 } 216 static 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 } 225 static 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 } 250 static 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 } 263 static int pth = -1; 264 void 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 } 273 void destroy_tap_read_thread(void) 274 { 275 if(pth >= 0) 276 { 277 btsock_thread_exit(pth); 278 pth = -1; 279 } 280 } 281 static 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 335 static 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 } 353 int 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 } 389 int 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 if(tap_fd != -1) 393 { 394 tETH_HDR eth_hdr; 395 //if(is_empty_eth_addr(dst)) 396 // memcpy(ð_hdr.h_dest, local_addr, ETH_ADDR_LEN); 397 //else 398 memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN); 399 memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN); 400 eth_hdr.h_proto = htons(proto); 401 char packet[2000]; 402 memcpy(packet, ð_hdr, sizeof(tETH_HDR)); 403 if(len > 2000) 404 { 405 ALOGE("btpan_tap_send eth packet size:%d is exceeded limit!", len); 406 return -1; 407 } 408 memcpy(packet + sizeof(tETH_HDR), buf, len); 409 410 /* Send data to network interface */ 411 //btnet_send(btpan_cb.conn[i].sock.sock, &buffer, (len + sizeof(tETH_HDR))); 412 //dump_bin("packet to network", packet, len + sizeof(tETH_HDR)); 413 int ret = write(tap_fd, packet, len + sizeof(tETH_HDR)); 414 BTIF_TRACE_DEBUG1("ret:%d", ret); 415 return ret; 416 } 417 return -1; 418 419 } 420 int btpan_tap_close(int fd) 421 { 422 tap_if_down(TAP_IF_NAME); 423 close(fd); 424 return 0; 425 } 426 btpan_conn_t * btpan_find_conn_handle(UINT16 handle) 427 { 428 int i; 429 for(i = 0; i < MAX_PAN_CONNS; i++) 430 if(btpan_cb.conns[i].handle == handle) 431 return &btpan_cb.conns[i]; 432 return NULL; 433 } 434 btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr) 435 { 436 int i; 437 for(i = 0; i < MAX_PAN_CONNS; i++) 438 if(memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0) 439 return &btpan_cb.conns[i]; 440 return NULL; 441 } 442 static void btpan_cleanup_conn(btpan_conn_t* conn) 443 { 444 if(conn) 445 { 446 conn->handle = -1; 447 conn->state = -1; 448 memset(&conn->peer, 0, sizeof(conn->peer)); 449 memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); 450 conn->local_role = conn->remote_role = 0; 451 } 452 } 453 btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int remote_role ) 454 { 455 int i; 456 for(i = 0; i < MAX_PAN_CONNS; i++) 457 { 458 BTIF_TRACE_DEBUG2("conns[%d]:%d", i, btpan_cb.conns[i].handle); 459 if(btpan_cb.conns[i].handle == -1) 460 { 461 BTIF_TRACE_DEBUG3("handle:%d, local_role:%d, remote_role:%d", handle, local_role, remote_role); 462 463 btpan_cb.conns[i].handle = handle; 464 bdcpy(btpan_cb.conns[i].peer, addr); 465 btpan_cb.conns[i].local_role = local_role; 466 btpan_cb.conns[i].remote_role = remote_role; 467 return &btpan_cb.conns[i]; 468 } 469 } 470 BTIF_TRACE_DEBUG1("MAX_PAN_CONNS:%d exceeded, return NULL as failed", MAX_PAN_CONNS); 471 return NULL; 472 } 473 474 void btpan_close_handle(btpan_conn_t *p) 475 { 476 BTIF_TRACE_DEBUG1("btpan_close_handle : close handle %d", p->handle); 477 p->handle = -1; 478 p->local_role = -1; 479 p->remote_role = -1; 480 memset(&p->peer, 0, 6); 481 } 482 static inline int should_forward(tETH_HDR* hdr) 483 { 484 if(ntohs(hdr->h_proto) == ETH_P_IP || ntohs(hdr->h_proto) == ETH_P_ARP) 485 return TRUE; 486 BTIF_TRACE_DEBUG1("unknown proto:%x", ntohs(hdr->h_proto)); 487 return FALSE; 488 } 489 extern void bta_pan_ci_rx_write(UINT16 handle, BD_ADDR dst, BD_ADDR src, UINT16 protocol, 490 UINT8 *p_data, UINT16 len, BOOLEAN ext); 491 static void forward_bnep(tETH_HDR* eth_hdr, char * packet, int size) 492 { 493 int broadcast = eth_hdr->h_dest[0] & 1; 494 int i; 495 for(i = 0; i < MAX_PAN_CONNS; i++) 496 { 497 UINT16 handle = btpan_cb.conns[i].handle; 498 if(handle != (UINT16)-1 && 499 (broadcast || memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0 500 || memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0)) 501 { 502 BTIF_TRACE_DEBUG1("calling bta_pan_ci_rx_write, handle:%d", handle); 503 bta_pan_ci_rx_write(handle, eth_hdr->h_dest, eth_hdr->h_src, 504 ntohs(eth_hdr->h_proto), (UINT8*)packet, size, 0); 505 break; 506 } 507 } 508 } 509 510 static void bta_pan_callback_transfer(UINT16 event, char *p_param) 511 { 512 tBTA_PAN *p_data = (tBTA_PAN *)p_param; 513 switch(event) 514 { 515 case BTA_PAN_ENABLE_EVT: 516 BTIF_TRACE_DEBUG0("BTA_PAN_ENABLE_EVT"); 517 break; 518 case BTA_PAN_SET_ROLE_EVT: 519 { 520 int btpan_role = bta_role_to_btpan(p_data->set_role.role); 521 bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 522 btpan_control_state_t state = btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; 523 callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); 524 break; 525 } 526 case BTA_PAN_OPENING_EVT: 527 { 528 btpan_conn_t* conn; 529 bdstr_t bds; 530 bd2str((bt_bdaddr_t*)p_data->opening.bd_addr, &bds); 531 BTIF_TRACE_DEBUG2("BTA_PAN_OPENING_EVT handle %d, addr: %s", p_data->opening.handle, bds); 532 conn = btpan_find_conn_addr(p_data->opening.bd_addr); 533 534 asrt(conn != NULL); 535 if (conn) 536 { 537 conn->handle = p_data->opening.handle; 538 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 539 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 540 callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, 541 (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, btpan_remote_role); 542 } 543 else 544 BTIF_TRACE_ERROR0("connection not found"); 545 break; 546 } 547 case BTA_PAN_OPEN_EVT: 548 { 549 /* debug("BTA_PAN_OPEN_EVT, open status:%d, bd_addr = [%02X:%02X:%02X:%02X:%02X:%02X]", */ 550 /* p_data->open.status, */ 551 /* p_data->open.bd_addr[0], p_data->open.bd_addr[1], p_data->open.bd_addr[2], */ 552 /* p_data->open.bd_addr[3], p_data->open.bd_addr[4], p_data->open.bd_addr[5]); */ 553 btpan_connection_state_t state; 554 bt_status_t status; 555 if(p_data->open.status == BTA_PAN_SUCCESS) 556 { 557 state = BTPAN_STATE_CONNECTED; 558 status = BT_STATUS_SUCCESS; 559 } 560 else 561 { 562 state = BTPAN_STATE_DISCONNECTED; 563 status = BT_STATUS_FAIL; 564 } 565 btpan_conn_t* conn = btpan_find_conn_handle(p_data->open.handle); 566 /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, conn); */ 567 /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, conn->remote_role); */ 568 int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); 569 /* debug("bta local_role:%d, bta remote role:%d", p_data->open.local_role, p_data->open.peer_role); */ 570 int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); 571 callback.connection_state_cb(state, status, (const bt_bdaddr_t*)p_data->open.bd_addr, 572 btpan_conn_local_role, btpan_remote_role); 573 break; 574 } 575 case BTA_PAN_CLOSE_EVT: 576 { 577 btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); 578 579 ALOGI("%s: event = BTA_PAN_CLOSE_EVT handle %d", __FUNCTION__, p_data->close.handle); 580 581 if(conn && conn->handle >= 0) 582 { 583 /* debug("BTA_PAN_CLOSE_EVT, conn local_role:%d, remote_role:%d", conn->local_role, conn->remote_role); */ 584 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); 585 int btpan_remote_role = bta_role_to_btpan(conn->remote_role); 586 callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, 0, (const bt_bdaddr_t*)conn->peer, 587 btpan_conn_local_role, btpan_remote_role); 588 btpan_cleanup_conn(conn); 589 } 590 else 591 BTIF_TRACE_ERROR1("pan handle not found (%d)", p_data->close.handle); 592 break; 593 } 594 default: 595 BTIF_TRACE_WARNING1("Unknown pan event %d", event); 596 break; 597 } 598 } 599 600 static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data) 601 { 602 btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, sizeof(tBTA_PAN), NULL); 603 } 604 #define MAX_PACKET_SIZE 2000 605 static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id) 606 { 607 char packet[MAX_PACKET_SIZE]; 608 tETH_HDR eth_hdr; 609 if(flags & SOCK_THREAD_FD_EXCEPTION) 610 { 611 BTIF_TRACE_ERROR1("pan tap fd:%d exception", fd); 612 } 613 else if(flags & SOCK_THREAD_FD_RD) 614 { 615 /* debug("tab fd read trigged, data"); */ 616 int size = read(fd, packet, MAX_PACKET_SIZE); 617 /* debug("tap fd read trigged, read size:%d", size); */ 618 memcpy(ð_hdr, &packet, sizeof(tETH_HDR)); 619 /* debug("eth src = %02x:%02x:%02x:%02x:%02x:%02x", */ 620 /* eth_hdr.h_src[0], eth_hdr.h_src[1], eth_hdr.h_src[2], eth_hdr.h_src[3], */ 621 /* eth_hdr.h_src[4], eth_hdr.h_src[5]); */ 622 /* debug("eth dest = %02x:%02x:%02x:%02x:%02x:%02x", */ 623 /* eth_hdr.h_dest[0], eth_hdr.h_dest[1], eth_hdr.h_dest[2], eth_hdr.h_dest[3], */ 624 /* eth_hdr.h_dest[4], eth_hdr.h_dest[5]); */ 625 //dump_bin("eth packet received", packet, size); 626 if(should_forward(ð_hdr)) 627 { 628 forward_bnep(ð_hdr, packet + sizeof(tETH_HDR), size - sizeof(tETH_HDR)); 629 } 630 btsock_thread_add_fd(pth, fd, 0, SOCK_THREAD_FD_RD | SOCK_THREAD_ADD_FD_SYNC, 0); 631 } 632 } 633 634 635