Bug 756299: Patch 1 - IPC Socket Functions; r=mrbkap

This commit is contained in:
Kyle Machulis 2012-09-05 20:06:06 -07:00
Родитель c03d084401
Коммит d403f38686
6 изменённых файлов: 236 добавлений и 3 удалений

Просмотреть файл

@ -191,7 +191,7 @@ if test -n "$gonkdir" ; then
;;
esac
CPPFLAGS="-DANDROID -isystem $gonkdir/bionic/libc/$ARCH_DIR/include -isystem $gonkdir/bionic/libc/include/ -isystem $gonkdir/bionic/libc/kernel/common -isystem $gonkdir/bionic/libc/kernel/$ARCH_DIR -isystem $gonkdir/bionic/libm/include -I$gonkdir/frameworks/base/opengl/include -I$gonkdir/frameworks/base/native/include -I$gonkdir/hardware/libhardware/include -I$gonkdir/hardware/libhardware_legacy/include -I$gonkdir/system -I$gonkdir/system/core/include -isystem $gonkdir/bionic -I$gonkdir/frameworks/base/include -I$gonkdir/external/dbus $CPPFLAGS -I$gonkdir/frameworks/base/services/sensorservice -I$gonkdir/frameworks/base/services/camera -I$gonkdir/system/media/wilhelm/include"
CPPFLAGS="-DANDROID -isystem $gonkdir/bionic/libc/$ARCH_DIR/include -isystem $gonkdir/bionic/libc/include/ -isystem $gonkdir/bionic/libc/kernel/common -isystem $gonkdir/bionic/libc/kernel/$ARCH_DIR -isystem $gonkdir/bionic/libm/include -I$gonkdir/frameworks/base/opengl/include -I$gonkdir/frameworks/base/native/include -I$gonkdir/hardware/libhardware/include -I$gonkdir/hardware/libhardware_legacy/include -I$gonkdir/system -I$gonkdir/system/core/include -isystem $gonkdir/bionic -I$gonkdir/frameworks/base/include -I$gonkdir/external/dbus -I$gonkdir/external/bluetooth/bluez/lib $CPPFLAGS -I$gonkdir/frameworks/base/services/sensorservice -I$gonkdir/frameworks/base/services/camera -I$gonkdir/system/media/wilhelm/include"
CFLAGS="-mandroid -fno-short-enums -fno-exceptions $CFLAGS"
CXXFLAGS="-mandroid -fno-short-enums -fno-exceptions -Wno-psabi $CXXFLAGS $STLPORT_CPPFLAGS"
dnl Add -llog by default, since we use it all over the place.

Просмотреть файл

@ -16,7 +16,7 @@ DIRS += ril
endif #}
ifdef MOZ_B2G_BT #{
DIRS += dbus
DIRS += dbus socket
endif #}
ifeq (gonk,$(MOZ_WIDGET_TOOLKIT))

31
ipc/socket/Makefile.in Normal file
Просмотреть файл

@ -0,0 +1,31 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DEPTH = ../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = ipc
LIBRARY_NAME = mozipcsocket_s
FORCE_STATIC_LIB = 1
LIBXUL_LIBRARY = 1
EXPORT_LIBRARY = 1
EXPORTS_NAMESPACES = mozilla/ipc
EXPORTS_mozilla/ipc = \
Socket.h \
$(NULL)
CPPSRCS += \
Socket.cpp \
$(NULL)
include $(topsrcdir)/config/config.mk
include $(topsrcdir)/ipc/chromium/chromium-config.mk
include $(topsrcdir)/config/rules.mk

180
ipc/socket/Socket.cpp Normal file
Просмотреть файл

@ -0,0 +1,180 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Socket.h"
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/sco.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/l2cap.h>
#include "nsThreadUtils.h"
#undef LOG
#if defined(MOZ_WIDGET_GONK)
#include <android/log.h>
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args)
#else
#define LOG(args...) printf(args);
#endif
#define TYPE_AS_STR(t) \
((t) == TYPE_RFCOMM ? "RFCOMM" : ((t) == TYPE_SCO ? "SCO" : "L2CAP"))
namespace mozilla {
namespace ipc {
static const int RFCOMM_SO_SNDBUF = 70 * 1024; // 70 KB send buffer
static const int TYPE_RFCOMM = 1;
static const int TYPE_SCO = 2;
static const int TYPE_L2CAP = 3;
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;
}
int
OpenSocket(int type, const char* aAddress, int channel, bool auth, bool encrypt)
{
MOZ_ASSERT(!NS_IsMainThread());
int lm = 0;
int fd = -1;
int sndbuf;
switch (type) {
case TYPE_RFCOMM:
fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
break;
case TYPE_SCO:
fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
break;
case TYPE_L2CAP:
fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
break;
default:
return -1;
}
if (fd < 0) {
NS_WARNING("Could not open bluetooth socket!");
return -1;
}
/* kernel does not yet support LM for SCO */
switch (type) {
case TYPE_RFCOMM:
lm |= auth ? RFCOMM_LM_AUTH : 0;
lm |= encrypt ? RFCOMM_LM_ENCRYPT : 0;
lm |= (auth && encrypt) ? RFCOMM_LM_SECURE : 0;
break;
case TYPE_L2CAP:
lm |= auth ? L2CAP_LM_AUTH : 0;
lm |= encrypt ? L2CAP_LM_ENCRYPT : 0;
lm |= (auth && encrypt) ? L2CAP_LM_SECURE : 0;
break;
}
if (lm) {
if (setsockopt(fd, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm))) {
LOG("setsockopt(RFCOMM_LM) failed, throwing");
return -1;
}
}
if (type == TYPE_RFCOMM) {
sndbuf = RFCOMM_SO_SNDBUF;
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
LOG("setsockopt(SO_SNDBUF) failed, throwing");
return -1;
}
}
int n = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
socklen_t addr_sz;
struct sockaddr *addr;
bdaddr_t bd_address_obj;
int mPort = channel;
char mAddress[18];
mAddress[17] = '\0';
strncpy(&mAddress[0], aAddress, 17);
if (get_bdaddr(aAddress, &bd_address_obj)) {
NS_WARNING("Can't get bluetooth address!");
return -1;
}
switch (type) {
case TYPE_RFCOMM:
struct sockaddr_rc addr_rc;
addr = (struct sockaddr *)&addr_rc;
addr_sz = sizeof(addr_rc);
memset(addr, 0, addr_sz);
addr_rc.rc_family = AF_BLUETOOTH;
addr_rc.rc_channel = mPort;
memcpy(&addr_rc.rc_bdaddr, &bd_address_obj, sizeof(bdaddr_t));
break;
case TYPE_SCO:
struct sockaddr_sco addr_sco;
addr = (struct sockaddr *)&addr_sco;
addr_sz = sizeof(addr_sco);
memset(addr, 0, addr_sz);
addr_sco.sco_family = AF_BLUETOOTH;
memcpy(&addr_sco.sco_bdaddr, &bd_address_obj, sizeof(bdaddr_t));
break;
default:
NS_WARNING("Socket type unknown!");
return -1;
}
int ret = connect(fd, addr, addr_sz);
if (ret) {
#if DEBUG
LOG("Socket connect errno=%d\n", errno);
#endif
NS_WARNING("Socket connect error!");
return -1;
}
// Match android_bluetooth_HeadsetBase.cpp line 384
// Skip many lines
return fd;
}
int
GetNewSocket(int type, const char* aAddress, int channel, bool auth, bool encrypt)
{
return OpenSocket(type, aAddress, channel, auth, encrypt);
}
int
CloseSocket(int aFd)
{
// This can block since we aren't opening sockets O_NONBLOCK
MOZ_ASSERT(!NS_IsMainThread());
return close(aFd);
}
} // namespace ipc
} // namespace mozilla

22
ipc/socket/Socket.h Normal file
Просмотреть файл

@ -0,0 +1,22 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_ipc_Socket_h
#define mozilla_ipc_Socket_h
namespace mozilla {
namespace ipc {
int
GetNewSocket(int type, const char* aAddress, int channel, bool auth, bool encrypt);
int
CloseSocket(int aFd);
} // namespace ipc
} // namepsace mozilla
#endif // mozilla_ipc_Socket_h

Просмотреть файл

@ -111,7 +111,7 @@ STATIC_LIBS += mozril_s
endif #}
ifdef MOZ_B2G_BT #{
STATIC_LIBS += mozdbus_s
STATIC_LIBS += mozdbus_s mozipcsocket_s
endif #}
ifeq (gonk,$(MOZ_WIDGET_TOOLKIT))