1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bluetooth/bluez/BluetoothUnixSocketConnector.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,285 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* 1.7 + * Copyright 2009, The Android Open Source Project 1.8 + * 1.9 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.10 + * you may not use this file except in compliance with the License. 1.11 + * You may obtain a copy of the License at 1.12 + * 1.13 + * http://www.apache.org/licenses/LICENSE-2.0 1.14 + * 1.15 + * Unless required by applicable law or agreed to in writing, software 1.16 + * distributed under the License is distributed on an "AS IS" BASIS, 1.17 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.18 + * See the License for the specific language governing permissions and 1.19 + * limitations under the License. 1.20 + * 1.21 + * NOTE: Due to being based on the dbus compatibility layer for 1.22 + * android's bluetooth implementation, this file is licensed under the 1.23 + * apache license instead of MPL. 1.24 + * 1.25 + */ 1.26 + 1.27 +#include <fcntl.h> 1.28 +#include <unistd.h> 1.29 +#include <stdlib.h> 1.30 +#include <errno.h> 1.31 + 1.32 +#include <sys/socket.h> 1.33 +#ifdef MOZ_B2G_BT_BLUEZ 1.34 +#include <bluetooth/bluetooth.h> 1.35 +#include <bluetooth/l2cap.h> 1.36 +#include <bluetooth/rfcomm.h> 1.37 +#include <bluetooth/sco.h> 1.38 +#endif 1.39 +#include "BluetoothUnixSocketConnector.h" 1.40 +#include "nsThreadUtils.h" 1.41 + 1.42 +using namespace mozilla::ipc; 1.43 +USING_BLUETOOTH_NAMESPACE 1.44 + 1.45 +static const int RFCOMM_SO_SNDBUF = 70 * 1024; // 70 KB send buffer 1.46 +static const int L2CAP_SO_SNDBUF = 400 * 1024; // 400 KB send buffer 1.47 +static const int L2CAP_SO_RCVBUF = 400 * 1024; // 400 KB receive buffer 1.48 +static const int L2CAP_MAX_MTU = 65000; 1.49 + 1.50 +#ifdef MOZ_B2G_BT_BLUEZ 1.51 +static 1.52 +int get_bdaddr(const char *str, bdaddr_t *ba) 1.53 +{ 1.54 + char *d = ((char*)ba) + 5, *endp; 1.55 + for (int i = 0; i < 6; i++) { 1.56 + *d-- = strtol(str, &endp, 16); 1.57 + MOZ_ASSERT(!(*endp != ':' && i != 5)); 1.58 + str = endp + 1; 1.59 + } 1.60 + return 0; 1.61 +} 1.62 + 1.63 +static 1.64 +void get_bdaddr_as_string(const bdaddr_t *ba, char *str) { 1.65 + const uint8_t *b = (const uint8_t *)ba; 1.66 + sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", 1.67 + b[5], b[4], b[3], b[2], b[1], b[0]); 1.68 +} 1.69 + 1.70 +#endif 1.71 + 1.72 +BluetoothUnixSocketConnector::BluetoothUnixSocketConnector( 1.73 + BluetoothSocketType aType, 1.74 + int aChannel, 1.75 + bool aAuth, 1.76 + bool aEncrypt) : mType(aType) 1.77 + , mChannel(aChannel) 1.78 + , mAuth(aAuth) 1.79 + , mEncrypt(aEncrypt) 1.80 +{ 1.81 +} 1.82 + 1.83 +bool 1.84 +BluetoothUnixSocketConnector::SetUp(int aFd) 1.85 +{ 1.86 +#ifdef MOZ_B2G_BT_BLUEZ 1.87 + int lm = 0; 1.88 + int sndbuf, rcvbuf; 1.89 + 1.90 + /* kernel does not yet support LM for SCO */ 1.91 + switch (mType) { 1.92 + case BluetoothSocketType::RFCOMM: 1.93 + lm |= mAuth ? RFCOMM_LM_AUTH : 0; 1.94 + lm |= mEncrypt ? RFCOMM_LM_ENCRYPT : 0; 1.95 + break; 1.96 + case BluetoothSocketType::L2CAP: 1.97 + case BluetoothSocketType::EL2CAP: 1.98 + lm |= mAuth ? L2CAP_LM_AUTH : 0; 1.99 + lm |= mEncrypt ? L2CAP_LM_ENCRYPT : 0; 1.100 + break; 1.101 + case BluetoothSocketType::SCO: 1.102 + break; 1.103 + default: 1.104 + MOZ_CRASH("Unknown socket type!"); 1.105 + } 1.106 + 1.107 + if (lm) { 1.108 + if (mType == BluetoothSocketType::RFCOMM) { 1.109 + if (setsockopt(aFd, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm))) { 1.110 + BT_WARNING("setsockopt(RFCOMM_LM) failed, throwing"); 1.111 + return false; 1.112 + } 1.113 + } else if (mType == BluetoothSocketType::L2CAP || 1.114 + mType == BluetoothSocketType::EL2CAP) { 1.115 + if (setsockopt(aFd, SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm))) { 1.116 + BT_WARNING("setsockopt(L2CAP_LM) failed, throwing"); 1.117 + return false; 1.118 + } 1.119 + } 1.120 + } 1.121 + 1.122 + if (mType == BluetoothSocketType::RFCOMM) { 1.123 + sndbuf = RFCOMM_SO_SNDBUF; 1.124 + if (setsockopt(aFd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) { 1.125 + BT_WARNING("setsockopt(SO_SNDBUF) failed, throwing"); 1.126 + return false; 1.127 + } 1.128 + } 1.129 + 1.130 + /* Setting L2CAP socket options */ 1.131 + if (mType == BluetoothSocketType::L2CAP || 1.132 + mType == BluetoothSocketType::EL2CAP) { 1.133 + struct l2cap_options opts; 1.134 + socklen_t optlen = sizeof(opts); 1.135 + int err; 1.136 + err = getsockopt(aFd, SOL_L2CAP, L2CAP_OPTIONS, &opts, &optlen); 1.137 + if (!err) { 1.138 + /* setting MTU for [E]L2CAP */ 1.139 + opts.omtu = opts.imtu = L2CAP_MAX_MTU; 1.140 + 1.141 + /* Enable ERTM for [E]L2CAP */ 1.142 + if (mType == BluetoothSocketType::EL2CAP) { 1.143 + opts.flush_to = 0xffff; /* infinite */ 1.144 + opts.mode = L2CAP_MODE_ERTM; 1.145 + opts.fcs = 1; 1.146 + opts.txwin_size = 64; 1.147 + opts.max_tx = 10; 1.148 + } 1.149 + 1.150 + err = setsockopt(aFd, SOL_L2CAP, L2CAP_OPTIONS, &opts, optlen); 1.151 + } 1.152 + 1.153 + /* Set larger SNDBUF & RCVBUF for EL2CAP connections */ 1.154 + if (mType == BluetoothSocketType::EL2CAP) { 1.155 + sndbuf = L2CAP_SO_SNDBUF; 1.156 + if (setsockopt(aFd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) { 1.157 + BT_WARNING("setsockopt(SO_SNDBUF) failed, throwing"); 1.158 + return false; 1.159 + } 1.160 + 1.161 + rcvbuf = L2CAP_SO_RCVBUF; 1.162 + if (setsockopt(aFd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf))) { 1.163 + BT_WARNING("setsockopt(SO_RCVBUF) failed, throwing"); 1.164 + return false; 1.165 + } 1.166 + } 1.167 + } 1.168 +#endif 1.169 + return true; 1.170 +} 1.171 + 1.172 +bool 1.173 +BluetoothUnixSocketConnector::SetUpListenSocket(int aFd) 1.174 +{ 1.175 + // Nothing to do here. 1.176 + return true; 1.177 +} 1.178 + 1.179 +int 1.180 +BluetoothUnixSocketConnector::Create() 1.181 +{ 1.182 + MOZ_ASSERT(!NS_IsMainThread()); 1.183 + int fd = -1; 1.184 + 1.185 +#ifdef MOZ_B2G_BT_BLUEZ 1.186 + switch (mType) { 1.187 + case BluetoothSocketType::RFCOMM: 1.188 + fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 1.189 + break; 1.190 + case BluetoothSocketType::SCO: 1.191 + fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO); 1.192 + break; 1.193 + case BluetoothSocketType::L2CAP: 1.194 + fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); 1.195 + break; 1.196 + case BluetoothSocketType::EL2CAP: 1.197 + fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_L2CAP); 1.198 + break; 1.199 + default: 1.200 + MOZ_CRASH(); 1.201 + } 1.202 + 1.203 + if (fd < 0) { 1.204 + BT_WARNING("Could not open bluetooth socket!"); 1.205 + return -1; 1.206 + } 1.207 + 1.208 + if (!SetUp(fd)) { 1.209 + BT_WARNING("Could not set up socket!"); 1.210 + return -1; 1.211 + } 1.212 +#endif 1.213 + return fd; 1.214 +} 1.215 + 1.216 +bool 1.217 +BluetoothUnixSocketConnector::CreateAddr(bool aIsServer, 1.218 + socklen_t& aAddrSize, 1.219 + sockaddr_any& aAddr, 1.220 + const char* aAddress) 1.221 +{ 1.222 +#ifdef MOZ_B2G_BT_BLUEZ 1.223 + // Set to BDADDR_ANY, if it's not a server, we'll reset. 1.224 + bdaddr_t bd_address_obj = {{0, 0, 0, 0, 0, 0}}; 1.225 + 1.226 + if (!aIsServer && aAddress && strlen(aAddress) > 0) { 1.227 + if (get_bdaddr(aAddress, &bd_address_obj)) { 1.228 + BT_WARNING("Can't get bluetooth address!"); 1.229 + return false; 1.230 + } 1.231 + } 1.232 + 1.233 + // Initialize 1.234 + memset(&aAddr, 0, sizeof(aAddr)); 1.235 + 1.236 + switch (mType) { 1.237 + case BluetoothSocketType::RFCOMM: 1.238 + struct sockaddr_rc addr_rc; 1.239 + aAddrSize = sizeof(addr_rc); 1.240 + aAddr.rc.rc_family = AF_BLUETOOTH; 1.241 + aAddr.rc.rc_channel = mChannel; 1.242 + memcpy(&aAddr.rc.rc_bdaddr, &bd_address_obj, sizeof(bd_address_obj)); 1.243 + break; 1.244 + case BluetoothSocketType::L2CAP: 1.245 + case BluetoothSocketType::EL2CAP: 1.246 + struct sockaddr_l2 addr_l2; 1.247 + aAddrSize = sizeof(addr_l2); 1.248 + aAddr.l2.l2_family = AF_BLUETOOTH; 1.249 + aAddr.l2.l2_psm = mChannel; 1.250 + memcpy(&aAddr.l2.l2_bdaddr, &bd_address_obj, sizeof(bdaddr_t)); 1.251 + break; 1.252 + case BluetoothSocketType::SCO: 1.253 + struct sockaddr_sco addr_sco; 1.254 + aAddrSize = sizeof(addr_sco); 1.255 + aAddr.sco.sco_family = AF_BLUETOOTH; 1.256 + memcpy(&aAddr.sco.sco_bdaddr, &bd_address_obj, sizeof(bd_address_obj)); 1.257 + break; 1.258 + default: 1.259 + BT_WARNING("Socket type unknown!"); 1.260 + return false; 1.261 + } 1.262 +#endif 1.263 + return true; 1.264 +} 1.265 + 1.266 +void 1.267 +BluetoothUnixSocketConnector::GetSocketAddr(const sockaddr_any& aAddr, 1.268 + nsAString& aAddrStr) 1.269 +{ 1.270 +#ifdef MOZ_B2G_BT_BLUEZ 1.271 + char addr[18]; 1.272 + switch (mType) { 1.273 + case BluetoothSocketType::RFCOMM: 1.274 + get_bdaddr_as_string((bdaddr_t*)(&aAddr.rc.rc_bdaddr), addr); 1.275 + break; 1.276 + case BluetoothSocketType::SCO: 1.277 + get_bdaddr_as_string((bdaddr_t*)(&aAddr.sco.sco_bdaddr), addr); 1.278 + break; 1.279 + case BluetoothSocketType::L2CAP: 1.280 + case BluetoothSocketType::EL2CAP: 1.281 + get_bdaddr_as_string((bdaddr_t*)(&aAddr.l2.l2_bdaddr), addr); 1.282 + break; 1.283 + default: 1.284 + MOZ_CRASH("Socket should be either RFCOMM or SCO!"); 1.285 + } 1.286 + aAddrStr.AssignASCII(addr); 1.287 +#endif 1.288 +}