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