1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /************************************************************************************
21 * Filename: btif_pan.c
23 * Description: PAN Profile Bluetooth Interface
26 ***********************************************************************************/
27 #include <hardware/bluetooth.h>
28 #include <hardware/bt_pan.h>
32 #include <sys/select.h>
34 #include <sys/ioctl.h>
35 #include <netinet/in.h>
40 #include <sys/socket.h>
43 #include <linux/sockios.h>
44 #include <sys/prctl.h>
46 #include <linux/if_tun.h>
47 #include <linux/if_ether.h>
49 #define LOG_TAG "BTIF_PAN"
50 #include "btif_common.h"
51 #include "btif_util.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"
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
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
75 #define BTPAN_LOCAL_ROLE (BTPAN_ROLE_PANU | BTPAN_ROLE_PANNAP)
78 #define asrt(s) if(!(s)) BTIF_TRACE_ERROR("btif_pan: ## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
80 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
85 static int jni_initialized, stack_initialized;
86 static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks);
87 static void btpan_jni_cleanup();
88 static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role);
89 static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr);
90 static bt_status_t btpan_enable(int local_role);
91 static int btpan_get_local_role(void);
93 static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id);
94 static void btpan_cleanup_conn(btpan_conn_t* conn);
95 static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data);
96 static void btu_exec_tap_fd_read(void *p_param);
98 static btpan_interface_t pan_if = {
102 btpan_get_local_role,
108 btpan_interface_t *btif_pan_get_interface()
113 /*******************************************************************************
115 ** Function btif_pan_init
117 ** Description initializes the pan interface
119 ** Returns bt_status_t
121 *******************************************************************************/
124 BTIF_TRACE_DEBUG("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized, btpan_cb.enabled);
125 stack_initialized = TRUE;
126 if (jni_initialized && !btpan_cb.enabled)
128 BTIF_TRACE_DEBUG("Enabling PAN....");
129 memset(&btpan_cb, 0, sizeof(btpan_cb));
130 btpan_cb.tap_fd = -1;
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);
141 static void pan_disable()
145 btpan_cb.enabled = 0;
147 if(btpan_cb.tap_fd != -1)
149 btpan_tap_close(btpan_cb.tap_fd);
150 btpan_cb.tap_fd = -1;
155 void btif_pan_cleanup()
157 if(stack_initialized)
159 //bt is shuting down, invalid all bta pan handles
161 for(i = 0; i < MAX_PAN_CONNS; i++)
162 btpan_cleanup_conn(&btpan_cb.conns[i]);
165 stack_initialized = FALSE;
168 static btpan_callbacks_t callback;
169 static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks)
171 BTIF_TRACE_DEBUG("stack_initialized = %d, btpan_cb.enabled:%d", stack_initialized, btpan_cb.enabled);
172 jni_initialized = TRUE;
173 if(stack_initialized && !btpan_cb.enabled)
175 callback = *callbacks;
176 return BT_STATUS_SUCCESS;
179 static void btpan_jni_cleanup()
182 jni_initialized = FALSE;
185 static inline int bta_role_to_btpan(int bta_pan_role)
188 BTIF_TRACE_DEBUG("bta_pan_role:0x%x", bta_pan_role);
189 if(bta_pan_role & PAN_ROLE_NAP_SERVER)
191 btpan_role |= BTPAN_ROLE_PANNAP;
193 if(bta_pan_role & PAN_ROLE_CLIENT)
195 btpan_role |= BTPAN_ROLE_PANU;
200 static inline int btpan_role_to_bta(int btpan_role)
202 int bta_pan_role = PAN_ROLE_INACTIVE;
203 BTIF_TRACE_DEBUG("btpan_role:0x%x", btpan_role);
204 if(btpan_role & BTPAN_ROLE_PANNAP)
206 bta_pan_role |= PAN_ROLE_NAP_SERVER;
208 if(btpan_role & BTPAN_ROLE_PANU)
210 bta_pan_role |= PAN_ROLE_CLIENT;
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, 1, PAN_SECURITY};
219 static bt_status_t btpan_enable(int local_role)
222 BTIF_TRACE_DEBUG("local_role:%d", local_role);
223 bta_pan_role = btpan_role_to_bta(local_role);
224 #if BTA_PAN_INCLUDED == TRUE
225 BTA_PanSetRole(bta_pan_role, &bta_panu_info, NULL, &bta_pan_nap_info);
226 btpan_dev_local_role = local_role;
227 return BT_STATUS_SUCCESS;
229 return BT_STATUS_FAIL;
233 static int btpan_get_local_role()
235 BTIF_TRACE_DEBUG("btpan_dev_local_role:%d", btpan_dev_local_role);
236 return btpan_dev_local_role;
239 static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role)
241 BTIF_TRACE_DEBUG("local_role:%d, remote_role:%d", local_role, remote_role);
242 int bta_local_role = btpan_role_to_bta(local_role);
243 int bta_remote_role = btpan_role_to_bta(remote_role);
244 btpan_new_conn(-1, bd_addr->address, bta_local_role, bta_remote_role);
245 BTA_PanOpen((UINT8*)bd_addr->address, bta_local_role, bta_remote_role);
246 return BT_STATUS_SUCCESS;
249 static void btif_in_pan_generic_evt(UINT16 event, char *p_param)
251 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
253 case BTIF_PAN_CB_DISCONNECTING:
255 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)p_param;
256 btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address);
257 int btpan_conn_local_role;
258 int btpan_remote_role;
261 btpan_conn_local_role = bta_role_to_btpan(conn->local_role);
262 btpan_remote_role = bta_role_to_btpan(conn->remote_role);
263 callback.connection_state_cb(BTPAN_STATE_DISCONNECTING, BT_STATUS_SUCCESS,
264 (const bt_bdaddr_t*)conn->peer, btpan_conn_local_role, btpan_remote_role);
269 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
275 static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr)
277 btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address);
278 if(conn && conn->handle >= 0)
280 BTA_PanClose(conn->handle);
281 /* Inform the application that the disconnect has been initiated successfully */
282 btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING,
283 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
284 return BT_STATUS_SUCCESS;
286 return BT_STATUS_FAIL;
289 static int pan_pth = -1;
290 void create_tap_read_thread(int tap_fd)
293 pan_pth = btsock_thread_create(btpan_tap_fd_signaled, NULL);
295 btsock_thread_add_fd(pan_pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0);
298 void destroy_tap_read_thread(void)
302 btsock_thread_exit(pan_pth);
307 static int tap_if_up(const char *devname, BD_ADDR addr)
312 sk = socket(AF_INET, SOCK_DGRAM, 0);
315 memset(&ifr, 0, sizeof(ifr));
316 strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1);
317 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
320 BTIF_TRACE_ERROR("Could not get network hardware for interface:%s, errno:%s", devname, strerror(errno));
324 /* debug("found mac address for interface:%s = %02x:%02x:%02x:%02x:%02x:%02x", devname, */
325 /* ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2], */
326 /* ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); */
327 strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1);
328 memcpy(ifr.ifr_hwaddr.sa_data, addr, 6);
329 /* debug("setting bt address for interface:%s = %02x:%02x:%02x:%02x:%02x:%02x", devname, */
330 /* ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2], */
331 /* ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); */
333 /* The IEEE has specified that the most significant bit of the most significant byte is used to
334 * determine a multicast address. If its a 1, that means multicast, 0 means unicast.
335 * Kernel returns an error if we try to set a multicast address for the tun-tap ethernet interface.
336 * Mask this bit to avoid any issue with auto generated address.
338 if (ifr.ifr_hwaddr.sa_data[0] & 0x01) {
339 BTIF_TRACE_WARNING("Not a unicast MAC address, force multicast bit flipping");
340 ifr.ifr_hwaddr.sa_data[0] &= ~0x01;
343 err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr);
346 BTIF_TRACE_ERROR("Could not set bt address for interface:%s, errno:%s", devname, strerror(errno));
352 memset(&ifr, 0, sizeof(ifr));
353 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
355 ifr.ifr_flags |= IFF_UP;
356 ifr.ifr_flags |= IFF_MULTICAST;
358 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr);
362 BTIF_TRACE_ERROR("Could not bring up network interface:%s, errno:%d", devname, errno);
367 BTIF_TRACE_DEBUG("network interface: %s is up", devname);
371 static int tap_if_down(const char *devname)
376 sk = socket(AF_INET, SOCK_DGRAM, 0);
378 memset(&ifr, 0, sizeof(ifr));
379 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
381 ifr.ifr_flags &= ~IFF_UP;
383 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr);
390 void btpan_set_flow_control(BOOLEAN enable) {
391 if (btpan_cb.tap_fd == -1)
394 btpan_cb.flow = enable;
396 btsock_thread_add_fd(pan_pth, btpan_cb.tap_fd, 0, SOCK_THREAD_FD_RD, 0);
397 bta_dmexecutecallback(btu_exec_tap_fd_read, (void *)btpan_cb.tap_fd);
405 const char *clonedev = "/dev/tun";
407 /* open the clone device */
409 //system("insmod /system/lib/modules/tun.ko");
410 if( (fd = open(clonedev, O_RDWR)) < 0 ) {
412 BTIF_TRACE_DEBUG("could not open %s, err:%d", clonedev, errno);
416 memset(&ifr, 0, sizeof(ifr));
417 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
419 strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ);
421 /* try to create the device */
422 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 )//|| tap_setup_ip(TAP_IF_NAME) == FALSE)
424 BTIF_TRACE_DEBUG("ioctl error:%d, errno:%s", err, strerror(errno));
428 BTM_GetLocalDeviceAddr (local_addr);
429 if(tap_if_up(TAP_IF_NAME, local_addr) == 0)
433 BTIF_TRACE_ERROR("can not bring up tap interface:%s", TAP_IF_NAME);
438 int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, UINT16 proto, const char* buf,
439 UINT16 len, BOOLEAN ext, BOOLEAN forward)
446 //if(is_empty_eth_addr(dst))
447 // memcpy(ð_hdr.h_dest, local_addr, ETH_ADDR_LEN);
449 memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN);
450 memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN);
451 eth_hdr.h_proto = htons(proto);
453 memcpy(packet, ð_hdr, sizeof(tETH_HDR));
456 ALOGE("btpan_tap_send eth packet size:%d is exceeded limit!", len);
459 memcpy(packet + sizeof(tETH_HDR), buf, len);
461 /* Send data to network interface */
462 //btnet_send(btpan_cb.conn[i].sock.sock, &buffer, (len + sizeof(tETH_HDR)));
463 //dump_bin("packet to network", packet, len + sizeof(tETH_HDR));
464 int ret = write(tap_fd, packet, len + sizeof(tETH_HDR));
465 BTIF_TRACE_DEBUG("ret:%d", ret);
472 int btpan_tap_close(int fd)
474 tap_if_down(TAP_IF_NAME);
477 btsock_thread_wakeup(pan_pth);
481 btpan_conn_t * btpan_find_conn_handle(UINT16 handle)
484 for(i = 0; i < MAX_PAN_CONNS; i++)
485 if(btpan_cb.conns[i].handle == handle)
486 return &btpan_cb.conns[i];
490 btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr)
493 for(i = 0; i < MAX_PAN_CONNS; i++)
494 if(memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0)
495 return &btpan_cb.conns[i];
499 static void btpan_cleanup_conn(btpan_conn_t* conn)
505 memset(&conn->peer, 0, sizeof(conn->peer));
506 memset(&conn->eth_addr, 0, sizeof(conn->eth_addr));
507 conn->local_role = conn->remote_role = 0;
511 btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int remote_role )
514 for(i = 0; i < MAX_PAN_CONNS; i++)
516 BTIF_TRACE_DEBUG("conns[%d]:%d", i, btpan_cb.conns[i].handle);
517 if(btpan_cb.conns[i].handle == -1)
519 BTIF_TRACE_DEBUG("handle:%d, local_role:%d, remote_role:%d", handle, local_role, remote_role);
521 btpan_cb.conns[i].handle = handle;
522 bdcpy(btpan_cb.conns[i].peer, addr);
523 btpan_cb.conns[i].local_role = local_role;
524 btpan_cb.conns[i].remote_role = remote_role;
525 return &btpan_cb.conns[i];
528 BTIF_TRACE_DEBUG("MAX_PAN_CONNS:%d exceeded, return NULL as failed", MAX_PAN_CONNS);
532 void btpan_close_handle(btpan_conn_t *p)
534 BTIF_TRACE_DEBUG("btpan_close_handle : close handle %d", p->handle);
538 memset(&p->peer, 0, 6);
541 static inline bool should_forward(tETH_HDR* hdr)
543 uint16_t proto = ntohs(hdr->h_proto);
544 if(proto == ETH_P_IP || proto == ETH_P_ARP || proto == ETH_P_IPV6)
546 BTIF_TRACE_DEBUG("unknown proto:%x", proto);
550 static int forward_bnep(tETH_HDR* eth_hdr, BT_HDR *hdr) {
551 int broadcast = eth_hdr->h_dest[0] & 1;
554 // Find the right connection to send this frame over.
555 for (i = 0; i < MAX_PAN_CONNS; i++) {
556 UINT16 handle = btpan_cb.conns[i].handle;
557 if (handle != (UINT16)-1 &&
558 (broadcast || memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0
559 || memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0)) {
560 int result = PAN_WriteBuf(handle, eth_hdr->h_dest, eth_hdr->h_src, ntohs(eth_hdr->h_proto), hdr, 0);
562 case PAN_Q_SIZE_EXCEEDED:
563 return FORWARD_CONGEST;
565 return FORWARD_SUCCESS;
567 return FORWARD_FAILURE;
572 return FORWARD_IGNORE;
575 static void bta_pan_callback_transfer(UINT16 event, char *p_param)
577 tBTA_PAN *p_data = (tBTA_PAN *)p_param;
580 case BTA_PAN_ENABLE_EVT:
581 BTIF_TRACE_DEBUG("BTA_PAN_ENABLE_EVT");
583 case BTA_PAN_SET_ROLE_EVT:
585 int btpan_role = bta_role_to_btpan(p_data->set_role.role);
586 bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
587 btpan_control_state_t state = btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED;
588 callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME);
591 case BTA_PAN_OPENING_EVT:
595 bd2str((bt_bdaddr_t*)p_data->opening.bd_addr, &bds);
596 BTIF_TRACE_DEBUG("BTA_PAN_OPENING_EVT handle %d, addr: %s", p_data->opening.handle, bds);
597 conn = btpan_find_conn_addr(p_data->opening.bd_addr);
602 conn->handle = p_data->opening.handle;
603 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role);
604 int btpan_remote_role = bta_role_to_btpan(conn->remote_role);
605 callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS,
606 (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, btpan_remote_role);
609 BTIF_TRACE_ERROR("connection not found");
612 case BTA_PAN_OPEN_EVT:
614 /* debug("BTA_PAN_OPEN_EVT, open status:%d, bd_addr = [%02X:%02X:%02X:%02X:%02X:%02X]", */
615 /* p_data->open.status, */
616 /* p_data->open.bd_addr[0], p_data->open.bd_addr[1], p_data->open.bd_addr[2], */
617 /* p_data->open.bd_addr[3], p_data->open.bd_addr[4], p_data->open.bd_addr[5]); */
618 btpan_connection_state_t state;
620 if(p_data->open.status == BTA_PAN_SUCCESS)
622 state = BTPAN_STATE_CONNECTED;
623 status = BT_STATUS_SUCCESS;
627 state = BTPAN_STATE_DISCONNECTED;
628 status = BT_STATUS_FAIL;
630 btpan_conn_t* conn = btpan_find_conn_handle(p_data->open.handle);
631 /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, conn); */
632 /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, conn->remote_role); */
633 int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role);
634 /* debug("bta local_role:%d, bta remote role:%d", p_data->open.local_role, p_data->open.peer_role); */
635 int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role);
636 callback.connection_state_cb(state, status, (const bt_bdaddr_t*)p_data->open.bd_addr,
637 btpan_conn_local_role, btpan_remote_role);
640 case BTA_PAN_CLOSE_EVT:
642 btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle);
644 ALOGI("%s: event = BTA_PAN_CLOSE_EVT handle %d", __FUNCTION__, p_data->close.handle);
646 if(conn && conn->handle >= 0)
648 /* debug("BTA_PAN_CLOSE_EVT, conn local_role:%d, remote_role:%d", conn->local_role, conn->remote_role); */
649 int btpan_conn_local_role = bta_role_to_btpan(conn->local_role);
650 int btpan_remote_role = bta_role_to_btpan(conn->remote_role);
651 callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, 0, (const bt_bdaddr_t*)conn->peer,
652 btpan_conn_local_role, btpan_remote_role);
653 btpan_cleanup_conn(conn);
656 BTIF_TRACE_ERROR("pan handle not found (%d)", p_data->close.handle);
660 BTIF_TRACE_WARNING("Unknown pan event %d", event);
665 static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data)
667 btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, sizeof(tBTA_PAN), NULL);
670 #define IS_EXCEPTION(e) ((e) & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL))
671 static void btu_exec_tap_fd_read(void *p_param) {
673 int fd = (int)p_param;
675 if (fd == -1 || fd != btpan_cb.tap_fd)
678 // Keep sending until someone either turns off BTIF or disables data the flow.
679 while (btif_is_enabled() && btpan_cb.flow) {
680 BT_HDR *buffer = (BT_HDR *)GKI_getpoolbuf(PAN_POOL_ID);
682 BTIF_TRACE_WARNING("%s unable to allocate buffer for packet.", __func__);
685 buffer->offset = PAN_MINIMUM_OFFSET;
686 buffer->len = GKI_get_buf_size(buffer) - sizeof(BT_HDR) - buffer->offset;
688 UINT8 *packet = (UINT8 *)buffer + sizeof(BT_HDR) + buffer->offset;
690 // If we don't have an undelivered packet left over, pull one from the TAP driver.
691 // We save it in the congest_packet right away in case we can't deliver it in this
693 if (!btpan_cb.congest_packet_size) {
694 ssize_t ret = read(fd, btpan_cb.congest_packet, sizeof(btpan_cb.congest_packet));
697 BTIF_TRACE_ERROR("%s unable to read from driver: %s", __func__, strerror(errno));
701 BTIF_TRACE_WARNING("%s end of file reached.", __func__);
705 btpan_cb.congest_packet_size = ret;
710 memcpy(packet, btpan_cb.congest_packet, MIN(btpan_cb.congest_packet_size, buffer->len));
711 buffer->len = MIN(btpan_cb.congest_packet_size, buffer->len);
713 if (buffer->len > sizeof(tETH_HDR) && should_forward((tETH_HDR *)packet)) {
714 // Extract the ethernet header from the buffer since the PAN_WriteBuf inside
715 // forward_bnep can't handle two pointers that point inside the same GKI buffer.
717 memcpy(&hdr, packet, sizeof(tETH_HDR));
719 // Skip the ethernet header.
720 buffer->len -= sizeof(tETH_HDR);
721 buffer->offset += sizeof(tETH_HDR);
722 if (forward_bnep(&hdr, buffer) != FORWARD_CONGEST)
723 btpan_cb.congest_packet_size = 0;
725 BTIF_TRACE_WARNING("%s dropping packet of length %d", __func__, buffer->len);
726 btpan_cb.congest_packet_size = 0;
730 // Bail out of the loop if reading from the TAP fd would block.
734 if(poll(&ufd, 1, 0) <= 0 || IS_EXCEPTION(ufd.revents)) {
735 btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0);
741 static void btif_pan_close_all_conns() {
743 if (!stack_initialized)
746 for (i = 0; i < MAX_PAN_CONNS; ++i)
747 if (btpan_cb.conns[i].handle != -1)
748 BTA_PanClose(btpan_cb.conns[i].handle);
751 static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id) {
752 assert(btpan_cb.tap_fd == fd);
754 if (btpan_cb.tap_fd != fd)
757 if(flags & SOCK_THREAD_FD_EXCEPTION) {
758 btpan_cb.tap_fd = -1;
760 btif_pan_close_all_conns();
761 } else if(flags & SOCK_THREAD_FD_RD)
762 bta_dmexecutecallback(btu_exec_tap_fd_read, (void *)fd);