Bug 851046: Patch 2 - New Class: BluetoothSocket; r=mrbkap

--HG--
extra : rebase_source : 692fc69bc24a212621a5ff8a37fd17c237de9b20
This commit is contained in:
Eric Chou 2013-04-04 17:25:44 -07:00
parent 9a9b911119
commit 989000dc56
6 changed files with 181 additions and 1 deletions

View File

@ -53,6 +53,7 @@ extern bool gBluetoothDebugFlag;
// Bluetooth address format: xx:xx:xx:xx:xx:xx (or xx_xx_xx_xx_xx_xx)
#define BLUETOOTH_ADDRESS_LENGTH 17
#define BLUETOOTH_ADDRESS_NONE "00:00:00:00:00:00"
BEGIN_BLUETOOTH_NAMESPACE

View File

@ -0,0 +1,98 @@
/* -*- 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 "BluetoothSocket.h"
#include "BluetoothSocketObserver.h"
#include "BluetoothUnixSocketConnector.h"
#include "nsThreadUtils.h"
using namespace mozilla::ipc;
USING_BLUETOOTH_NAMESPACE
BluetoothSocket::BluetoothSocket(BluetoothSocketObserver* aObserver,
BluetoothSocketType aType,
bool aAuth,
bool aEncrypt)
: mObserver(aObserver)
, mType(aType)
, mAuth(aAuth)
, mEncrypt(aEncrypt)
{
MOZ_ASSERT(aObserver);
}
bool
BluetoothSocket::Connect(const nsACString& aDeviceAddress, int aChannel)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
nsAutoPtr<BluetoothUnixSocketConnector> c(
new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt));
if (!ConnectSocket(c.forget(), aDeviceAddress.BeginReading())) {
nsAutoString addr;
GetAddress(addr);
BT_LOG("%s failed. Current connected device address: %s",
__FUNCTION__, NS_ConvertUTF16toUTF8(addr).get());
return false;
}
return true;
}
bool
BluetoothSocket::Listen(int aChannel)
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoPtr<BluetoothUnixSocketConnector> c(
new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt));
if (!ListenSocket(c.forget())) {
nsAutoString addr;
GetAddress(addr);
BT_LOG("%s failed. Current connected device address: %s",
__FUNCTION__, NS_ConvertUTF16toUTF8(addr).get());
return false;
}
return true;
}
void
BluetoothSocket::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mObserver);
mObserver->ReceiveSocketData(aMessage);
}
void
BluetoothSocket::OnConnectSuccess()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mObserver);
mObserver->OnConnectSuccess();
}
void
BluetoothSocket::OnConnectError()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mObserver);
mObserver->OnConnectError();
}
void
BluetoothSocket::OnDisconnect()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mObserver);
mObserver->OnDisconnect();
}

View File

@ -0,0 +1,52 @@
/* -*- 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_dom_bluetooth_BluetoothSocket_h
#define mozilla_dom_bluetooth_BluetoothSocket_h
#include "BluetoothCommon.h"
#include "mozilla/ipc/UnixSocket.h"
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothSocketObserver;
class BluetoothSocket : public mozilla::ipc::UnixSocketConsumer
{
public:
BluetoothSocket(BluetoothSocketObserver* aObserver,
BluetoothSocketType aType,
bool aAuth,
bool aEncrypt);
bool Connect(const nsACString& aDeviceAddress, int aChannel);
bool Listen(int aChannel);
inline void Disconnect()
{
CloseSocket();
}
virtual void OnConnectSuccess() MOZ_OVERRIDE;
virtual void OnConnectError() MOZ_OVERRIDE;
virtual void OnDisconnect() MOZ_OVERRIDE;
virtual void ReceiveSocketData(
nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
inline void GetAddress(nsAString& aDeviceAddress)
{
GetSocketAddr(aDeviceAddress);
}
private:
BluetoothSocketObserver* mObserver;
BluetoothSocketType mType;
bool mAuth;
bool mEncrypt;
};
END_BLUETOOTH_NAMESPACE
#endif

View File

@ -0,0 +1,28 @@
/* -*- 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_dom_bluetooth_BluetoothSocketObserver_h
#define mozilla_dom_bluetooth_BluetoothSocketObserver_h
#include "BluetoothCommon.h"
#include <mozilla/ipc/UnixSocket.h>
using namespace mozilla::ipc;
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothSocketObserver
{
public:
virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) = 0;
virtual void OnConnectSuccess() = 0;
virtual void OnConnectError() = 0;
virtual void OnDisconnect() = 0;
};
END_BLUETOOTH_NAMESPACE
#endif

View File

@ -54,6 +54,7 @@ CPPSRCS += \
ObexBase.cpp \
BluetoothScoManager.cpp \
BluetoothUuid.cpp \
BluetoothSocket.cpp \
$(NULL)
ifdef MOZ_B2G_RIL

View File

@ -142,7 +142,7 @@ public:
virtual ~UnixSocketConsumer();
SocketConnectionStatus GetConnectionStatus()
SocketConnectionStatus GetConnectionStatus() const
{
return mConnectionStatus;
}