mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 1091575: Implement Socket module for Bluetooth daemon (under bluetooth2/), r=btian
This patch adds support for the Bluetooth daemon's Socket module, which provides OPP functionality.
This commit is contained in:
parent
0bd3ecc1f6
commit
27e500288e
@ -5,6 +5,7 @@
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "BluetoothDaemonHelpers.h"
|
||||
#include <limits>
|
||||
|
||||
#define MAX_UUID_SIZE 16
|
||||
|
||||
@ -44,6 +45,18 @@ Convert(bool aIn, BluetoothScanMode& aOut)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int16_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<int16_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<int16_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<int16_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, bool& aOut)
|
||||
{
|
||||
@ -136,6 +149,26 @@ Convert(uint8_t aIn, BluetoothPropertyType& aOut)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(BluetoothSocketType aIn, uint8_t& aOut)
|
||||
{
|
||||
static const uint8_t sSocketType[] = {
|
||||
CONVERT(0, 0), // silences compiler warning
|
||||
CONVERT(BluetoothSocketType::RFCOMM, 0x01),
|
||||
CONVERT(BluetoothSocketType::SCO, 0x02),
|
||||
CONVERT(BluetoothSocketType::L2CAP, 0x03)
|
||||
// EL2CAP not supported
|
||||
};
|
||||
if (NS_WARN_IF(aIn == BluetoothSocketType::EL2CAP) ||
|
||||
NS_WARN_IF(aIn >= MOZ_ARRAY_LENGTH(sSocketType)) ||
|
||||
NS_WARN_IF(!sSocketType[aIn])) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = sSocketType[aIn];
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int32_t aIn, BluetoothScanMode& aOut)
|
||||
{
|
||||
@ -267,6 +300,23 @@ Convert(const nsAString& aIn, BluetoothPropertyType& aOut)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(const nsAString& aIn, BluetoothServiceName& aOut)
|
||||
{
|
||||
NS_ConvertUTF16toUTF8 serviceNameUTF8(aIn);
|
||||
const char* str = serviceNameUTF8.get();
|
||||
size_t len = strlen(str);
|
||||
|
||||
if (NS_WARN_IF(len > sizeof(aOut.mName))) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
memcpy(aOut.mName, str, len);
|
||||
memset(aOut.mName + len, 0, sizeof(aOut.mName) - len);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(const nsAString& aIn, BluetoothSspVariant& aOut)
|
||||
{
|
||||
@ -519,6 +569,18 @@ PackPDU(BluetoothScanMode aIn, BluetoothDaemonPDU& aPDU)
|
||||
return PackPDU(PackConversion<BluetoothScanMode, int32_t>(aIn), aPDU);
|
||||
}
|
||||
|
||||
nsresult
|
||||
PackPDU(const BluetoothServiceName& aIn, BluetoothDaemonPDU& aPDU)
|
||||
{
|
||||
return PackPDU(PackArray<uint8_t>(aIn.mName, sizeof(aIn.mName)), aPDU);
|
||||
}
|
||||
|
||||
nsresult
|
||||
PackPDU(BluetoothSocketType aIn, BluetoothDaemonPDU& aPDU)
|
||||
{
|
||||
return PackPDU(PackConversion<BluetoothSocketType, uint8_t>(aIn), aPDU);
|
||||
}
|
||||
|
||||
//
|
||||
// Unpacking
|
||||
//
|
||||
|
@ -63,6 +63,10 @@ struct BluetoothRemoteName {
|
||||
uint8_t mName[249];
|
||||
};
|
||||
|
||||
struct BluetoothServiceName {
|
||||
uint8_t mName[256];
|
||||
};
|
||||
|
||||
//
|
||||
// Conversion
|
||||
//
|
||||
@ -79,6 +83,9 @@ Convert(bool aIn, uint8_t& aOut);
|
||||
nsresult
|
||||
Convert(bool aIn, BluetoothScanMode& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int16_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, bool& aOut);
|
||||
|
||||
@ -118,6 +125,9 @@ Convert(const nsAString& aIn, BluetoothPinCode& aOut);
|
||||
nsresult
|
||||
Convert(const nsAString& aIn, BluetoothPropertyType& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(const nsAString& aIn, BluetoothServiceName& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(const nsAString& aIn, BluetoothSspVariant& aOut);
|
||||
|
||||
@ -136,6 +146,9 @@ Convert(const BluetoothRemoteName& aIn, nsAString& aOut);
|
||||
nsresult
|
||||
Convert(BluetoothScanMode aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(BluetoothSocketType aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(BluetoothSspVariant aIn, uint8_t& aOut);
|
||||
|
||||
@ -188,6 +201,12 @@ PackPDU(const BluetoothPinCode& aIn, BluetoothDaemonPDU& aPDU);
|
||||
nsresult
|
||||
PackPDU(BluetoothPropertyType aIn, BluetoothDaemonPDU& aPDU);
|
||||
|
||||
nsresult
|
||||
PackPDU(const BluetoothServiceName& aIn, BluetoothDaemonPDU& aPDU);
|
||||
|
||||
nsresult
|
||||
PackPDU(BluetoothSocketType aIn, BluetoothDaemonPDU& aPDU);
|
||||
|
||||
nsresult
|
||||
PackPDU(BluetoothSspVariant aIn, BluetoothDaemonPDU& aPDU);
|
||||
|
||||
@ -307,6 +326,32 @@ PackPDU(const T1& aIn1, const T2& aIn2, const T3& aIn3, const T4& aIn4,
|
||||
return PackPDU(aIn4, aPDU);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2, typename T3,
|
||||
typename T4, typename T5>
|
||||
inline nsresult
|
||||
PackPDU(const T1& aIn1, const T2& aIn2, const T3& aIn3,
|
||||
const T4& aIn4, const T5& aIn5,
|
||||
BluetoothDaemonPDU& aPDU)
|
||||
{
|
||||
nsresult rv = PackPDU(aIn1, aPDU);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = PackPDU(aIn2, aPDU);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = PackPDU(aIn3, aPDU);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = PackPDU(aIn4, aPDU);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
return PackPDU(aIn5, aPDU);
|
||||
}
|
||||
|
||||
//
|
||||
// Unpacking
|
||||
//
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "BluetoothDaemonInterface.h"
|
||||
#include "BluetoothDaemonHelpers.h"
|
||||
#include "BluetoothDaemonSetupInterface.h"
|
||||
#include "BluetoothDaemonSocketInterface.h"
|
||||
#include "BluetoothInterfaceHelpers.h"
|
||||
#include "mozilla/unused.h"
|
||||
|
||||
@ -1454,6 +1455,7 @@ class BluetoothDaemonProtocol MOZ_FINAL
|
||||
: public BluetoothDaemonPDUConsumer
|
||||
, public BluetoothDaemonSetupModule
|
||||
, public BluetoothDaemonCoreModule
|
||||
, public BluetoothDaemonSocketModule
|
||||
{
|
||||
public:
|
||||
BluetoothDaemonProtocol(BluetoothDaemonConnection* aConnection);
|
||||
@ -1477,6 +1479,8 @@ private:
|
||||
BluetoothDaemonPDU& aPDU, void* aUserData);
|
||||
void HandleCoreSvc(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU, void* aUserData);
|
||||
void HandleSocketSvc(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU, void* aUserData);
|
||||
|
||||
BluetoothDaemonConnection* mConnection;
|
||||
nsTArray<void*> mUserDataQ;
|
||||
@ -1515,6 +1519,14 @@ BluetoothDaemonProtocol::HandleCoreSvc(
|
||||
BluetoothDaemonCoreModule::HandleSvc(aHeader, aPDU, aUserData);
|
||||
}
|
||||
|
||||
void
|
||||
BluetoothDaemonProtocol::HandleSocketSvc(
|
||||
const BluetoothDaemonPDUHeader& aHeader, BluetoothDaemonPDU& aPDU,
|
||||
void* aUserData)
|
||||
{
|
||||
BluetoothDaemonSocketModule::HandleSvc(aHeader, aPDU, aUserData);
|
||||
}
|
||||
|
||||
void
|
||||
BluetoothDaemonProtocol::Handle(BluetoothDaemonPDU& aPDU)
|
||||
{
|
||||
@ -1523,7 +1535,9 @@ BluetoothDaemonProtocol::Handle(BluetoothDaemonPDU& aPDU)
|
||||
INIT_ARRAY_AT(BluetoothDaemonSetupModule::SERVICE_ID,
|
||||
&BluetoothDaemonProtocol::HandleSetupSvc),
|
||||
INIT_ARRAY_AT(BluetoothDaemonCoreModule::SERVICE_ID,
|
||||
&BluetoothDaemonProtocol::HandleCoreSvc)
|
||||
&BluetoothDaemonProtocol::HandleCoreSvc),
|
||||
INIT_ARRAY_AT(BluetoothDaemonSocketModule::SERVICE_ID,
|
||||
&BluetoothDaemonProtocol::HandleSocketSvc)
|
||||
};
|
||||
|
||||
BluetoothDaemonPDUHeader header;
|
||||
@ -1719,7 +1733,8 @@ public:
|
||||
if (!mRegisteredSocketModule) {
|
||||
mRegisteredSocketModule = true;
|
||||
// Init, step 4: Register Socket module
|
||||
mInterface->mProtocol->RegisterModuleCmd(0x02, 0x00, this);
|
||||
mInterface->mProtocol->RegisterModuleCmd(
|
||||
BluetoothDaemonSocketModule::SERVICE_ID, 0x00, this);
|
||||
} else if (mRes) {
|
||||
// Init, step 5: Signal success to caller
|
||||
mRes->Init();
|
||||
@ -1887,7 +1902,8 @@ BluetoothDaemonInterface::Cleanup(BluetoothResultHandler* aRes)
|
||||
mResultHandlerQ.AppendElement(aRes);
|
||||
|
||||
// Cleanup, step 1: Unregister Socket module
|
||||
mProtocol->UnregisterModuleCmd(0x02, new CleanupResultHandler(this));
|
||||
mProtocol->UnregisterModuleCmd(
|
||||
BluetoothDaemonSocketModule::SERVICE_ID, new CleanupResultHandler(this));
|
||||
}
|
||||
|
||||
void
|
||||
@ -2076,10 +2092,19 @@ BluetoothDaemonInterface::DispatchError(BluetoothResultHandler* aRes,
|
||||
ConstantInitOp1<BluetoothStatus>(aStatus));
|
||||
}
|
||||
|
||||
// Profile Interfaces
|
||||
//
|
||||
|
||||
BluetoothSocketInterface*
|
||||
BluetoothDaemonInterface::GetBluetoothSocketInterface()
|
||||
{
|
||||
return nullptr;
|
||||
if (mSocketInterface) {
|
||||
return mSocketInterface;
|
||||
}
|
||||
|
||||
mSocketInterface = new BluetoothDaemonSocketInterface(mProtocol);
|
||||
|
||||
return mSocketInterface;
|
||||
}
|
||||
|
||||
BluetoothHandsfreeInterface*
|
||||
|
@ -13,6 +13,7 @@ BEGIN_BLUETOOTH_NAMESPACE
|
||||
|
||||
class BluetoothDaemonChannel;
|
||||
class BluetoothDaemonProtocol;
|
||||
class BluetoothDaemonSocketInterface;
|
||||
|
||||
class BluetoothDaemonInterface MOZ_FINAL : public BluetoothInterface
|
||||
{
|
||||
@ -123,6 +124,8 @@ private:
|
||||
nsAutoPtr<BluetoothDaemonProtocol> mProtocol;
|
||||
|
||||
nsTArray<nsRefPtr<BluetoothResultHandler> > mResultHandlerQ;
|
||||
|
||||
nsAutoPtr<BluetoothDaemonSocketInterface> mSocketInterface;
|
||||
};
|
||||
|
||||
END_BLUETOOTH_NAMESPACE
|
||||
|
352
dom/bluetooth2/bluedroid/BluetoothDaemonSocketInterface.cpp
Normal file
352
dom/bluetooth2/bluedroid/BluetoothDaemonSocketInterface.cpp
Normal file
@ -0,0 +1,352 @@
|
||||
/* -*- 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 "BluetoothDaemonSocketInterface.h"
|
||||
#include "BluetoothSocketMessageWatcher.h"
|
||||
#include "nsXULAppAPI.h"
|
||||
#include "mozilla/unused.h"
|
||||
|
||||
BEGIN_BLUETOOTH_NAMESPACE
|
||||
|
||||
//
|
||||
// Socket module
|
||||
//
|
||||
|
||||
// Commands
|
||||
//
|
||||
|
||||
nsresult
|
||||
BluetoothDaemonSocketModule::ListenCmd(BluetoothSocketType aType,
|
||||
const nsAString& aServiceName,
|
||||
const uint8_t aServiceUuid[16],
|
||||
int aChannel, bool aEncrypt,
|
||||
bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
nsAutoPtr<BluetoothDaemonPDU> pdu(new BluetoothDaemonPDU(
|
||||
SERVICE_ID, OPCODE_LISTEN, 0));
|
||||
|
||||
nsresult rv = PackPDU(
|
||||
aType,
|
||||
PackConversion<nsAString, BluetoothServiceName>(aServiceName),
|
||||
PackArray<uint8_t>(aServiceUuid, 16),
|
||||
PackConversion<int, uint16_t>(aChannel),
|
||||
SocketFlags(aEncrypt, aAuth), *pdu);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = Send(pdu, aRes);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
unused << pdu.forget();
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
BluetoothDaemonSocketModule::ConnectCmd(const nsAString& aBdAddr,
|
||||
BluetoothSocketType aType,
|
||||
const uint8_t aUuid[16],
|
||||
int aChannel, bool aEncrypt,
|
||||
bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
nsAutoPtr<BluetoothDaemonPDU> pdu(new BluetoothDaemonPDU(
|
||||
SERVICE_ID, OPCODE_CONNECT, 0));
|
||||
|
||||
nsresult rv = PackPDU(
|
||||
PackConversion<nsAString, BluetoothAddress>(aBdAddr),
|
||||
aType,
|
||||
PackArray<uint8_t>(aUuid, 16),
|
||||
PackConversion<int, int16_t>(aChannel),
|
||||
SocketFlags(aEncrypt, aAuth), *pdu);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = Send(pdu, aRes);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
unused << pdu.forget();
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* |DeleteTask| deletes a class instance on the I/O thread
|
||||
*/
|
||||
template <typename T>
|
||||
class DeleteTask MOZ_FINAL : public Task
|
||||
{
|
||||
public:
|
||||
DeleteTask(T* aPtr)
|
||||
: mPtr(aPtr)
|
||||
{ }
|
||||
|
||||
void Run() MOZ_OVERRIDE
|
||||
{
|
||||
mPtr = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
nsAutoPtr<T> mPtr;
|
||||
};
|
||||
|
||||
/* |AcceptWatcher| specializes SocketMessageWatcher for Accept
|
||||
* operations by reading the socket messages from Bluedroid and
|
||||
* forwarding the received client socket to the resource handler.
|
||||
* The first message is received immediately. When there's a new
|
||||
* connection, Bluedroid sends the 2nd message with the socket
|
||||
* info and socket file descriptor.
|
||||
*/
|
||||
class BluetoothDaemonSocketModule::AcceptWatcher MOZ_FINAL
|
||||
: public SocketMessageWatcher
|
||||
{
|
||||
public:
|
||||
AcceptWatcher(int aFd, BluetoothSocketResultHandler* aRes)
|
||||
: SocketMessageWatcher(aFd, aRes)
|
||||
{ }
|
||||
|
||||
void Proceed(BluetoothStatus aStatus) MOZ_OVERRIDE
|
||||
{
|
||||
if (aStatus == STATUS_SUCCESS) {
|
||||
IntStringIntResultRunnable::Dispatch(
|
||||
GetResultHandler(), &BluetoothSocketResultHandler::Accept,
|
||||
ConstantInitOp3<int, nsString, int>(GetClientFd(), GetBdAddress(),
|
||||
GetConnectionStatus()));
|
||||
} else {
|
||||
ErrorRunnable::Dispatch(GetResultHandler(),
|
||||
&BluetoothSocketResultHandler::OnError,
|
||||
ConstantInitOp1<BluetoothStatus>(aStatus));
|
||||
}
|
||||
|
||||
MessageLoopForIO::current()->PostTask(
|
||||
FROM_HERE, new DeleteTask<AcceptWatcher>(this));
|
||||
}
|
||||
};
|
||||
|
||||
nsresult
|
||||
BluetoothDaemonSocketModule::AcceptCmd(int aFd,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
/* receive Bluedroid's socket-setup messages and client fd */
|
||||
Task* t = new SocketMessageWatcherTask(new AcceptWatcher(aFd, aRes));
|
||||
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, t);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
BluetoothDaemonSocketModule::CloseCmd(BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
/* stop the watcher corresponding to |aRes| */
|
||||
Task* t = new DeleteSocketMessageWatcherTask(aRes);
|
||||
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, t);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketModule::HandleSvc(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU,
|
||||
void* aUserData)
|
||||
{
|
||||
static void (BluetoothDaemonSocketModule::* const HandleRsp[])(
|
||||
const BluetoothDaemonPDUHeader&,
|
||||
BluetoothDaemonPDU&,
|
||||
BluetoothSocketResultHandler*) = {
|
||||
INIT_ARRAY_AT(OPCODE_ERROR, &BluetoothDaemonSocketModule::ErrorRsp),
|
||||
INIT_ARRAY_AT(OPCODE_LISTEN, &BluetoothDaemonSocketModule::ListenRsp),
|
||||
INIT_ARRAY_AT(OPCODE_CONNECT, &BluetoothDaemonSocketModule::ConnectRsp),
|
||||
};
|
||||
|
||||
if (NS_WARN_IF(MOZ_ARRAY_LENGTH(HandleRsp) <= aHeader.mOpcode) ||
|
||||
NS_WARN_IF(!HandleRsp[aHeader.mOpcode])) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsRefPtr<BluetoothSocketResultHandler> res =
|
||||
already_AddRefed<BluetoothSocketResultHandler>(
|
||||
static_cast<BluetoothSocketResultHandler*>(aUserData));
|
||||
|
||||
if (!res) {
|
||||
return; // Return early if no result handler has been set
|
||||
}
|
||||
|
||||
(this->*(HandleRsp[aHeader.mOpcode]))(aHeader, aPDU, res);
|
||||
}
|
||||
|
||||
nsresult
|
||||
BluetoothDaemonSocketModule::Send(BluetoothDaemonPDU* aPDU,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
aRes->AddRef(); // Keep reference for response
|
||||
return Send(aPDU, static_cast<void*>(aRes));
|
||||
}
|
||||
|
||||
uint8_t
|
||||
BluetoothDaemonSocketModule::SocketFlags(bool aEncrypt, bool aAuth)
|
||||
{
|
||||
return (0x01 * aEncrypt) | (0x02 * aAuth);
|
||||
}
|
||||
|
||||
// Responses
|
||||
//
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketModule::ErrorRsp(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
ErrorRunnable::Dispatch(
|
||||
aRes, &BluetoothSocketResultHandler::OnError, UnpackPDUInitOp(aPDU));
|
||||
}
|
||||
|
||||
class BluetoothDaemonSocketModule::ListenInitOp MOZ_FINAL : private PDUInitOp
|
||||
{
|
||||
public:
|
||||
ListenInitOp(BluetoothDaemonPDU& aPDU)
|
||||
: PDUInitOp(aPDU)
|
||||
{ }
|
||||
|
||||
nsresult
|
||||
operator () (int& aArg1) const
|
||||
{
|
||||
BluetoothDaemonPDU& pdu = GetPDU();
|
||||
|
||||
aArg1 = pdu.AcquireFd();
|
||||
|
||||
if (NS_WARN_IF(aArg1 < 0)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
WarnAboutTrailingData();
|
||||
return NS_OK;
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketModule::ListenRsp(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
IntResultRunnable::Dispatch(
|
||||
aRes, &BluetoothSocketResultHandler::Listen, ListenInitOp(aPDU));
|
||||
}
|
||||
|
||||
/* |ConnectWatcher| specializes SocketMessageWatcher for
|
||||
* connect operations by reading the socket messages from
|
||||
* Bluedroid and forwarding the connected socket to the
|
||||
* resource handler.
|
||||
*/
|
||||
class BluetoothDaemonSocketModule::ConnectWatcher MOZ_FINAL
|
||||
: public SocketMessageWatcher
|
||||
{
|
||||
public:
|
||||
ConnectWatcher(int aFd, BluetoothSocketResultHandler* aRes)
|
||||
: SocketMessageWatcher(aFd, aRes)
|
||||
{ }
|
||||
|
||||
void Proceed(BluetoothStatus aStatus) MOZ_OVERRIDE
|
||||
{
|
||||
if (aStatus == STATUS_SUCCESS) {
|
||||
IntStringIntResultRunnable::Dispatch(
|
||||
GetResultHandler(), &BluetoothSocketResultHandler::Connect,
|
||||
ConstantInitOp3<int, nsString, int>(GetFd(), GetBdAddress(),
|
||||
GetConnectionStatus()));
|
||||
} else {
|
||||
ErrorRunnable::Dispatch(GetResultHandler(),
|
||||
&BluetoothSocketResultHandler::OnError,
|
||||
ConstantInitOp1<BluetoothStatus>(aStatus));
|
||||
}
|
||||
|
||||
MessageLoopForIO::current()->PostTask(
|
||||
FROM_HERE, new DeleteTask<ConnectWatcher>(this));
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketModule::ConnectRsp(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
/* the file descriptor is attached in the PDU's ancillary data */
|
||||
int fd = aPDU.AcquireFd();
|
||||
if (fd < 0) {
|
||||
ErrorRunnable::Dispatch(aRes, &BluetoothSocketResultHandler::OnError,
|
||||
ConstantInitOp1<BluetoothStatus>(STATUS_FAIL));
|
||||
return;
|
||||
}
|
||||
|
||||
/* receive Bluedroid's socket-setup messages */
|
||||
Task* t = new SocketMessageWatcherTask(new ConnectWatcher(fd, aRes));
|
||||
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, t);
|
||||
}
|
||||
|
||||
//
|
||||
// Socket interface
|
||||
//
|
||||
|
||||
BluetoothDaemonSocketInterface::BluetoothDaemonSocketInterface(
|
||||
BluetoothDaemonSocketModule* aModule)
|
||||
: mModule(aModule)
|
||||
{
|
||||
MOZ_ASSERT(mModule);
|
||||
}
|
||||
|
||||
BluetoothDaemonSocketInterface::~BluetoothDaemonSocketInterface()
|
||||
{ }
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketInterface::Listen(BluetoothSocketType aType,
|
||||
const nsAString& aServiceName,
|
||||
const uint8_t aServiceUuid[16],
|
||||
int aChannel, bool aEncrypt,
|
||||
bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(mModule);
|
||||
|
||||
mModule->ListenCmd(aType, aServiceName, aServiceUuid, aChannel,
|
||||
aEncrypt, aAuth, aRes);
|
||||
}
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketInterface::Connect(const nsAString& aBdAddr,
|
||||
BluetoothSocketType aType,
|
||||
const uint8_t aUuid[16],
|
||||
int aChannel, bool aEncrypt,
|
||||
bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(mModule);
|
||||
|
||||
mModule->ConnectCmd(aBdAddr, aType, aUuid, aChannel, aEncrypt, aAuth, aRes);
|
||||
}
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketInterface::Accept(int aFd,
|
||||
BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(mModule);
|
||||
|
||||
mModule->AcceptCmd(aFd, aRes);
|
||||
}
|
||||
|
||||
void
|
||||
BluetoothDaemonSocketInterface::Close(BluetoothSocketResultHandler* aRes)
|
||||
{
|
||||
MOZ_ASSERT(mModule);
|
||||
|
||||
mModule->CloseCmd(aRes);
|
||||
}
|
||||
|
||||
END_BLUETOOTH_NAMESPACE
|
129
dom/bluetooth2/bluedroid/BluetoothDaemonSocketInterface.h
Normal file
129
dom/bluetooth2/bluedroid/BluetoothDaemonSocketInterface.h
Normal file
@ -0,0 +1,129 @@
|
||||
/* -*- 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_bluedroid_bluetoothdaemonsocketinterface_h__
|
||||
#define mozilla_dom_bluetooth_bluedroid_bluetoothdaemonsocketinterface_h__
|
||||
|
||||
#include "BluetoothDaemonHelpers.h"
|
||||
#include "BluetoothInterface.h"
|
||||
#include "BluetoothInterfaceHelpers.h"
|
||||
|
||||
BEGIN_BLUETOOTH_NAMESPACE
|
||||
|
||||
using namespace mozilla::ipc;
|
||||
|
||||
class BlutoothDaemonInterface;
|
||||
|
||||
class BluetoothDaemonSocketModule
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
SERVICE_ID = 0x02
|
||||
};
|
||||
|
||||
enum {
|
||||
OPCODE_ERROR = 0x00,
|
||||
OPCODE_LISTEN = 0x01,
|
||||
OPCODE_CONNECT = 0x02
|
||||
};
|
||||
|
||||
virtual nsresult Send(BluetoothDaemonPDU* aPDU, void* aUserData) = 0;
|
||||
|
||||
// Commands
|
||||
//
|
||||
|
||||
nsresult ListenCmd(BluetoothSocketType aType,
|
||||
const nsAString& aServiceName,
|
||||
const uint8_t aServiceUuid[16],
|
||||
int aChannel, bool aEncrypt, bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes);
|
||||
|
||||
nsresult ConnectCmd(const nsAString& aBdAddr,
|
||||
BluetoothSocketType aType,
|
||||
const uint8_t aUuid[16],
|
||||
int aChannel, bool aEncrypt, bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes);
|
||||
|
||||
nsresult AcceptCmd(int aFd, BluetoothSocketResultHandler* aRes);
|
||||
|
||||
nsresult CloseCmd(BluetoothSocketResultHandler* aRes);
|
||||
|
||||
protected:
|
||||
|
||||
void HandleSvc(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU, void* aUserData);
|
||||
|
||||
nsresult Send(BluetoothDaemonPDU* aPDU, BluetoothSocketResultHandler* aRes);
|
||||
|
||||
private:
|
||||
class AcceptWatcher;
|
||||
class ConnectWatcher;
|
||||
class ListenInitOp;
|
||||
|
||||
uint8_t SocketFlags(bool aEncrypt, bool aAuth);
|
||||
|
||||
// Responses
|
||||
//
|
||||
|
||||
typedef BluetoothResultRunnable0<BluetoothSocketResultHandler, void>
|
||||
ResultRunnable;
|
||||
|
||||
typedef BluetoothResultRunnable1<BluetoothSocketResultHandler, void,
|
||||
int, int>
|
||||
IntResultRunnable;
|
||||
|
||||
typedef BluetoothResultRunnable1<BluetoothSocketResultHandler, void,
|
||||
BluetoothStatus, BluetoothStatus>
|
||||
ErrorRunnable;
|
||||
|
||||
typedef BluetoothResultRunnable3<BluetoothSocketResultHandler, void,
|
||||
int, nsString, int,
|
||||
int, const nsAString_internal&, int>
|
||||
IntStringIntResultRunnable;
|
||||
|
||||
void ErrorRsp(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU,
|
||||
BluetoothSocketResultHandler* aRes);
|
||||
|
||||
void ListenRsp(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU,
|
||||
BluetoothSocketResultHandler* aRes);
|
||||
|
||||
void ConnectRsp(const BluetoothDaemonPDUHeader& aHeader,
|
||||
BluetoothDaemonPDU& aPDU,
|
||||
BluetoothSocketResultHandler* aRes);
|
||||
};
|
||||
|
||||
class BluetoothDaemonSocketInterface MOZ_FINAL
|
||||
: public BluetoothSocketInterface
|
||||
{
|
||||
public:
|
||||
BluetoothDaemonSocketInterface(BluetoothDaemonSocketModule* aModule);
|
||||
~BluetoothDaemonSocketInterface();
|
||||
|
||||
void Listen(BluetoothSocketType aType,
|
||||
const nsAString& aServiceName,
|
||||
const uint8_t aServiceUuid[16],
|
||||
int aChannel, bool aEncrypt, bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes);
|
||||
|
||||
void Connect(const nsAString& aBdAddr,
|
||||
BluetoothSocketType aType,
|
||||
const uint8_t aUuid[16],
|
||||
int aChannel, bool aEncrypt, bool aAuth,
|
||||
BluetoothSocketResultHandler* aRes);
|
||||
|
||||
void Accept(int aFd, BluetoothSocketResultHandler* aRes);
|
||||
|
||||
void Close(BluetoothSocketResultHandler* aRes);
|
||||
|
||||
private:
|
||||
BluetoothDaemonSocketModule* mModule;
|
||||
};
|
||||
|
||||
END_BLUETOOTH_NAMESPACE
|
||||
|
||||
#endif
|
@ -54,6 +54,7 @@ if CONFIG['MOZ_B2G_BT']:
|
||||
'bluedroid/BluetoothDaemonHelpers.cpp',
|
||||
'bluedroid/BluetoothDaemonInterface.cpp',
|
||||
'bluedroid/BluetoothDaemonSetupInterface.cpp',
|
||||
'bluedroid/BluetoothDaemonSocketInterface.cpp',
|
||||
'bluedroid/BluetoothGattHALInterface.cpp',
|
||||
'bluedroid/BluetoothGattManager.cpp',
|
||||
'bluedroid/BluetoothHALHelpers.cpp',
|
||||
|
Loading…
Reference in New Issue
Block a user