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