The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | ***********************************************************************************/ |
Marie Janssen | 49120dc | 2015-07-07 16:47:20 -0700 | [diff] [blame] | 27 | |
| 28 | #define LOG_TAG "bt_btif_pan" |
| 29 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 30 | #include <assert.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 31 | #include <ctype.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 34 | #include <linux/if_ether.h> |
| 35 | #include <linux/if_tun.h> |
| 36 | #include <linux/sockios.h> |
| 37 | #include <net/if.h> |
| 38 | #include <netdb.h> |
| 39 | #include <netinet/in.h> |
| 40 | #include <signal.h> |
| 41 | #include <stdio.h> |
| 42 | #include <string.h> |
| 43 | #include <string.h> |
| 44 | #include <sys/ioctl.h> |
| 45 | #include <sys/poll.h> |
| 46 | #include <sys/prctl.h> |
| 47 | #include <sys/select.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 48 | #include <sys/socket.h> |
| 49 | #include <sys/wait.h> |
Arman Uguray | d30195c | 2015-05-29 15:27:58 -0700 | [diff] [blame] | 50 | #include <unistd.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 51 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 52 | #include <hardware/bluetooth.h> |
| 53 | #include <hardware/bt_pan.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 54 | |
| 55 | #include "bta_api.h" |
| 56 | #include "bta_pan_api.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 57 | #include "btcore/include/bdaddr.h" |
| 58 | #include "btif_common.h" |
| 59 | #include "btif_pan_internal.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 60 | #include "btif_sock_thread.h" |
| 61 | #include "btif_sock_util.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 62 | #include "btif_util.h" |
| 63 | #include "btm_api.h" |
| 64 | #include "device/include/controller.h" |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 65 | #include "bt_common.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 66 | #include "osi/include/log.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 67 | #include "osi/include/osi.h" |
| 68 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 69 | #define FORWARD_IGNORE 1 |
| 70 | #define FORWARD_SUCCESS 0 |
| 71 | #define FORWARD_FAILURE (-1) |
| 72 | #define FORWARD_CONGEST (-2) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 73 | //#define PANU_DISABLED TRUE |
| 74 | |
| 75 | #if (PAN_NAP_DISABLED == TRUE) && (PANU_DISABLED == TRUE) |
| 76 | #define BTPAN_LOCAL_ROLE BTPAN_ROLE_NONE |
| 77 | #elif PAN_NAP_DISABLED == TRUE |
| 78 | #define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANU |
| 79 | #elif PANU_DISABLED == TRUE |
| 80 | #define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANNAP |
| 81 | #else |
| 82 | #define BTPAN_LOCAL_ROLE (BTPAN_ROLE_PANU | BTPAN_ROLE_PANNAP) |
| 83 | #endif |
| 84 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 85 | #define asrt(s) if (!(s)) BTIF_TRACE_ERROR("btif_pan: ## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 86 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 87 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 88 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 89 | btpan_cb_t btpan_cb; |
| 90 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 91 | static bool jni_initialized; |
| 92 | static bool stack_initialized; |
| 93 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 94 | static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks); |
| 95 | static void btpan_jni_cleanup(); |
| 96 | static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role); |
| 97 | static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr); |
| 98 | static bt_status_t btpan_enable(int local_role); |
| 99 | static int btpan_get_local_role(void); |
| 100 | |
| 101 | static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id); |
| 102 | static void btpan_cleanup_conn(btpan_conn_t* conn); |
| 103 | static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 104 | static void btu_exec_tap_fd_read(void *p_param); |
| 105 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | }; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 115 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 116 | btpan_interface_t *btif_pan_get_interface() |
| 117 | { |
| 118 | return &pan_if; |
| 119 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 120 | |
| 121 | /******************************************************************************* |
| 122 | ** |
| 123 | ** Function btif_pan_init |
| 124 | ** |
| 125 | ** Description initializes the pan interface |
| 126 | ** |
| 127 | ** Returns bt_status_t |
| 128 | ** |
| 129 | *******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 130 | void btif_pan_init() |
| 131 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 132 | BTIF_TRACE_DEBUG("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized, btpan_cb.enabled); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 133 | stack_initialized = true; |
| 134 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 135 | if (jni_initialized && !btpan_cb.enabled) |
| 136 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 137 | BTIF_TRACE_DEBUG("Enabling PAN...."); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 138 | memset(&btpan_cb, 0, sizeof(btpan_cb)); |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 139 | btpan_cb.tap_fd = INVALID_FD; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 140 | btpan_cb.flow = 1; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 141 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 142 | btpan_cleanup_conn(&btpan_cb.conns[i]); |
| 143 | BTA_PanEnable(bta_pan_callback); |
| 144 | btpan_cb.enabled = 1; |
| 145 | btpan_enable(BTPAN_LOCAL_ROLE); |
| 146 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 147 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 148 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 149 | static void pan_disable() |
| 150 | { |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 151 | if (btpan_cb.enabled) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 152 | { |
| 153 | btpan_cb.enabled = 0; |
| 154 | BTA_PanDisable(); |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 155 | if (btpan_cb.tap_fd != INVALID_FD) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 156 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 157 | btpan_tap_close(btpan_cb.tap_fd); |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 158 | btpan_cb.tap_fd = INVALID_FD; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 162 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 163 | void btif_pan_cleanup() |
| 164 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 165 | if (!stack_initialized) |
| 166 | return; |
| 167 | |
| 168 | // Bluetooth is shuting down, invalidate all BTA PAN handles |
| 169 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
| 170 | btpan_cleanup_conn(&btpan_cb.conns[i]); |
| 171 | |
| 172 | pan_disable(); |
| 173 | stack_initialized = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static btpan_callbacks_t callback; |
| 177 | static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks) |
| 178 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 179 | BTIF_TRACE_DEBUG("stack_initialized = %d, btpan_cb.enabled:%d", stack_initialized, btpan_cb.enabled); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 180 | callback = *callbacks; |
Mudumba Ananth | f4f744f | 2015-05-06 09:15:41 -0700 | [diff] [blame] | 181 | jni_initialized = TRUE; |
| 182 | if (stack_initialized && !btpan_cb.enabled) |
| 183 | btif_pan_init(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 184 | return BT_STATUS_SUCCESS; |
| 185 | } |
| 186 | |
| 187 | static void btpan_jni_cleanup() |
| 188 | { |
| 189 | pan_disable(); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 190 | jni_initialized = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 191 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 192 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 193 | static inline int bta_role_to_btpan(int bta_pan_role) |
| 194 | { |
| 195 | int btpan_role = 0; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 196 | BTIF_TRACE_DEBUG("bta_pan_role:0x%x", bta_pan_role); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 197 | if (bta_pan_role & PAN_ROLE_NAP_SERVER) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 198 | btpan_role |= BTPAN_ROLE_PANNAP; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 199 | if (bta_pan_role & PAN_ROLE_CLIENT) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 200 | btpan_role |= BTPAN_ROLE_PANU; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 201 | return btpan_role; |
| 202 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 203 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 204 | static inline int btpan_role_to_bta(int btpan_role) |
| 205 | { |
| 206 | int bta_pan_role = PAN_ROLE_INACTIVE; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 207 | BTIF_TRACE_DEBUG("btpan_role:0x%x", btpan_role); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 208 | if (btpan_role & BTPAN_ROLE_PANNAP) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 209 | bta_pan_role |= PAN_ROLE_NAP_SERVER; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 210 | if (btpan_role & BTPAN_ROLE_PANU) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 211 | bta_pan_role |= PAN_ROLE_CLIENT; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 212 | return bta_pan_role; |
| 213 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 214 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 215 | static volatile int btpan_dev_local_role; |
Pavlin Radoslavov | a9ea43b | 2016-02-15 11:47:37 -0800 | [diff] [blame] | 216 | #if BTA_PAN_INCLUDED == TRUE |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 217 | static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY}; |
Nitin Shivpure | 36f43cc | 2013-08-29 21:28:59 +0530 | [diff] [blame] | 218 | static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1, PAN_SECURITY}; |
Pavlin Radoslavov | a9ea43b | 2016-02-15 11:47:37 -0800 | [diff] [blame] | 219 | #endif |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 220 | |
| 221 | static bt_status_t btpan_enable(int local_role) |
| 222 | { |
Mike J. Chen | 3576c56 | 2014-04-01 12:54:27 -0700 | [diff] [blame] | 223 | #if BTA_PAN_INCLUDED == TRUE |
Andre Eisenbach | 1f01b26 | 2015-04-02 01:01:34 -0700 | [diff] [blame] | 224 | BTIF_TRACE_DEBUG("%s - local_role: %d", __func__, local_role); |
| 225 | int bta_pan_role = btpan_role_to_bta(local_role); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 226 | BTA_PanSetRole(bta_pan_role, &bta_panu_info, NULL, &bta_pan_nap_info); |
| 227 | btpan_dev_local_role = local_role; |
| 228 | return BT_STATUS_SUCCESS; |
Mike J. Chen | 3576c56 | 2014-04-01 12:54:27 -0700 | [diff] [blame] | 229 | #else |
| 230 | return BT_STATUS_FAIL; |
| 231 | #endif |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 232 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 233 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 234 | static int btpan_get_local_role() |
| 235 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 236 | BTIF_TRACE_DEBUG("btpan_dev_local_role:%d", btpan_dev_local_role); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 237 | return btpan_dev_local_role; |
| 238 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 239 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 240 | static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role) |
| 241 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 242 | BTIF_TRACE_DEBUG("local_role:%d, remote_role:%d", local_role, remote_role); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 243 | int bta_local_role = btpan_role_to_bta(local_role); |
| 244 | int bta_remote_role = btpan_role_to_bta(remote_role); |
| 245 | btpan_new_conn(-1, bd_addr->address, bta_local_role, bta_remote_role); |
| 246 | BTA_PanOpen((UINT8*)bd_addr->address, bta_local_role, bta_remote_role); |
| 247 | return BT_STATUS_SUCCESS; |
| 248 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 249 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 250 | static void btif_in_pan_generic_evt(UINT16 event, char *p_param) |
| 251 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 252 | BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 253 | switch (event) { |
| 254 | case BTIF_PAN_CB_DISCONNECTING: |
| 255 | { |
| 256 | bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)p_param; |
| 257 | btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); |
| 258 | int btpan_conn_local_role; |
| 259 | int btpan_remote_role; |
| 260 | asrt(conn != NULL); |
| 261 | if (conn) { |
| 262 | btpan_conn_local_role = bta_role_to_btpan(conn->local_role); |
| 263 | btpan_remote_role = bta_role_to_btpan(conn->remote_role); |
| 264 | callback.connection_state_cb(BTPAN_STATE_DISCONNECTING, BT_STATUS_SUCCESS, |
| 265 | (const bt_bdaddr_t*)conn->peer, btpan_conn_local_role, btpan_remote_role); |
| 266 | } |
| 267 | } break; |
| 268 | default: |
| 269 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 270 | BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 271 | } |
| 272 | break; |
| 273 | } |
| 274 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 275 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 276 | static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr) |
| 277 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 278 | btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 279 | if (conn && conn->handle >= 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 280 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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); |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 284 | BTA_PanClose(conn->handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 285 | return BT_STATUS_SUCCESS; |
| 286 | } |
| 287 | return BT_STATUS_FAIL; |
| 288 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 289 | |
| 290 | static int pan_pth = -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 291 | void create_tap_read_thread(int tap_fd) |
| 292 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 293 | if (pan_pth < 0) |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 294 | pan_pth = btsock_thread_create(btpan_tap_fd_signaled, NULL); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 295 | if (pan_pth >= 0) |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 296 | btsock_thread_add_fd(pan_pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 297 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 298 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 299 | void destroy_tap_read_thread(void) |
| 300 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 301 | if (pan_pth >= 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 302 | { |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 303 | btsock_thread_exit(pan_pth); |
| 304 | pan_pth = -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 307 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 308 | static int tap_if_up(const char *devname, const bt_bdaddr_t *addr) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 309 | { |
| 310 | struct ifreq ifr; |
| 311 | int sk, err; |
| 312 | |
| 313 | sk = socket(AF_INET, SOCK_DGRAM, 0); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 314 | if (sk < 0) |
| 315 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 316 | |
| 317 | //set mac addr |
| 318 | memset(&ifr, 0, sizeof(ifr)); |
| 319 | strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); |
| 320 | err = ioctl(sk, SIOCGIFHWADDR, &ifr); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 321 | if (err < 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 322 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 323 | BTIF_TRACE_ERROR("Could not get network hardware for interface:%s, errno:%s", devname, strerror(errno)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 324 | close(sk); |
| 325 | return -1; |
| 326 | } |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 327 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 328 | strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 329 | memcpy(ifr.ifr_hwaddr.sa_data, addr->address, 6); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 330 | |
Loic Poulain | 01364c7 | 2014-04-08 08:46:01 +0200 | [diff] [blame] | 331 | /* The IEEE has specified that the most significant bit of the most significant byte is used to |
| 332 | * determine a multicast address. If its a 1, that means multicast, 0 means unicast. |
| 333 | * Kernel returns an error if we try to set a multicast address for the tun-tap ethernet interface. |
| 334 | * Mask this bit to avoid any issue with auto generated address. |
| 335 | */ |
| 336 | if (ifr.ifr_hwaddr.sa_data[0] & 0x01) { |
Matthew Xie | afa6e1a | 2014-06-28 11:35:29 -0700 | [diff] [blame] | 337 | BTIF_TRACE_WARNING("Not a unicast MAC address, force multicast bit flipping"); |
Loic Poulain | 01364c7 | 2014-04-08 08:46:01 +0200 | [diff] [blame] | 338 | ifr.ifr_hwaddr.sa_data[0] &= ~0x01; |
| 339 | } |
| 340 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 341 | err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr); |
| 342 | |
| 343 | if (err < 0) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 344 | BTIF_TRACE_ERROR("Could not set bt address for interface:%s, errno:%s", devname, strerror(errno)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 345 | close(sk); |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | //bring it up |
| 350 | memset(&ifr, 0, sizeof(ifr)); |
| 351 | strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); |
| 352 | |
| 353 | ifr.ifr_flags |= IFF_UP; |
| 354 | ifr.ifr_flags |= IFF_MULTICAST; |
| 355 | |
| 356 | err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); |
| 357 | |
| 358 | |
| 359 | if (err < 0) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 360 | BTIF_TRACE_ERROR("Could not bring up network interface:%s, errno:%d", devname, errno); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 361 | close(sk); |
| 362 | return -1; |
| 363 | } |
| 364 | close(sk); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 365 | BTIF_TRACE_DEBUG("network interface: %s is up", devname); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int tap_if_down(const char *devname) |
| 370 | { |
| 371 | struct ifreq ifr; |
Sharvil Nanavati | f1c764f | 2015-02-23 17:31:48 -0800 | [diff] [blame] | 372 | int sk; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 373 | |
| 374 | sk = socket(AF_INET, SOCK_DGRAM, 0); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 375 | if (sk < 0) |
| 376 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 377 | |
| 378 | memset(&ifr, 0, sizeof(ifr)); |
| 379 | strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); |
| 380 | |
| 381 | ifr.ifr_flags &= ~IFF_UP; |
| 382 | |
Sharvil Nanavati | f1c764f | 2015-02-23 17:31:48 -0800 | [diff] [blame] | 383 | ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 384 | |
| 385 | close(sk); |
| 386 | |
| 387 | return 0; |
| 388 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 389 | |
| 390 | void btpan_set_flow_control(BOOLEAN enable) { |
| 391 | if (btpan_cb.tap_fd == -1) |
| 392 | return; |
| 393 | |
| 394 | btpan_cb.flow = enable; |
| 395 | if (enable) { |
| 396 | btsock_thread_add_fd(pan_pth, btpan_cb.tap_fd, 0, SOCK_THREAD_FD_RD, 0); |
Arman Uguray | bb95452 | 2015-06-02 21:11:07 -0700 | [diff] [blame] | 397 | bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(btpan_cb.tap_fd)); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 401 | int btpan_tap_open() |
| 402 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 403 | struct ifreq ifr; |
| 404 | int fd, err; |
| 405 | const char *clonedev = "/dev/tun"; |
| 406 | |
| 407 | /* open the clone device */ |
| 408 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 409 | if ((fd = open(clonedev, O_RDWR)) < 0) |
| 410 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 411 | BTIF_TRACE_DEBUG("could not open %s, err:%d", clonedev, errno); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 412 | return fd; |
| 413 | } |
| 414 | |
| 415 | memset(&ifr, 0, sizeof(ifr)); |
| 416 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 417 | |
| 418 | strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ); |
| 419 | |
| 420 | /* try to create the device */ |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 421 | if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 422 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 423 | BTIF_TRACE_DEBUG("ioctl error:%d, errno:%s", err, strerror(errno)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 424 | close(fd); |
| 425 | return err; |
| 426 | } |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 427 | if (tap_if_up(TAP_IF_NAME, controller_get_interface()->get_address()) == 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 428 | { |
Zhenye Zhu | 95ca3c4 | 2014-08-26 14:26:56 -0700 | [diff] [blame] | 429 | int flags = fcntl(fd, F_GETFL, 0); |
| 430 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 431 | return fd; |
| 432 | } |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 433 | BTIF_TRACE_ERROR("can not bring up tap interface:%s", TAP_IF_NAME); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 434 | close(fd); |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 435 | return INVALID_FD; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 436 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 437 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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) |
| 440 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 441 | UNUSED(ext); |
| 442 | UNUSED(forward); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 443 | if (tap_fd != INVALID_FD) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 444 | { |
| 445 | tETH_HDR eth_hdr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 446 | memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN); |
| 447 | memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN); |
| 448 | eth_hdr.h_proto = htons(proto); |
Hemant Gupta | 2a52710 | 2014-10-15 19:59:23 +0530 | [diff] [blame] | 449 | char packet[TAP_MAX_PKT_WRITE_LEN + sizeof(tETH_HDR)]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 450 | memcpy(packet, ð_hdr, sizeof(tETH_HDR)); |
Hemant Gupta | 2a52710 | 2014-10-15 19:59:23 +0530 | [diff] [blame] | 451 | if (len > TAP_MAX_PKT_WRITE_LEN) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 452 | { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 453 | LOG_ERROR(LOG_TAG, "btpan_tap_send eth packet size:%d is exceeded limit!", len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 454 | return -1; |
| 455 | } |
| 456 | memcpy(packet + sizeof(tETH_HDR), buf, len); |
| 457 | |
| 458 | /* Send data to network interface */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 459 | int ret = write(tap_fd, packet, len + sizeof(tETH_HDR)); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 460 | BTIF_TRACE_DEBUG("ret:%d", ret); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 461 | return ret; |
| 462 | } |
| 463 | return -1; |
| 464 | |
| 465 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 466 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 467 | int btpan_tap_close(int fd) |
| 468 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 469 | if (tap_if_down(TAP_IF_NAME) == 0) |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 470 | close(fd); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 471 | if (pan_pth >= 0) |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 472 | btsock_thread_wakeup(pan_pth); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 473 | return 0; |
| 474 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 475 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 476 | btpan_conn_t * btpan_find_conn_handle(UINT16 handle) |
| 477 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 478 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
| 479 | { |
| 480 | if (btpan_cb.conns[i].handle == handle) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 481 | return &btpan_cb.conns[i]; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 482 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 483 | return NULL; |
| 484 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 485 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 486 | btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr) |
| 487 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 488 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
| 489 | { |
| 490 | if (memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 491 | return &btpan_cb.conns[i]; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 492 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 493 | return NULL; |
| 494 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 495 | |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 496 | static void btpan_open_conn(btpan_conn_t* conn, tBTA_PAN *p_data) |
| 497 | { |
| 498 | BTIF_TRACE_API("btpan_open_conn: local_role:%d, peer_role: %d, handle:%d, conn: %p", |
| 499 | p_data->open.local_role, p_data->open.peer_role, p_data->open.handle, conn); |
| 500 | |
| 501 | if (conn == NULL) |
| 502 | conn = btpan_new_conn(p_data->open.handle, p_data->open.bd_addr, p_data->open.local_role, |
| 503 | p_data->open.peer_role); |
| 504 | if (conn) |
| 505 | { |
| 506 | BTIF_TRACE_DEBUG("btpan_open_conn:tap_fd:%d, open_count:%d, " |
| 507 | "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d", |
| 508 | btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, p_data->open.handle, |
| 509 | conn->local_role, conn->remote_role); |
| 510 | |
| 511 | btpan_cb.open_count++; |
| 512 | conn->handle = p_data->open.handle; |
| 513 | if (btpan_cb.tap_fd < 0) |
| 514 | { |
| 515 | btpan_cb.tap_fd = btpan_tap_open(); |
| 516 | if(btpan_cb.tap_fd >= 0) |
| 517 | create_tap_read_thread(btpan_cb.tap_fd); |
| 518 | } |
| 519 | |
| 520 | if (btpan_cb.tap_fd >= 0) |
| 521 | { |
| 522 | btpan_cb.flow = 1; |
| 523 | conn->state = PAN_STATE_OPEN; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | static void btpan_close_conn(btpan_conn_t* conn) |
| 529 | { |
| 530 | BTIF_TRACE_API("btpan_close_conn: %p",conn); |
| 531 | |
| 532 | if (conn && conn->state == PAN_STATE_OPEN) |
| 533 | { |
| 534 | BTIF_TRACE_DEBUG("btpan_close_conn: PAN_STATE_OPEN"); |
| 535 | |
| 536 | conn->state = PAN_STATE_CLOSE; |
| 537 | btpan_cb.open_count--; |
| 538 | |
| 539 | if (btpan_cb.open_count == 0) |
| 540 | { |
| 541 | destroy_tap_read_thread(); |
| 542 | if (btpan_cb.tap_fd != INVALID_FD) |
| 543 | { |
| 544 | btpan_tap_close(btpan_cb.tap_fd); |
| 545 | btpan_cb.tap_fd = INVALID_FD; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 552 | static void btpan_cleanup_conn(btpan_conn_t* conn) |
| 553 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 554 | if (conn) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 555 | { |
| 556 | conn->handle = -1; |
| 557 | conn->state = -1; |
| 558 | memset(&conn->peer, 0, sizeof(conn->peer)); |
| 559 | memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); |
| 560 | conn->local_role = conn->remote_role = 0; |
| 561 | } |
| 562 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 563 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 564 | btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int remote_role) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 565 | { |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 566 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 567 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 568 | BTIF_TRACE_DEBUG("conns[%d]:%d", i, btpan_cb.conns[i].handle); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 569 | if (btpan_cb.conns[i].handle == -1) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 570 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 571 | BTIF_TRACE_DEBUG("handle:%d, local_role:%d, remote_role:%d", handle, local_role, remote_role); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 572 | |
| 573 | btpan_cb.conns[i].handle = handle; |
| 574 | bdcpy(btpan_cb.conns[i].peer, addr); |
| 575 | btpan_cb.conns[i].local_role = local_role; |
| 576 | btpan_cb.conns[i].remote_role = remote_role; |
| 577 | return &btpan_cb.conns[i]; |
| 578 | } |
| 579 | } |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 580 | BTIF_TRACE_DEBUG("MAX_PAN_CONNS:%d exceeded, return NULL as failed", MAX_PAN_CONNS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | void btpan_close_handle(btpan_conn_t *p) |
| 585 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 586 | BTIF_TRACE_DEBUG("btpan_close_handle : close handle %d", p->handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 587 | p->handle = -1; |
| 588 | p->local_role = -1; |
| 589 | p->remote_role = -1; |
| 590 | memset(&p->peer, 0, 6); |
| 591 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 592 | |
Sharvil Nanavati | da271ee | 2014-05-30 21:04:03 -0700 | [diff] [blame] | 593 | static inline bool should_forward(tETH_HDR* hdr) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 594 | { |
Sharvil Nanavati | da271ee | 2014-05-30 21:04:03 -0700 | [diff] [blame] | 595 | uint16_t proto = ntohs(hdr->h_proto); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 596 | if (proto == ETH_P_IP || proto == ETH_P_ARP || proto == ETH_P_IPV6) |
Sharvil Nanavati | da271ee | 2014-05-30 21:04:03 -0700 | [diff] [blame] | 597 | return true; |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 598 | BTIF_TRACE_DEBUG("unknown proto:%x", proto); |
Sharvil Nanavati | da271ee | 2014-05-30 21:04:03 -0700 | [diff] [blame] | 599 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 600 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 601 | |
| 602 | static int forward_bnep(tETH_HDR* eth_hdr, BT_HDR *hdr) { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 603 | int broadcast = eth_hdr->h_dest[0] & 1; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 604 | |
| 605 | // Find the right connection to send this frame over. |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 606 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
| 607 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 608 | UINT16 handle = btpan_cb.conns[i].handle; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 609 | if (handle != (UINT16)-1 && |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 610 | (broadcast || memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0 |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 611 | || memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0)) { |
| 612 | int result = PAN_WriteBuf(handle, eth_hdr->h_dest, eth_hdr->h_src, ntohs(eth_hdr->h_proto), hdr, 0); |
| 613 | switch (result) { |
| 614 | case PAN_Q_SIZE_EXCEEDED: |
| 615 | return FORWARD_CONGEST; |
| 616 | case PAN_SUCCESS: |
| 617 | return FORWARD_SUCCESS; |
| 618 | default: |
| 619 | return FORWARD_FAILURE; |
| 620 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 621 | } |
| 622 | } |
Pavlin Radoslavov | cceb430 | 2016-02-05 13:54:43 -0800 | [diff] [blame] | 623 | osi_free(hdr); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 624 | return FORWARD_IGNORE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void bta_pan_callback_transfer(UINT16 event, char *p_param) |
| 628 | { |
| 629 | tBTA_PAN *p_data = (tBTA_PAN *)p_param; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 630 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 631 | switch(event) |
| 632 | { |
| 633 | case BTA_PAN_ENABLE_EVT: |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 634 | BTIF_TRACE_DEBUG("BTA_PAN_ENABLE_EVT"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 635 | break; |
| 636 | case BTA_PAN_SET_ROLE_EVT: |
| 637 | { |
| 638 | int btpan_role = bta_role_to_btpan(p_data->set_role.role); |
| 639 | bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; |
| 640 | btpan_control_state_t state = btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; |
| 641 | callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); |
| 642 | break; |
| 643 | } |
| 644 | case BTA_PAN_OPENING_EVT: |
| 645 | { |
| 646 | btpan_conn_t* conn; |
| 647 | bdstr_t bds; |
Sharvil Nanavati | 8a6a89f | 2014-08-20 09:39:25 -0700 | [diff] [blame] | 648 | bdaddr_to_string((bt_bdaddr_t *)p_data->opening.bd_addr, bds, sizeof(bds)); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 649 | BTIF_TRACE_DEBUG("BTA_PAN_OPENING_EVT handle %d, addr: %s", p_data->opening.handle, bds); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 650 | conn = btpan_find_conn_addr(p_data->opening.bd_addr); |
| 651 | |
| 652 | asrt(conn != NULL); |
| 653 | if (conn) |
| 654 | { |
| 655 | conn->handle = p_data->opening.handle; |
| 656 | int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); |
| 657 | int btpan_remote_role = bta_role_to_btpan(conn->remote_role); |
| 658 | callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, |
| 659 | (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, btpan_remote_role); |
| 660 | } |
| 661 | else |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 662 | BTIF_TRACE_ERROR("connection not found"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 663 | break; |
| 664 | } |
| 665 | case BTA_PAN_OPEN_EVT: |
| 666 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 667 | btpan_connection_state_t state; |
| 668 | bt_status_t status; |
Nitin Shivpure | e85eb5a | 2013-10-03 19:06:12 +0530 | [diff] [blame] | 669 | btpan_conn_t *conn = btpan_find_conn_handle(p_data->open.handle); |
| 670 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 671 | LOG_VERBOSE(LOG_TAG, "%s pan connection open status: %d", __func__, p_data->open.status); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 672 | if (p_data->open.status == BTA_PAN_SUCCESS) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 673 | { |
| 674 | state = BTPAN_STATE_CONNECTED; |
| 675 | status = BT_STATUS_SUCCESS; |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 676 | btpan_open_conn(conn, p_data); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 677 | } |
| 678 | else |
| 679 | { |
| 680 | state = BTPAN_STATE_DISCONNECTED; |
| 681 | status = BT_STATUS_FAIL; |
Nitin Shivpure | e85eb5a | 2013-10-03 19:06:12 +0530 | [diff] [blame] | 682 | btpan_cleanup_conn(conn); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 683 | } |
The Android Open Source Project | 689d66b | 2012-12-12 17:18:15 -0800 | [diff] [blame] | 684 | /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, conn); */ |
| 685 | /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, conn->remote_role); */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 686 | int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 687 | int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); |
| 688 | callback.connection_state_cb(state, status, (const bt_bdaddr_t*)p_data->open.bd_addr, |
| 689 | btpan_conn_local_role, btpan_remote_role); |
| 690 | break; |
| 691 | } |
| 692 | case BTA_PAN_CLOSE_EVT: |
| 693 | { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 694 | LOG_INFO(LOG_TAG, "%s: event = BTA_PAN_CLOSE_EVT handle %d", __FUNCTION__, p_data->close.handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 695 | btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 696 | btpan_close_conn(conn); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 697 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 698 | if (conn && conn->handle >= 0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 699 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 700 | int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); |
| 701 | int btpan_remote_role = bta_role_to_btpan(conn->remote_role); |
| 702 | callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, 0, (const bt_bdaddr_t*)conn->peer, |
| 703 | btpan_conn_local_role, btpan_remote_role); |
| 704 | btpan_cleanup_conn(conn); |
| 705 | } |
| 706 | else |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 707 | BTIF_TRACE_ERROR("pan handle not found (%d)", p_data->close.handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 708 | break; |
| 709 | } |
| 710 | default: |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 711 | BTIF_TRACE_WARNING("Unknown pan event %d", event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 712 | break; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data) |
| 717 | { |
| 718 | btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, sizeof(tBTA_PAN), NULL); |
| 719 | } |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 720 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 721 | #define IS_EXCEPTION(e) ((e) & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)) |
| 722 | static void btu_exec_tap_fd_read(void *p_param) { |
| 723 | struct pollfd ufd; |
Arman Uguray | bb95452 | 2015-06-02 21:11:07 -0700 | [diff] [blame] | 724 | int fd = PTR_TO_INT(p_param); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 725 | |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 726 | if (fd == INVALID_FD || fd != btpan_cb.tap_fd) |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 727 | return; |
| 728 | |
Pavlin Radoslavov | 70ae7de | 2015-09-23 14:49:24 -0700 | [diff] [blame] | 729 | // Don't occupy BTU context too long, avoid buffer overruns and |
Zhenye Zhu | 95ca3c4 | 2014-08-26 14:26:56 -0700 | [diff] [blame] | 730 | // give other profiles a chance to run by limiting the amount of memory |
Pavlin Radoslavov | 70ae7de | 2015-09-23 14:49:24 -0700 | [diff] [blame] | 731 | // PAN can use. |
| 732 | for (int i = 0; i < PAN_BUF_MAX && btif_is_enabled() && btpan_cb.flow; i++) { |
Pavlin Radoslavov | cceb430 | 2016-02-05 13:54:43 -0800 | [diff] [blame] | 733 | BT_HDR *buffer = (BT_HDR *)osi_malloc(PAN_BUF_SIZE); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 734 | buffer->offset = PAN_MINIMUM_OFFSET; |
Pavlin Radoslavov | 20524d3 | 2016-02-02 18:12:08 -0800 | [diff] [blame] | 735 | buffer->len = PAN_BUF_SIZE - sizeof(BT_HDR) - buffer->offset; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 736 | |
| 737 | UINT8 *packet = (UINT8 *)buffer + sizeof(BT_HDR) + buffer->offset; |
| 738 | |
| 739 | // If we don't have an undelivered packet left over, pull one from the TAP driver. |
| 740 | // We save it in the congest_packet right away in case we can't deliver it in this |
| 741 | // attempt. |
| 742 | if (!btpan_cb.congest_packet_size) { |
| 743 | ssize_t ret = read(fd, btpan_cb.congest_packet, sizeof(btpan_cb.congest_packet)); |
| 744 | switch (ret) { |
| 745 | case -1: |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 746 | BTIF_TRACE_ERROR("%s unable to read from driver: %s", __func__, strerror(errno)); |
Pavlin Radoslavov | cceb430 | 2016-02-05 13:54:43 -0800 | [diff] [blame] | 747 | osi_free(buffer); |
Zhenye Zhu | 285f7c6 | 2014-11-25 15:51:07 -0800 | [diff] [blame] | 748 | //add fd back to monitor thread to try it again later |
| 749 | btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 750 | return; |
| 751 | case 0: |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 752 | BTIF_TRACE_WARNING("%s end of file reached.", __func__); |
Pavlin Radoslavov | cceb430 | 2016-02-05 13:54:43 -0800 | [diff] [blame] | 753 | osi_free(buffer); |
Zhenye Zhu | 285f7c6 | 2014-11-25 15:51:07 -0800 | [diff] [blame] | 754 | //add fd back to monitor thread to process the exception |
| 755 | btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 756 | return; |
| 757 | default: |
| 758 | btpan_cb.congest_packet_size = ret; |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | memcpy(packet, btpan_cb.congest_packet, MIN(btpan_cb.congest_packet_size, buffer->len)); |
| 764 | buffer->len = MIN(btpan_cb.congest_packet_size, buffer->len); |
| 765 | |
| 766 | if (buffer->len > sizeof(tETH_HDR) && should_forward((tETH_HDR *)packet)) { |
| 767 | // Extract the ethernet header from the buffer since the PAN_WriteBuf inside |
| 768 | // forward_bnep can't handle two pointers that point inside the same GKI buffer. |
| 769 | tETH_HDR hdr; |
| 770 | memcpy(&hdr, packet, sizeof(tETH_HDR)); |
| 771 | |
| 772 | // Skip the ethernet header. |
| 773 | buffer->len -= sizeof(tETH_HDR); |
| 774 | buffer->offset += sizeof(tETH_HDR); |
| 775 | if (forward_bnep(&hdr, buffer) != FORWARD_CONGEST) |
| 776 | btpan_cb.congest_packet_size = 0; |
| 777 | } else { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 778 | BTIF_TRACE_WARNING("%s dropping packet of length %d", __func__, buffer->len); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 779 | btpan_cb.congest_packet_size = 0; |
Pavlin Radoslavov | cceb430 | 2016-02-05 13:54:43 -0800 | [diff] [blame] | 780 | osi_free(buffer); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // Bail out of the loop if reading from the TAP fd would block. |
| 784 | ufd.fd = fd; |
| 785 | ufd.events = POLLIN; |
| 786 | ufd.revents = 0; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 787 | if (poll(&ufd, 1, 0) <= 0 || IS_EXCEPTION(ufd.revents)) |
Zhenye Zhu | 285f7c6 | 2014-11-25 15:51:07 -0800 | [diff] [blame] | 788 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 789 | } |
Srinu Jella | 7baa1e1 | 2015-08-11 19:16:52 +0530 | [diff] [blame^] | 790 | |
| 791 | if (btpan_cb.flow) { |
| 792 | //add fd back to monitor thread when the flow is on |
| 793 | btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); |
| 794 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 797 | static void btif_pan_close_all_conns() { |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 798 | if (!stack_initialized) |
| 799 | return; |
| 800 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 801 | for (int i = 0; i < MAX_PAN_CONNS; ++i) |
| 802 | { |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 803 | if (btpan_cb.conns[i].handle != -1) |
| 804 | BTA_PanClose(btpan_cb.conns[i].handle); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 805 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id) { |
Chris Manton | 8d546df | 2014-08-12 14:11:38 -0700 | [diff] [blame] | 809 | assert(btpan_cb.tap_fd == INVALID_FD || btpan_cb.tap_fd == fd); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 810 | |
Chris Manton | 8d546df | 2014-08-12 14:11:38 -0700 | [diff] [blame] | 811 | if (btpan_cb.tap_fd != fd) { |
| 812 | BTIF_TRACE_WARNING("%s Signaled on mismatched fds exp:%d act:%d\n", |
| 813 | __func__, btpan_cb.tap_fd, fd); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 814 | return; |
Chris Manton | 8d546df | 2014-08-12 14:11:38 -0700 | [diff] [blame] | 815 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 816 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 817 | if (flags & SOCK_THREAD_FD_EXCEPTION) { |
VenkatRaghavan VijayaRaghavan | 4540f59 | 2015-02-05 04:40:47 -0800 | [diff] [blame] | 818 | btpan_cb.tap_fd = INVALID_FD; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 819 | btpan_tap_close(fd); |
| 820 | btif_pan_close_all_conns(); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 821 | } else if (flags & SOCK_THREAD_FD_RD) |
Arman Uguray | bb95452 | 2015-06-02 21:11:07 -0700 | [diff] [blame] | 822 | bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(fd)); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 823 | } |