michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* michael@0: * Copyright 2009, The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * michael@0: * NOTE: Due to being based on the dbus compatibility layer for michael@0: * android's bluetooth implementation, this file is licensed under the michael@0: * apache license instead of MPL. michael@0: * michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #ifdef MOZ_B2G_BT_BLUEZ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: #include "BluetoothUnixSocketConnector.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: using namespace mozilla::ipc; michael@0: USING_BLUETOOTH_NAMESPACE michael@0: michael@0: static const int RFCOMM_SO_SNDBUF = 70 * 1024; // 70 KB send buffer michael@0: static const int L2CAP_SO_SNDBUF = 400 * 1024; // 400 KB send buffer michael@0: static const int L2CAP_SO_RCVBUF = 400 * 1024; // 400 KB receive buffer michael@0: static const int L2CAP_MAX_MTU = 65000; michael@0: michael@0: #ifdef MOZ_B2G_BT_BLUEZ michael@0: static michael@0: int get_bdaddr(const char *str, bdaddr_t *ba) michael@0: { michael@0: char *d = ((char*)ba) + 5, *endp; michael@0: for (int i = 0; i < 6; i++) { michael@0: *d-- = strtol(str, &endp, 16); michael@0: MOZ_ASSERT(!(*endp != ':' && i != 5)); michael@0: str = endp + 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static michael@0: void get_bdaddr_as_string(const bdaddr_t *ba, char *str) { michael@0: const uint8_t *b = (const uint8_t *)ba; michael@0: sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", michael@0: b[5], b[4], b[3], b[2], b[1], b[0]); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: BluetoothUnixSocketConnector::BluetoothUnixSocketConnector( michael@0: BluetoothSocketType aType, michael@0: int aChannel, michael@0: bool aAuth, michael@0: bool aEncrypt) : mType(aType) michael@0: , mChannel(aChannel) michael@0: , mAuth(aAuth) michael@0: , mEncrypt(aEncrypt) michael@0: { michael@0: } michael@0: michael@0: bool michael@0: BluetoothUnixSocketConnector::SetUp(int aFd) michael@0: { michael@0: #ifdef MOZ_B2G_BT_BLUEZ michael@0: int lm = 0; michael@0: int sndbuf, rcvbuf; michael@0: michael@0: /* kernel does not yet support LM for SCO */ michael@0: switch (mType) { michael@0: case BluetoothSocketType::RFCOMM: michael@0: lm |= mAuth ? RFCOMM_LM_AUTH : 0; michael@0: lm |= mEncrypt ? RFCOMM_LM_ENCRYPT : 0; michael@0: break; michael@0: case BluetoothSocketType::L2CAP: michael@0: case BluetoothSocketType::EL2CAP: michael@0: lm |= mAuth ? L2CAP_LM_AUTH : 0; michael@0: lm |= mEncrypt ? L2CAP_LM_ENCRYPT : 0; michael@0: break; michael@0: case BluetoothSocketType::SCO: michael@0: break; michael@0: default: michael@0: MOZ_CRASH("Unknown socket type!"); michael@0: } michael@0: michael@0: if (lm) { michael@0: if (mType == BluetoothSocketType::RFCOMM) { michael@0: if (setsockopt(aFd, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm))) { michael@0: BT_WARNING("setsockopt(RFCOMM_LM) failed, throwing"); michael@0: return false; michael@0: } michael@0: } else if (mType == BluetoothSocketType::L2CAP || michael@0: mType == BluetoothSocketType::EL2CAP) { michael@0: if (setsockopt(aFd, SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm))) { michael@0: BT_WARNING("setsockopt(L2CAP_LM) failed, throwing"); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (mType == BluetoothSocketType::RFCOMM) { michael@0: sndbuf = RFCOMM_SO_SNDBUF; michael@0: if (setsockopt(aFd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) { michael@0: BT_WARNING("setsockopt(SO_SNDBUF) failed, throwing"); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: /* Setting L2CAP socket options */ michael@0: if (mType == BluetoothSocketType::L2CAP || michael@0: mType == BluetoothSocketType::EL2CAP) { michael@0: struct l2cap_options opts; michael@0: socklen_t optlen = sizeof(opts); michael@0: int err; michael@0: err = getsockopt(aFd, SOL_L2CAP, L2CAP_OPTIONS, &opts, &optlen); michael@0: if (!err) { michael@0: /* setting MTU for [E]L2CAP */ michael@0: opts.omtu = opts.imtu = L2CAP_MAX_MTU; michael@0: michael@0: /* Enable ERTM for [E]L2CAP */ michael@0: if (mType == BluetoothSocketType::EL2CAP) { michael@0: opts.flush_to = 0xffff; /* infinite */ michael@0: opts.mode = L2CAP_MODE_ERTM; michael@0: opts.fcs = 1; michael@0: opts.txwin_size = 64; michael@0: opts.max_tx = 10; michael@0: } michael@0: michael@0: err = setsockopt(aFd, SOL_L2CAP, L2CAP_OPTIONS, &opts, optlen); michael@0: } michael@0: michael@0: /* Set larger SNDBUF & RCVBUF for EL2CAP connections */ michael@0: if (mType == BluetoothSocketType::EL2CAP) { michael@0: sndbuf = L2CAP_SO_SNDBUF; michael@0: if (setsockopt(aFd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) { michael@0: BT_WARNING("setsockopt(SO_SNDBUF) failed, throwing"); michael@0: return false; michael@0: } michael@0: michael@0: rcvbuf = L2CAP_SO_RCVBUF; michael@0: if (setsockopt(aFd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf))) { michael@0: BT_WARNING("setsockopt(SO_RCVBUF) failed, throwing"); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: BluetoothUnixSocketConnector::SetUpListenSocket(int aFd) michael@0: { michael@0: // Nothing to do here. michael@0: return true; michael@0: } michael@0: michael@0: int michael@0: BluetoothUnixSocketConnector::Create() michael@0: { michael@0: MOZ_ASSERT(!NS_IsMainThread()); michael@0: int fd = -1; michael@0: michael@0: #ifdef MOZ_B2G_BT_BLUEZ michael@0: switch (mType) { michael@0: case BluetoothSocketType::RFCOMM: michael@0: fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); michael@0: break; michael@0: case BluetoothSocketType::SCO: michael@0: fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO); michael@0: break; michael@0: case BluetoothSocketType::L2CAP: michael@0: fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); michael@0: break; michael@0: case BluetoothSocketType::EL2CAP: michael@0: fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_L2CAP); michael@0: break; michael@0: default: michael@0: MOZ_CRASH(); michael@0: } michael@0: michael@0: if (fd < 0) { michael@0: BT_WARNING("Could not open bluetooth socket!"); michael@0: return -1; michael@0: } michael@0: michael@0: if (!SetUp(fd)) { michael@0: BT_WARNING("Could not set up socket!"); michael@0: return -1; michael@0: } michael@0: #endif michael@0: return fd; michael@0: } michael@0: michael@0: bool michael@0: BluetoothUnixSocketConnector::CreateAddr(bool aIsServer, michael@0: socklen_t& aAddrSize, michael@0: sockaddr_any& aAddr, michael@0: const char* aAddress) michael@0: { michael@0: #ifdef MOZ_B2G_BT_BLUEZ michael@0: // Set to BDADDR_ANY, if it's not a server, we'll reset. michael@0: bdaddr_t bd_address_obj = {{0, 0, 0, 0, 0, 0}}; michael@0: michael@0: if (!aIsServer && aAddress && strlen(aAddress) > 0) { michael@0: if (get_bdaddr(aAddress, &bd_address_obj)) { michael@0: BT_WARNING("Can't get bluetooth address!"); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // Initialize michael@0: memset(&aAddr, 0, sizeof(aAddr)); michael@0: michael@0: switch (mType) { michael@0: case BluetoothSocketType::RFCOMM: michael@0: struct sockaddr_rc addr_rc; michael@0: aAddrSize = sizeof(addr_rc); michael@0: aAddr.rc.rc_family = AF_BLUETOOTH; michael@0: aAddr.rc.rc_channel = mChannel; michael@0: memcpy(&aAddr.rc.rc_bdaddr, &bd_address_obj, sizeof(bd_address_obj)); michael@0: break; michael@0: case BluetoothSocketType::L2CAP: michael@0: case BluetoothSocketType::EL2CAP: michael@0: struct sockaddr_l2 addr_l2; michael@0: aAddrSize = sizeof(addr_l2); michael@0: aAddr.l2.l2_family = AF_BLUETOOTH; michael@0: aAddr.l2.l2_psm = mChannel; michael@0: memcpy(&aAddr.l2.l2_bdaddr, &bd_address_obj, sizeof(bdaddr_t)); michael@0: break; michael@0: case BluetoothSocketType::SCO: michael@0: struct sockaddr_sco addr_sco; michael@0: aAddrSize = sizeof(addr_sco); michael@0: aAddr.sco.sco_family = AF_BLUETOOTH; michael@0: memcpy(&aAddr.sco.sco_bdaddr, &bd_address_obj, sizeof(bd_address_obj)); michael@0: break; michael@0: default: michael@0: BT_WARNING("Socket type unknown!"); michael@0: return false; michael@0: } michael@0: #endif michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: BluetoothUnixSocketConnector::GetSocketAddr(const sockaddr_any& aAddr, michael@0: nsAString& aAddrStr) michael@0: { michael@0: #ifdef MOZ_B2G_BT_BLUEZ michael@0: char addr[18]; michael@0: switch (mType) { michael@0: case BluetoothSocketType::RFCOMM: michael@0: get_bdaddr_as_string((bdaddr_t*)(&aAddr.rc.rc_bdaddr), addr); michael@0: break; michael@0: case BluetoothSocketType::SCO: michael@0: get_bdaddr_as_string((bdaddr_t*)(&aAddr.sco.sco_bdaddr), addr); michael@0: break; michael@0: case BluetoothSocketType::L2CAP: michael@0: case BluetoothSocketType::EL2CAP: michael@0: get_bdaddr_as_string((bdaddr_t*)(&aAddr.l2.l2_bdaddr), addr); michael@0: break; michael@0: default: michael@0: MOZ_CRASH("Socket should be either RFCOMM or SCO!"); michael@0: } michael@0: aAddrStr.AssignASCII(addr); michael@0: #endif michael@0: }