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