Bug 1204801: Move |BluetoothDaemonSetupModule| into separate compilation unit, r=btian

This commit is contained in:
Thomas Zimmermann 2015-09-16 10:37:58 +02:00
parent 4e5e58b00d
commit 9599e8d246
3 changed files with 214 additions and 163 deletions

View File

@ -27,167 +27,6 @@ using namespace mozilla::ipc;
static const int sRetryInterval = 100; // ms
//
// Protocol initialization and setup
//
class BluetoothDaemonSetupModule
{
public:
virtual nsresult Send(DaemonSocketPDU* aPDU,
DaemonSocketResultHandler* aRes) = 0;
// Commands
//
nsresult RegisterModuleCmd(uint8_t aId, uint8_t aMode,
uint32_t aMaxNumClients,
BluetoothSetupResultHandler* aRes)
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoPtr<DaemonSocketPDU> pdu(new DaemonSocketPDU(0x00, 0x01, 0));
#if ANDROID_VERSION >= 21
nsresult rv = PackPDU(aId, aMode, aMaxNumClients, *pdu);
#else
nsresult rv = PackPDU(aId, aMode, *pdu);
#endif
if (NS_FAILED(rv)) {
return rv;
}
rv = Send(pdu, aRes);
if (NS_FAILED(rv)) {
return rv;
}
unused << pdu.forget();
return rv;
}
nsresult UnregisterModuleCmd(uint8_t aId,
BluetoothSetupResultHandler* aRes)
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoPtr<DaemonSocketPDU> pdu(new DaemonSocketPDU(0x00, 0x02, 0));
nsresult rv = PackPDU(aId, *pdu);
if (NS_FAILED(rv)) {
return rv;
}
rv = Send(pdu, aRes);
if (NS_FAILED(rv)) {
return rv;
}
unused << pdu.forget();
return rv;
}
nsresult ConfigurationCmd(const BluetoothConfigurationParameter* aParam,
uint8_t aLen, BluetoothSetupResultHandler* aRes)
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoPtr<DaemonSocketPDU> pdu(new DaemonSocketPDU(0x00, 0x03, 0));
nsresult rv = PackPDU(
aLen, PackArray<BluetoothConfigurationParameter>(aParam, aLen), *pdu);
if (NS_FAILED(rv)) {
return rv;
}
rv = Send(pdu, aRes);
if (NS_FAILED(rv)) {
return rv;
}
unused << pdu.forget();
return rv;
}
protected:
// Called to handle PDUs with Service field equal to 0x00, which
// contains internal operations for setup and configuration.
void HandleSvc(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes)
{
static void (BluetoothDaemonSetupModule::* const HandleRsp[])(
const DaemonSocketPDUHeader&,
DaemonSocketPDU&,
BluetoothSetupResultHandler*) = {
[0x00] = &BluetoothDaemonSetupModule::ErrorRsp,
[0x01] = &BluetoothDaemonSetupModule::RegisterModuleRsp,
[0x02] = &BluetoothDaemonSetupModule::UnregisterModuleRsp,
[0x03] = &BluetoothDaemonSetupModule::ConfigurationRsp
};
if (NS_WARN_IF(aHeader.mOpcode >= MOZ_ARRAY_LENGTH(HandleRsp)) ||
NS_WARN_IF(!HandleRsp[aHeader.mOpcode])) {
return;
}
nsRefPtr<BluetoothSetupResultHandler> res =
static_cast<BluetoothSetupResultHandler*>(aRes);
if (!aRes) {
return; // Return early if no result handler has been set
}
(this->*(HandleRsp[aHeader.mOpcode]))(aHeader, aPDU, res);
}
private:
// Responses
//
typedef mozilla::ipc::DaemonResultRunnable0<
BluetoothSetupResultHandler, void>
ResultRunnable;
typedef mozilla::ipc::DaemonResultRunnable1<
BluetoothSetupResultHandler, void, BluetoothStatus, BluetoothStatus>
ErrorRunnable;
void
ErrorRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ErrorRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::OnError, UnpackPDUInitOp(aPDU));
}
void
RegisterModuleRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ResultRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::RegisterModule,
UnpackPDUInitOp(aPDU));
}
void
UnregisterModuleRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ResultRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::UnregisterModule,
UnpackPDUInitOp(aPDU));
}
void
ConfigurationRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ResultRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::Configuration,
UnpackPDUInitOp(aPDU));
}
};
//
// Core module
//

View File

@ -5,6 +5,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "BluetoothDaemonSetupInterface.h"
#include "mozilla/unused.h"
BEGIN_BLUETOOTH_NAMESPACE
@ -29,4 +30,152 @@ void
BluetoothSetupResultHandler::Configuration()
{ }
//
// Setup module
//
// Called to handle PDUs with Service field equal to 0x00, which
// contains internal operations for setup and configuration.
void
BluetoothDaemonSetupModule::HandleSvc(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
DaemonSocketResultHandler* aRes)
{
static void (BluetoothDaemonSetupModule::* const HandleRsp[])(
const DaemonSocketPDUHeader&,
DaemonSocketPDU&,
BluetoothSetupResultHandler*) = {
[0x00] = &BluetoothDaemonSetupModule::ErrorRsp,
[0x01] = &BluetoothDaemonSetupModule::RegisterModuleRsp,
[0x02] = &BluetoothDaemonSetupModule::UnregisterModuleRsp,
[0x03] = &BluetoothDaemonSetupModule::ConfigurationRsp
};
if (NS_WARN_IF(aHeader.mOpcode >= MOZ_ARRAY_LENGTH(HandleRsp)) ||
NS_WARN_IF(!HandleRsp[aHeader.mOpcode])) {
return;
}
nsRefPtr<BluetoothSetupResultHandler> res =
static_cast<BluetoothSetupResultHandler*>(aRes);
if (!aRes) {
return; // Return early if no result handler has been set
}
(this->*(HandleRsp[aHeader.mOpcode]))(aHeader, aPDU, res);
}
// Commands
//
nsresult
BluetoothDaemonSetupModule::RegisterModuleCmd(
uint8_t aId, uint8_t aMode, uint32_t aMaxNumClients,
BluetoothSetupResultHandler* aRes)
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoPtr<DaemonSocketPDU> pdu(new DaemonSocketPDU(0x00, 0x01, 0));
#if ANDROID_VERSION >= 21
nsresult rv = PackPDU(aId, aMode, aMaxNumClients, *pdu);
#else
nsresult rv = PackPDU(aId, aMode, *pdu);
#endif
if (NS_FAILED(rv)) {
return rv;
}
rv = Send(pdu, aRes);
if (NS_FAILED(rv)) {
return rv;
}
unused << pdu.forget();
return rv;
}
nsresult
BluetoothDaemonSetupModule::UnregisterModuleCmd(
uint8_t aId, BluetoothSetupResultHandler* aRes)
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoPtr<DaemonSocketPDU> pdu(new DaemonSocketPDU(0x00, 0x02, 0));
nsresult rv = PackPDU(aId, *pdu);
if (NS_FAILED(rv)) {
return rv;
}
rv = Send(pdu, aRes);
if (NS_FAILED(rv)) {
return rv;
}
unused << pdu.forget();
return rv;
}
nsresult
BluetoothDaemonSetupModule::ConfigurationCmd(
const BluetoothConfigurationParameter* aParam, uint8_t aLen,
BluetoothSetupResultHandler* aRes)
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoPtr<DaemonSocketPDU> pdu(new DaemonSocketPDU(0x00, 0x03, 0));
nsresult rv = PackPDU(
aLen, PackArray<BluetoothConfigurationParameter>(aParam, aLen), *pdu);
if (NS_FAILED(rv)) {
return rv;
}
rv = Send(pdu, aRes);
if (NS_FAILED(rv)) {
return rv;
}
unused << pdu.forget();
return rv;
}
// Responses
//
void
BluetoothDaemonSetupModule::ErrorRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ErrorRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::OnError, UnpackPDUInitOp(aPDU));
}
void
BluetoothDaemonSetupModule::RegisterModuleRsp(
const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ResultRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::RegisterModule,
UnpackPDUInitOp(aPDU));
}
void
BluetoothDaemonSetupModule::UnregisterModuleRsp(
const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ResultRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::UnregisterModule,
UnpackPDUInitOp(aPDU));
}
void
BluetoothDaemonSetupModule::ConfigurationRsp(
const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes)
{
ResultRunnable::Dispatch(
aRes, &BluetoothSetupResultHandler::Configuration,
UnpackPDUInitOp(aPDU));
}
END_BLUETOOTH_NAMESPACE

View File

@ -7,11 +7,16 @@
#ifndef mozilla_dom_bluetooth_bluedroid_BluetoothDaemonSetupInterface_h
#define mozilla_dom_bluetooth_bluedroid_BluetoothDaemonSetupInterface_h
#include "BluetoothCommon.h"
#include "mozilla/ipc/DaemonSocketMessageHandlers.h"
#include "BluetoothDaemonHelpers.h"
#include "BluetoothInterface.h"
#include "mozilla/ipc/DaemonRunnables.h"
BEGIN_BLUETOOTH_NAMESPACE
using mozilla::ipc::DaemonSocketPDU;
using mozilla::ipc::DaemonSocketPDUHeader;
using mozilla::ipc::DaemonSocketResultHandler;
class BluetoothSetupResultHandler
: public mozilla::ipc::DaemonSocketResultHandler
{
@ -25,6 +30,64 @@ protected:
virtual ~BluetoothSetupResultHandler();
};
class BluetoothDaemonSetupModule
{
public:
virtual nsresult Send(DaemonSocketPDU* aPDU,
DaemonSocketResultHandler* aRes) = 0;
// Commands
//
nsresult RegisterModuleCmd(uint8_t aId, uint8_t aMode,
uint32_t aMaxNumClients,
BluetoothSetupResultHandler* aRes);
nsresult UnregisterModuleCmd(uint8_t aId,
BluetoothSetupResultHandler* aRes);
nsresult ConfigurationCmd(const BluetoothConfigurationParameter* aParam,
uint8_t aLen, BluetoothSetupResultHandler* aRes);
protected:
void HandleSvc(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes);
private:
// Responses
//
typedef mozilla::ipc::DaemonResultRunnable0<
BluetoothSetupResultHandler, void>
ResultRunnable;
typedef mozilla::ipc::DaemonResultRunnable1<
BluetoothSetupResultHandler, void, BluetoothStatus, BluetoothStatus>
ErrorRunnable;
void
ErrorRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes);
void
RegisterModuleRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes);
void
UnregisterModuleRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes);
void
ConfigurationRsp(const DaemonSocketPDUHeader& aHeader,
DaemonSocketPDU& aPDU,
BluetoothSetupResultHandler* aRes);
};
END_BLUETOOTH_NAMESPACE
#endif // mozilla_dom_bluetooth_bluedroid_BluetoothDaemonSetupInterface_h