From 0ff4a88b977ddcf77009156ebe0ce140be94cb9b Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Thu, 17 Sep 2015 10:08:47 +0200 Subject: [PATCH] Bug 1205253: Move |BluetoothDaemonCoreModule| into separate compilation unit, r=btian This patch moves |BluetoothDaemonCoreModule| into its own compilation unit. The notifications handlers are not set via getter/setter methods o fthis class. No further code changes are made. --- .../BluetoothDaemonCoreInterface.cpp | 1194 +++++++++++++++++ .../bluedroid/BluetoothDaemonCoreInterface.h | 308 +++++ .../bluedroid/BluetoothDaemonInterface.cpp | 1188 +--------------- dom/bluetooth/moz.build | 1 + 4 files changed, 1516 insertions(+), 1175 deletions(-) create mode 100644 dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.cpp create mode 100644 dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.h diff --git a/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.cpp b/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.cpp new file mode 100644 index 00000000000..cec4110fa4a --- /dev/null +++ b/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.cpp @@ -0,0 +1,1194 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=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 "BluetoothDaemonCoreInterface.h" +#include "mozilla/unused.h" + +BEGIN_BLUETOOTH_NAMESPACE + +using namespace mozilla::ipc; + +// +// Core module +// + +const int BluetoothDaemonCoreModule::MAX_NUM_CLIENTS = 1; + +BluetoothNotificationHandler* BluetoothDaemonCoreModule::sNotificationHandler; + +void +BluetoothDaemonCoreModule::SetNotificationHandler( + BluetoothNotificationHandler* aNotificationHandler) +{ + sNotificationHandler = aNotificationHandler; +} + +BluetoothNotificationHandler* +BluetoothDaemonCoreModule::GetNotificationHandler() +{ + return sNotificationHandler; +} + +void +BluetoothDaemonCoreModule::HandleSvc(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + DaemonSocketResultHandler* aRes) +{ + static void (BluetoothDaemonCoreModule::* const HandleOp[])( + const DaemonSocketPDUHeader&, DaemonSocketPDU&, + DaemonSocketResultHandler*) = { + [0] = &BluetoothDaemonCoreModule::HandleRsp, + [1] = &BluetoothDaemonCoreModule::HandleNtf + }; + + MOZ_ASSERT(!NS_IsMainThread()); + + (this->*(HandleOp[!!(aHeader.mOpcode & 0x80)]))(aHeader, aPDU, aRes); +} + +// Commands +// + +nsresult +BluetoothDaemonCoreModule::EnableCmd(BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x01, 0)); + + nsresult rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::DisableCmd(BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x02, 0)); + + nsresult rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::GetAdapterPropertiesCmd( + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x03, 0)); + + nsresult rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::GetAdapterPropertyCmd(const nsAString& aName, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x04, 0)); + + nsresult rv = PackPDU( + PackConversion(aName), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::SetAdapterPropertyCmd( + const BluetoothNamedValue& aProperty, BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x05, 0)); + + nsresult rv = PackPDU(aProperty, *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::GetRemoteDevicePropertiesCmd( + const nsAString& aRemoteAddr, BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x06, 0)); + + nsresult rv = PackPDU( + PackConversion(aRemoteAddr), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::GetRemoteDevicePropertyCmd( + const nsAString& aRemoteAddr, + const nsAString& aName, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x07, 0)); + + nsresult rv = PackPDU( + PackConversion(aRemoteAddr), + PackConversion(aName), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::SetRemoteDevicePropertyCmd( + const nsAString& aRemoteAddr, + const BluetoothNamedValue& aProperty, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x08, 0)); + + nsresult rv = PackPDU( + PackConversion(aRemoteAddr), + aProperty, *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::GetRemoteServiceRecordCmd( + const nsAString& aRemoteAddr, const uint8_t aUuid[16], + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x09, 0)); + + nsresult rv = PackPDU( + PackConversion(aRemoteAddr), + PackArray(aUuid, 16), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::GetRemoteServicesCmd( + const nsAString& aRemoteAddr, BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0a, 0)); + + nsresult rv = PackPDU( + PackConversion(aRemoteAddr), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::StartDiscoveryCmd(BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0b, 0)); + + nsresult rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::CancelDiscoveryCmd(BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0c, 0)); + + nsresult rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::CreateBondCmd(const nsAString& aBdAddr, + BluetoothTransport aTransport, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0d, 0)); + +#if ANDROID_VERSION >= 21 + nsresult rv = PackPDU( + PackConversion(aBdAddr), aTransport, *pdu); +#else + nsresult rv = PackPDU( + PackConversion(aBdAddr), *pdu); +#endif + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::RemoveBondCmd(const nsAString& aBdAddr, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0e, 0)); + + nsresult rv = PackPDU( + PackConversion(aBdAddr), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::CancelBondCmd(const nsAString& aBdAddr, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0f, 0)); + + nsresult rv = PackPDU( + PackConversion(aBdAddr), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::PinReplyCmd(const nsAString& aBdAddr, bool aAccept, + const nsAString& aPinCode, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x10, 0)); + + nsresult rv = PackPDU( + PackConversion(aBdAddr), + aAccept, + PackConversion(aPinCode), *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::SspReplyCmd(const nsAString& aBdAddr, + BluetoothSspVariant aVariant, + bool aAccept, uint32_t aPasskey, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x11, 0)); + + nsresult rv = PackPDU( + PackConversion(aBdAddr), + aVariant, aAccept, aPasskey, *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::DutModeConfigureCmd(bool aEnable, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x12, 0)); + + nsresult rv = PackPDU(aEnable, *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::DutModeSendCmd(uint16_t aOpcode, + uint8_t* aBuf, uint8_t aLen, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x13, 0)); + + nsresult rv = PackPDU(aOpcode, aLen, PackArray(aBuf, aLen), + *pdu); + if (NS_FAILED(rv)) { + return rv; + } + rv = Send(pdu, aRes); + if (NS_FAILED(rv)) { + return rv; + } + unused << pdu.forget(); + return rv; +} + +nsresult +BluetoothDaemonCoreModule::LeTestModeCmd(uint16_t aOpcode, + uint8_t* aBuf, uint8_t aLen, + BluetoothResultHandler* aRes) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x14, 0)); + + nsresult rv = PackPDU(aOpcode, aLen, PackArray(aBuf, 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 +BluetoothDaemonCoreModule::ErrorRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ErrorRunnable::Dispatch( + aRes, &BluetoothResultHandler::OnError, UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::EnableRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::Enable, UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::DisableRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::Disable, UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::GetAdapterPropertiesRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::GetAdapterProperties, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::GetAdapterPropertyRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::GetAdapterProperty, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::SetAdapterPropertyRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::SetAdapterProperty, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::GetRemoteDevicePropertiesRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::GetRemoteDeviceProperties, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::GetRemoteDevicePropertyRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::GetRemoteDeviceProperty, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::SetRemoteDevicePropertyRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::SetRemoteDeviceProperty, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::GetRemoteServiceRecordRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::GetRemoteServiceRecord, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::GetRemoteServicesRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::GetRemoteServices, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::StartDiscoveryRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::StartDiscovery, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::CancelDiscoveryRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::CancelDiscovery, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::CreateBondRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::CreateBond, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::RemoveBondRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::RemoveBond, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::CancelBondRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::CancelBond, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::PinReplyRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::PinReply, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::SspReplyRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::SspReply, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::DutModeConfigureRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::DutModeConfigure, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::DutModeSendRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::DutModeSend, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::LeTestModeRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes) +{ + ResultRunnable::Dispatch( + aRes, &BluetoothResultHandler::LeTestMode, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::HandleRsp( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + DaemonSocketResultHandler* aRes) +{ + static void (BluetoothDaemonCoreModule::* const HandleRsp[])( + const DaemonSocketPDUHeader&, + DaemonSocketPDU&, + BluetoothResultHandler*) = { + [0x00] = &BluetoothDaemonCoreModule::ErrorRsp, + [0x01] = &BluetoothDaemonCoreModule::EnableRsp, + [0x02] = &BluetoothDaemonCoreModule::DisableRsp, + [0x03] = &BluetoothDaemonCoreModule::GetAdapterPropertiesRsp, + [0x04] = &BluetoothDaemonCoreModule::GetAdapterPropertyRsp, + [0x05] = &BluetoothDaemonCoreModule::SetAdapterPropertyRsp, + [0x06] = &BluetoothDaemonCoreModule::GetRemoteDevicePropertiesRsp, + [0x07] = &BluetoothDaemonCoreModule::GetRemoteDevicePropertyRsp, + [0x08] = &BluetoothDaemonCoreModule::SetRemoteDevicePropertyRsp, + [0x09] = &BluetoothDaemonCoreModule::GetRemoteServiceRecordRsp, + [0x0a] = &BluetoothDaemonCoreModule::GetRemoteServicesRsp, + [0x0b] = &BluetoothDaemonCoreModule::StartDiscoveryRsp, + [0x0c] = &BluetoothDaemonCoreModule::CancelDiscoveryRsp, + [0x0d] = &BluetoothDaemonCoreModule::CreateBondRsp, + [0x0e] = &BluetoothDaemonCoreModule::RemoveBondRsp, + [0x0f] = &BluetoothDaemonCoreModule::CancelBondRsp, + [0x10] = &BluetoothDaemonCoreModule::PinReplyRsp, + [0x11] = &BluetoothDaemonCoreModule::SspReplyRsp, + [0x12] = &BluetoothDaemonCoreModule::DutModeConfigureRsp, + [0x13] = &BluetoothDaemonCoreModule::DutModeSendRsp, + [0x14] = &BluetoothDaemonCoreModule::LeTestModeRsp, + }; + + MOZ_ASSERT(!NS_IsMainThread()); + + if (NS_WARN_IF(!(aHeader.mOpcode < MOZ_ARRAY_LENGTH(HandleRsp))) || + NS_WARN_IF(!HandleRsp[aHeader.mOpcode])) { + return; + } + + nsRefPtr res = + static_cast(aRes); + + if (!res) { + return; // Return early if no result handler has been set for response + } + + (this->*(HandleRsp[aHeader.mOpcode]))(aHeader, aPDU, res); +} + +// Notifications +// + +class BluetoothDaemonCoreModule::NotificationHandlerWrapper final +{ +public: + typedef BluetoothNotificationHandler ObjectType; + + static ObjectType* GetInstance() + { + MOZ_ASSERT(NS_IsMainThread()); + + return sNotificationHandler; + } +}; + +void +BluetoothDaemonCoreModule::AdapterStateChangedNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + AdapterStateChangedNotification::Dispatch( + &BluetoothNotificationHandler::AdapterStateChangedNotification, + UnpackPDUInitOp(aPDU)); +} + +// Init operator class for AdapterPropertiesNotification +class BluetoothDaemonCoreModule::AdapterPropertiesInitOp final + : private PDUInitOp +{ +public: + AdapterPropertiesInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (BluetoothStatus& aArg1, int& aArg2, + nsAutoArrayPtr& aArg3) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read status */ + nsresult rv = UnpackPDU(pdu, aArg1); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read number of properties */ + uint8_t numProperties; + rv = UnpackPDU(pdu, numProperties); + if (NS_FAILED(rv)) { + return rv; + } + aArg2 = numProperties; + + /* Read properties array */ + UnpackArray properties(aArg3, aArg2); + rv = UnpackPDU(pdu, properties); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::AdapterPropertiesNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + AdapterPropertiesNotification::Dispatch( + &BluetoothNotificationHandler::AdapterPropertiesNotification, + AdapterPropertiesInitOp(aPDU)); +} + +// Init operator class for RemoteDevicePropertiesNotification +class BluetoothDaemonCoreModule::RemoteDevicePropertiesInitOp final + : private PDUInitOp +{ +public: + RemoteDevicePropertiesInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (BluetoothStatus& aArg1, nsString& aArg2, int& aArg3, + nsAutoArrayPtr& aArg4) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read status */ + nsresult rv = UnpackPDU(pdu, aArg1); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read address */ + rv = UnpackPDU( + pdu, UnpackConversion(aArg2)); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read number of properties */ + uint8_t numProperties; + rv = UnpackPDU(pdu, numProperties); + if (NS_FAILED(rv)) { + return rv; + } + aArg3 = numProperties; + + /* Read properties array */ + UnpackArray properties(aArg4, aArg3); + rv = UnpackPDU(pdu, properties); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::RemoteDevicePropertiesNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + RemoteDevicePropertiesNotification::Dispatch( + &BluetoothNotificationHandler::RemoteDevicePropertiesNotification, + RemoteDevicePropertiesInitOp(aPDU)); +} + +// Init operator class for DeviceFoundNotification +class BluetoothDaemonCoreModule::DeviceFoundInitOp final + : private PDUInitOp +{ +public: + DeviceFoundInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (int& aArg1, nsAutoArrayPtr& aArg2) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read number of properties */ + uint8_t numProperties; + nsresult rv = UnpackPDU(pdu, numProperties); + if (NS_FAILED(rv)) { + return rv; + } + aArg1 = numProperties; + + /* Read properties array */ + UnpackArray properties(aArg2, aArg1); + rv = UnpackPDU(pdu, properties); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::DeviceFoundNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + DeviceFoundNotification::Dispatch( + &BluetoothNotificationHandler::DeviceFoundNotification, + DeviceFoundInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::DiscoveryStateChangedNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + DiscoveryStateChangedNotification::Dispatch( + &BluetoothNotificationHandler::DiscoveryStateChangedNotification, + UnpackPDUInitOp(aPDU)); +} + +// Init operator class for PinRequestNotification +class BluetoothDaemonCoreModule::PinRequestInitOp final + : private PDUInitOp +{ +public: + PinRequestInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (nsString& aArg1, nsString& aArg2, uint32_t& aArg3) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read remote address */ + nsresult rv = UnpackPDU( + pdu, UnpackConversion(aArg1)); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read remote name */ + rv = UnpackPDU( + pdu, UnpackConversion(aArg2)); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read CoD */ + rv = UnpackPDU(pdu, aArg3); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::PinRequestNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + PinRequestNotification::Dispatch( + &BluetoothNotificationHandler::PinRequestNotification, + PinRequestInitOp(aPDU)); +} + +// Init operator class for SspRequestNotification +class BluetoothDaemonCoreModule::SspRequestInitOp final + : private PDUInitOp +{ +public: + SspRequestInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (nsString& aArg1, nsString& aArg2, uint32_t& aArg3, + BluetoothSspVariant& aArg4, uint32_t& aArg5) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read remote address */ + nsresult rv = UnpackPDU( + pdu, UnpackConversion(aArg1)); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read remote name */ + rv = UnpackPDU( + pdu, UnpackConversion(aArg2)); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read CoD */ + rv = UnpackPDU(pdu, aArg3); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read pairing variant */ + rv = UnpackPDU(pdu, aArg4); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read passkey */ + rv = UnpackPDU(pdu, aArg5); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::SspRequestNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + SspRequestNotification::Dispatch( + &BluetoothNotificationHandler::SspRequestNotification, + SspRequestInitOp(aPDU)); +} + +// Init operator class for BondStateChangedNotification +class BluetoothDaemonCoreModule::BondStateChangedInitOp final + : private PDUInitOp +{ +public: + BondStateChangedInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (BluetoothStatus& aArg1, nsString& aArg2, + BluetoothBondState& aArg3) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read status */ + nsresult rv = UnpackPDU(pdu, aArg1); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read remote address */ + rv = UnpackPDU( + pdu, UnpackConversion(aArg2)); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read bond state */ + rv = UnpackPDU(pdu, aArg3); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::BondStateChangedNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + BondStateChangedNotification::Dispatch( + &BluetoothNotificationHandler::BondStateChangedNotification, + BondStateChangedInitOp(aPDU)); +} + +// Init operator class for AclStateChangedNotification +class BluetoothDaemonCoreModule::AclStateChangedInitOp final + : private PDUInitOp +{ +public: + AclStateChangedInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (BluetoothStatus& aArg1, nsString& aArg2, bool& aArg3) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read status */ + nsresult rv = UnpackPDU(pdu, aArg1); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read remote address */ + rv = UnpackPDU( + pdu, UnpackConversion(aArg2)); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read ACL state */ + rv = UnpackPDU( + pdu, UnpackConversion(aArg3)); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::AclStateChangedNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + AclStateChangedNotification::Dispatch( + &BluetoothNotificationHandler::AclStateChangedNotification, + AclStateChangedInitOp(aPDU)); +} + +// Init operator class for DutModeRecvNotification +class BluetoothDaemonCoreModule::DutModeRecvInitOp final + : private PDUInitOp +{ +public: + DutModeRecvInitOp(DaemonSocketPDU& aPDU) + : PDUInitOp(aPDU) + { } + + nsresult + operator () (uint16_t& aArg1, nsAutoArrayPtr& aArg2, + uint8_t& aArg3) const + { + DaemonSocketPDU& pdu = GetPDU(); + + /* Read opcode */ + nsresult rv = UnpackPDU(pdu, aArg1); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read length */ + rv = UnpackPDU(pdu, aArg3); + if (NS_FAILED(rv)) { + return rv; + } + + /* Read data */ + rv = UnpackPDU(pdu, UnpackArray(aArg2, aArg3)); + if (NS_FAILED(rv)) { + return rv; + } + WarnAboutTrailingData(); + return NS_OK; + } +}; + +void +BluetoothDaemonCoreModule::DutModeRecvNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + DutModeRecvNotification::Dispatch( + &BluetoothNotificationHandler::DutModeRecvNotification, + DutModeRecvInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::LeTestModeNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU) +{ + LeTestModeNotification::Dispatch( + &BluetoothNotificationHandler::LeTestModeNotification, + UnpackPDUInitOp(aPDU)); +} + +void +BluetoothDaemonCoreModule::HandleNtf( + const DaemonSocketPDUHeader& aHeader, DaemonSocketPDU& aPDU, + DaemonSocketResultHandler* aRes) +{ + static void (BluetoothDaemonCoreModule::* const HandleNtf[])( + const DaemonSocketPDUHeader&, DaemonSocketPDU&) = { + [0] = &BluetoothDaemonCoreModule::AdapterStateChangedNtf, + [1] = &BluetoothDaemonCoreModule::AdapterPropertiesNtf, + [2] = &BluetoothDaemonCoreModule::RemoteDevicePropertiesNtf, + [3] = &BluetoothDaemonCoreModule::DeviceFoundNtf, + [4] = &BluetoothDaemonCoreModule::DiscoveryStateChangedNtf, + [5] = &BluetoothDaemonCoreModule::PinRequestNtf, + [6] = &BluetoothDaemonCoreModule::SspRequestNtf, + [7] = &BluetoothDaemonCoreModule::BondStateChangedNtf, + [8] = &BluetoothDaemonCoreModule::AclStateChangedNtf, + [9] = &BluetoothDaemonCoreModule::DutModeRecvNtf, + [10] = &BluetoothDaemonCoreModule::LeTestModeNtf + }; + + MOZ_ASSERT(!NS_IsMainThread()); + + uint8_t index = aHeader.mOpcode - 0x81; + + if (NS_WARN_IF(!(index < MOZ_ARRAY_LENGTH(HandleNtf))) || + NS_WARN_IF(!HandleNtf[index])) { + return; + } + + (this->*(HandleNtf[index]))(aHeader, aPDU); +} + +END_BLUETOOTH_NAMESPACE diff --git a/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.h b/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.h new file mode 100644 index 00000000000..a7950229718 --- /dev/null +++ b/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.h @@ -0,0 +1,308 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=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_BluetoothDaemonCoreInterface_h +#define mozilla_dom_bluetooth_bluedroid_BluetoothDaemonCoreInterface_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 BluetoothDaemonCoreModule +{ +public: + + static const int MAX_NUM_CLIENTS; + + virtual nsresult Send(DaemonSocketPDU* aPDU, + DaemonSocketResultHandler* aRes) = 0; + + void SetNotificationHandler( + BluetoothNotificationHandler* aNotificationHandler); + + BluetoothNotificationHandler* GetNotificationHandler(); + + // + // Commands + // + + nsresult EnableCmd(BluetoothResultHandler* aRes); + + nsresult DisableCmd(BluetoothResultHandler* aRes); + + nsresult GetAdapterPropertiesCmd(BluetoothResultHandler* aRes); + + nsresult GetAdapterPropertyCmd(const nsAString& aName, + BluetoothResultHandler* aRes); + + nsresult SetAdapterPropertyCmd(const BluetoothNamedValue& aProperty, + BluetoothResultHandler* aRes); + + nsresult GetRemoteDevicePropertiesCmd(const nsAString& aRemoteAddr, + BluetoothResultHandler* aRes); + + nsresult GetRemoteDevicePropertyCmd(const nsAString& aRemoteAddr, + const nsAString& aName, + BluetoothResultHandler* aRes); + + nsresult SetRemoteDevicePropertyCmd(const nsAString& aRemoteAddr, + const BluetoothNamedValue& aProperty, + BluetoothResultHandler* aRes); + + nsresult GetRemoteServiceRecordCmd(const nsAString& aRemoteAddr, + const uint8_t aUuid[16], + BluetoothResultHandler* aRes); + + nsresult GetRemoteServicesCmd(const nsAString& aRemoteAddr, + BluetoothResultHandler* aRes); + + nsresult StartDiscoveryCmd(BluetoothResultHandler* aRes); + + nsresult CancelDiscoveryCmd(BluetoothResultHandler* aRes); + + nsresult CreateBondCmd(const nsAString& aBdAddr, + BluetoothTransport aTransport, + BluetoothResultHandler* aRes); + + nsresult RemoveBondCmd(const nsAString& aBdAddr, + BluetoothResultHandler* aRes); + + nsresult CancelBondCmd(const nsAString& aBdAddr, + BluetoothResultHandler* aRes); + + nsresult PinReplyCmd(const nsAString& aBdAddr, bool aAccept, + const nsAString& aPinCode, + BluetoothResultHandler* aRes); + + nsresult SspReplyCmd(const nsAString& aBdAddr, BluetoothSspVariant aVariant, + bool aAccept, uint32_t aPasskey, + BluetoothResultHandler* aRes); + + nsresult DutModeConfigureCmd(bool aEnable, BluetoothResultHandler* aRes); + + nsresult DutModeSendCmd(uint16_t aOpcode, uint8_t* aBuf, uint8_t aLen, + BluetoothResultHandler* aRes); + + nsresult LeTestModeCmd(uint16_t aOpcode, uint8_t* aBuf, uint8_t aLen, + BluetoothResultHandler* aRes); + +protected: + void HandleSvc(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes); + +private: + + // + // Responses + // + + typedef mozilla::ipc::DaemonResultRunnable0< + BluetoothResultHandler, void> + ResultRunnable; + + typedef mozilla::ipc::DaemonResultRunnable1< + BluetoothResultHandler, void, BluetoothStatus, BluetoothStatus> + ErrorRunnable; + + void ErrorRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void EnableRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void DisableRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void GetAdapterPropertiesRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void GetAdapterPropertyRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void SetAdapterPropertyRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void GetRemoteDevicePropertiesRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void + GetRemoteDevicePropertyRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void SetRemoteDevicePropertyRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + void GetRemoteServiceRecordRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + void GetRemoteServicesRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void StartDiscoveryRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + void CancelDiscoveryRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void CreateBondRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + void RemoveBondRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + void CancelBondRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void PinReplyRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + void SspReplyRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void DutModeConfigureRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void DutModeSendRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void LeTestModeRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, + BluetoothResultHandler* aRes); + + void HandleRsp(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes); + + // + // Notifications + // + + class NotificationHandlerWrapper; + + typedef mozilla::ipc::DaemonNotificationRunnable1< + NotificationHandlerWrapper, void, bool> + AdapterStateChangedNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable3< + NotificationHandlerWrapper, void, BluetoothStatus, int, + nsAutoArrayPtr, BluetoothStatus, int, + const BluetoothProperty*> + AdapterPropertiesNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable4< + NotificationHandlerWrapper, void, BluetoothStatus, nsString, int, + nsAutoArrayPtr, BluetoothStatus, const nsAString&, + int, const BluetoothProperty*> + RemoteDevicePropertiesNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable2< + NotificationHandlerWrapper, void, int, nsAutoArrayPtr, + int, const BluetoothProperty*> + DeviceFoundNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable1< + NotificationHandlerWrapper, void, bool> + DiscoveryStateChangedNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable3< + NotificationHandlerWrapper, void, nsString, nsString, uint32_t, + const nsAString&, const nsAString&> + PinRequestNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable5< + NotificationHandlerWrapper, void, nsString, nsString, uint32_t, + BluetoothSspVariant, uint32_t, const nsAString&, const nsAString&> + SspRequestNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable3< + NotificationHandlerWrapper, void, BluetoothStatus, nsString, + BluetoothBondState, BluetoothStatus, const nsAString&> + BondStateChangedNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable3< + NotificationHandlerWrapper, void, BluetoothStatus, nsString, bool, + BluetoothStatus, const nsAString&> + AclStateChangedNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable3< + NotificationHandlerWrapper, void, uint16_t, nsAutoArrayPtr, + uint8_t, uint16_t, const uint8_t*> + DutModeRecvNotification; + + typedef mozilla::ipc::DaemonNotificationRunnable2< + NotificationHandlerWrapper, void, BluetoothStatus, uint16_t> + LeTestModeNotification; + + class AclStateChangedInitOp; + class AdapterPropertiesInitOp; + class BondStateChangedInitOp; + class DeviceFoundInitOp; + class DutModeRecvInitOp; + class PinRequestInitOp; + class RemoteDevicePropertiesInitOp; + class SspRequestInitOp; + + void AdapterStateChangedNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void AdapterPropertiesNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void RemoteDevicePropertiesNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void DeviceFoundNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void DiscoveryStateChangedNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void PinRequestNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void SspRequestNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void BondStateChangedNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void AclStateChangedNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void DutModeRecvNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void LeTestModeNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU); + + void HandleNtf(const DaemonSocketPDUHeader& aHeader, + DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes); + + static BluetoothNotificationHandler* sNotificationHandler; +}; + +END_BLUETOOTH_NAMESPACE + +#endif // mozilla_dom_bluetooth_bluedroid_BluetoothDaemonCoreInterface_h diff --git a/dom/bluetooth/bluedroid/BluetoothDaemonInterface.cpp b/dom/bluetooth/bluedroid/BluetoothDaemonInterface.cpp index 0f5069f5e1f..94733752af1 100644 --- a/dom/bluetooth/bluedroid/BluetoothDaemonInterface.cpp +++ b/dom/bluetooth/bluedroid/BluetoothDaemonInterface.cpp @@ -10,6 +10,7 @@ #include #include "BluetoothDaemonA2dpInterface.h" #include "BluetoothDaemonAvrcpInterface.h" +#include "BluetoothDaemonCoreInterface.h" #include "BluetoothDaemonGattInterface.h" #include "BluetoothDaemonHandsfreeInterface.h" #include "BluetoothDaemonHelpers.h" @@ -27,1174 +28,6 @@ using namespace mozilla::ipc; static const int sRetryInterval = 100; // ms -// -// Core module -// - -static BluetoothNotificationHandler* sNotificationHandler; - -class BluetoothDaemonCoreModule -{ -public: - - static const int MAX_NUM_CLIENTS; - - virtual nsresult Send(DaemonSocketPDU* aPDU, - DaemonSocketResultHandler* aRes) = 0; - - nsresult EnableCmd(BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x01, 0)); - - nsresult rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult DisableCmd(BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x02, 0)); - - nsresult rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult GetAdapterPropertiesCmd(BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x03, 0)); - - nsresult rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult GetAdapterPropertyCmd(const nsAString& aName, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x04, 0)); - - nsresult rv = PackPDU( - PackConversion(aName), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult SetAdapterPropertyCmd(const BluetoothNamedValue& aProperty, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x05, 0)); - - nsresult rv = PackPDU(aProperty, *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult GetRemoteDevicePropertiesCmd(const nsAString& aRemoteAddr, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x06, 0)); - - nsresult rv = PackPDU( - PackConversion(aRemoteAddr), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult GetRemoteDevicePropertyCmd(const nsAString& aRemoteAddr, - const nsAString& aName, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x07, 0)); - - nsresult rv = PackPDU( - PackConversion(aRemoteAddr), - PackConversion(aName), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult SetRemoteDevicePropertyCmd(const nsAString& aRemoteAddr, - const BluetoothNamedValue& aProperty, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x08, 0)); - - nsresult rv = PackPDU( - PackConversion(aRemoteAddr), - aProperty, *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult GetRemoteServiceRecordCmd(const nsAString& aRemoteAddr, - const uint8_t aUuid[16], - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x09, 0)); - - nsresult rv = PackPDU( - PackConversion(aRemoteAddr), - PackArray(aUuid, 16), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult GetRemoteServicesCmd(const nsAString& aRemoteAddr, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0a, 0)); - - nsresult rv = PackPDU( - PackConversion(aRemoteAddr), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult StartDiscoveryCmd(BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0b, 0)); - - nsresult rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult CancelDiscoveryCmd(BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0c, 0)); - - nsresult rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult CreateBondCmd(const nsAString& aBdAddr, - BluetoothTransport aTransport, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0d, 0)); - -#if ANDROID_VERSION >= 21 - nsresult rv = PackPDU( - PackConversion(aBdAddr), aTransport, *pdu); -#else - nsresult rv = PackPDU( - PackConversion(aBdAddr), *pdu); -#endif - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult RemoveBondCmd(const nsAString& aBdAddr, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0e, 0)); - - nsresult rv = PackPDU( - PackConversion(aBdAddr), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult CancelBondCmd(const nsAString& aBdAddr, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x0f, 0)); - - nsresult rv = PackPDU( - PackConversion(aBdAddr), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult PinReplyCmd(const nsAString& aBdAddr, bool aAccept, - const nsAString& aPinCode, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x10, 0)); - - nsresult rv = PackPDU( - PackConversion(aBdAddr), - aAccept, - PackConversion(aPinCode), *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult SspReplyCmd(const nsAString& aBdAddr, BluetoothSspVariant aVariant, - bool aAccept, uint32_t aPasskey, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x11, 0)); - - nsresult rv = PackPDU( - PackConversion(aBdAddr), - aVariant, aAccept, aPasskey, *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult DutModeConfigureCmd(bool aEnable, BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x12, 0)); - - nsresult rv = PackPDU(aEnable, *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult DutModeSendCmd(uint16_t aOpcode, uint8_t* aBuf, uint8_t aLen, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x13, 0)); - - nsresult rv = PackPDU(aOpcode, aLen, PackArray(aBuf, aLen), - *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - - nsresult LeTestModeCmd(uint16_t aOpcode, uint8_t* aBuf, uint8_t aLen, - BluetoothResultHandler* aRes) - { - MOZ_ASSERT(NS_IsMainThread()); - - nsAutoPtr pdu(new DaemonSocketPDU(0x01, 0x14, 0)); - - nsresult rv = PackPDU(aOpcode, aLen, PackArray(aBuf, aLen), - *pdu); - if (NS_FAILED(rv)) { - return rv; - } - rv = Send(pdu, aRes); - if (NS_FAILED(rv)) { - return rv; - } - unused << pdu.forget(); - return rv; - } - -protected: - - void HandleSvc(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes) - { - static void (BluetoothDaemonCoreModule::* const HandleOp[])( - const DaemonSocketPDUHeader&, DaemonSocketPDU&, - DaemonSocketResultHandler*) = { - [0] = &BluetoothDaemonCoreModule::HandleRsp, - [1] = &BluetoothDaemonCoreModule::HandleNtf - }; - - MOZ_ASSERT(!NS_IsMainThread()); - - (this->*(HandleOp[!!(aHeader.mOpcode & 0x80)]))(aHeader, aPDU, aRes); - } - -private: - - // Responses - // - - typedef mozilla::ipc::DaemonResultRunnable0< - BluetoothResultHandler, void> - ResultRunnable; - - typedef mozilla::ipc::DaemonResultRunnable1< - BluetoothResultHandler, void, BluetoothStatus, BluetoothStatus> - ErrorRunnable; - - void ErrorRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ErrorRunnable::Dispatch( - aRes, &BluetoothResultHandler::OnError, UnpackPDUInitOp(aPDU)); - } - - void EnableRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::Enable, UnpackPDUInitOp(aPDU)); - } - - void DisableRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::Disable, UnpackPDUInitOp(aPDU)); - } - - void GetAdapterPropertiesRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::GetAdapterProperties, - UnpackPDUInitOp(aPDU)); - } - - void GetAdapterPropertyRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::GetAdapterProperty, - UnpackPDUInitOp(aPDU)); - } - - void SetAdapterPropertyRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::SetAdapterProperty, - UnpackPDUInitOp(aPDU)); - } - - void GetRemoteDevicePropertiesRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::GetRemoteDeviceProperties, - UnpackPDUInitOp(aPDU)); - } - - void - GetRemoteDevicePropertyRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::GetRemoteDeviceProperty, - UnpackPDUInitOp(aPDU)); - } - - void SetRemoteDevicePropertyRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::SetRemoteDeviceProperty, - UnpackPDUInitOp(aPDU)); - } - - void GetRemoteServiceRecordRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::GetRemoteServiceRecord, - UnpackPDUInitOp(aPDU)); - } - - void GetRemoteServicesRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::GetRemoteServices, - UnpackPDUInitOp(aPDU)); - } - - void StartDiscoveryRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::StartDiscovery, - UnpackPDUInitOp(aPDU)); - } - - void CancelDiscoveryRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::CancelDiscovery, - UnpackPDUInitOp(aPDU)); - } - - void CreateBondRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::CreateBond, - UnpackPDUInitOp(aPDU)); - } - - void RemoveBondRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::RemoveBond, - UnpackPDUInitOp(aPDU)); - } - - void CancelBondRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::CancelBond, - UnpackPDUInitOp(aPDU)); - } - - void PinReplyRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::PinReply, - UnpackPDUInitOp(aPDU)); - } - - void SspReplyRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::SspReply, - UnpackPDUInitOp(aPDU)); - } - - void DutModeConfigureRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::DutModeConfigure, - UnpackPDUInitOp(aPDU)); - } - - void DutModeSendRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::DutModeSend, - UnpackPDUInitOp(aPDU)); - } - - void LeTestModeRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, - BluetoothResultHandler* aRes) - { - ResultRunnable::Dispatch( - aRes, &BluetoothResultHandler::LeTestMode, - UnpackPDUInitOp(aPDU)); - } - - void HandleRsp(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes) - { - static void (BluetoothDaemonCoreModule::* const HandleRsp[])( - const DaemonSocketPDUHeader&, - DaemonSocketPDU&, - BluetoothResultHandler*) = { - [0x00] = &BluetoothDaemonCoreModule::ErrorRsp, - [0x01] = &BluetoothDaemonCoreModule::EnableRsp, - [0x02] = &BluetoothDaemonCoreModule::DisableRsp, - [0x03] = &BluetoothDaemonCoreModule::GetAdapterPropertiesRsp, - [0x04] = &BluetoothDaemonCoreModule::GetAdapterPropertyRsp, - [0x05] = &BluetoothDaemonCoreModule::SetAdapterPropertyRsp, - [0x06] = &BluetoothDaemonCoreModule::GetRemoteDevicePropertiesRsp, - [0x07] = &BluetoothDaemonCoreModule::GetRemoteDevicePropertyRsp, - [0x08] = &BluetoothDaemonCoreModule::SetRemoteDevicePropertyRsp, - [0x09] = &BluetoothDaemonCoreModule::GetRemoteServiceRecordRsp, - [0x0a] = &BluetoothDaemonCoreModule::GetRemoteServicesRsp, - [0x0b] = &BluetoothDaemonCoreModule::StartDiscoveryRsp, - [0x0c] = &BluetoothDaemonCoreModule::CancelDiscoveryRsp, - [0x0d] = &BluetoothDaemonCoreModule::CreateBondRsp, - [0x0e] = &BluetoothDaemonCoreModule::RemoveBondRsp, - [0x0f] = &BluetoothDaemonCoreModule::CancelBondRsp, - [0x10] = &BluetoothDaemonCoreModule::PinReplyRsp, - [0x11] = &BluetoothDaemonCoreModule::SspReplyRsp, - [0x12] = &BluetoothDaemonCoreModule::DutModeConfigureRsp, - [0x13] = &BluetoothDaemonCoreModule::DutModeSendRsp, - [0x14] = &BluetoothDaemonCoreModule::LeTestModeRsp, - }; - - MOZ_ASSERT(!NS_IsMainThread()); - - if (NS_WARN_IF(!(aHeader.mOpcode < MOZ_ARRAY_LENGTH(HandleRsp))) || - NS_WARN_IF(!HandleRsp[aHeader.mOpcode])) { - return; - } - - nsRefPtr res = - static_cast(aRes); - - if (!res) { - return; // Return early if no result handler has been set for response - } - - (this->*(HandleRsp[aHeader.mOpcode]))(aHeader, aPDU, res); - } - - // Notifications - // - - class NotificationHandlerWrapper - { - public: - typedef BluetoothNotificationHandler ObjectType; - - static ObjectType* GetInstance() - { - MOZ_ASSERT(NS_IsMainThread()); - - return sNotificationHandler; - } - }; - - typedef mozilla::ipc::DaemonNotificationRunnable1< - NotificationHandlerWrapper, void, bool> - AdapterStateChangedNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable3< - NotificationHandlerWrapper, void, BluetoothStatus, int, - nsAutoArrayPtr, BluetoothStatus, int, - const BluetoothProperty*> - AdapterPropertiesNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable4< - NotificationHandlerWrapper, void, BluetoothStatus, nsString, int, - nsAutoArrayPtr, BluetoothStatus, const nsAString&, - int, const BluetoothProperty*> - RemoteDevicePropertiesNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable2< - NotificationHandlerWrapper, void, int, nsAutoArrayPtr, - int, const BluetoothProperty*> - DeviceFoundNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable1< - NotificationHandlerWrapper, void, bool> - DiscoveryStateChangedNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable3< - NotificationHandlerWrapper, void, nsString, nsString, uint32_t, - const nsAString&, const nsAString&> - PinRequestNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable5< - NotificationHandlerWrapper, void, nsString, nsString, uint32_t, - BluetoothSspVariant, uint32_t, const nsAString&, const nsAString&> - SspRequestNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable3< - NotificationHandlerWrapper, void, BluetoothStatus, nsString, - BluetoothBondState, BluetoothStatus, const nsAString&> - BondStateChangedNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable3< - NotificationHandlerWrapper, void, BluetoothStatus, nsString, bool, - BluetoothStatus, const nsAString&> - AclStateChangedNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable3< - NotificationHandlerWrapper, void, uint16_t, nsAutoArrayPtr, - uint8_t, uint16_t, const uint8_t*> - DutModeRecvNotification; - - typedef mozilla::ipc::DaemonNotificationRunnable2< - NotificationHandlerWrapper, void, BluetoothStatus, uint16_t> - LeTestModeNotification; - - void AdapterStateChangedNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - AdapterStateChangedNotification::Dispatch( - &BluetoothNotificationHandler::AdapterStateChangedNotification, - UnpackPDUInitOp(aPDU)); - } - - // Init operator class for AdapterPropertiesNotification - class AdapterPropertiesInitOp final : private PDUInitOp - { - public: - AdapterPropertiesInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (BluetoothStatus& aArg1, int& aArg2, - nsAutoArrayPtr& aArg3) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read status */ - nsresult rv = UnpackPDU(pdu, aArg1); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read number of properties */ - uint8_t numProperties; - rv = UnpackPDU(pdu, numProperties); - if (NS_FAILED(rv)) { - return rv; - } - aArg2 = numProperties; - - /* Read properties array */ - UnpackArray properties(aArg3, aArg2); - rv = UnpackPDU(pdu, properties); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void AdapterPropertiesNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - AdapterPropertiesNotification::Dispatch( - &BluetoothNotificationHandler::AdapterPropertiesNotification, - AdapterPropertiesInitOp(aPDU)); - } - - // Init operator class for RemoteDevicePropertiesNotification - class RemoteDevicePropertiesInitOp final : private PDUInitOp - { - public: - RemoteDevicePropertiesInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (BluetoothStatus& aArg1, nsString& aArg2, int& aArg3, - nsAutoArrayPtr& aArg4) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read status */ - nsresult rv = UnpackPDU(pdu, aArg1); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read address */ - rv = UnpackPDU( - pdu, UnpackConversion(aArg2)); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read number of properties */ - uint8_t numProperties; - rv = UnpackPDU(pdu, numProperties); - if (NS_FAILED(rv)) { - return rv; - } - aArg3 = numProperties; - - /* Read properties array */ - UnpackArray properties(aArg4, aArg3); - rv = UnpackPDU(pdu, properties); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void RemoteDevicePropertiesNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - RemoteDevicePropertiesNotification::Dispatch( - &BluetoothNotificationHandler::RemoteDevicePropertiesNotification, - RemoteDevicePropertiesInitOp(aPDU)); - } - - // Init operator class for DeviceFoundNotification - class DeviceFoundInitOp final : private PDUInitOp - { - public: - DeviceFoundInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (int& aArg1, nsAutoArrayPtr& aArg2) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read number of properties */ - uint8_t numProperties; - nsresult rv = UnpackPDU(pdu, numProperties); - if (NS_FAILED(rv)) { - return rv; - } - aArg1 = numProperties; - - /* Read properties array */ - UnpackArray properties(aArg2, aArg1); - rv = UnpackPDU(pdu, properties); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void DeviceFoundNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - DeviceFoundNotification::Dispatch( - &BluetoothNotificationHandler::DeviceFoundNotification, - DeviceFoundInitOp(aPDU)); - } - - void DiscoveryStateChangedNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - DiscoveryStateChangedNotification::Dispatch( - &BluetoothNotificationHandler::DiscoveryStateChangedNotification, - UnpackPDUInitOp(aPDU)); - } - - // Init operator class for PinRequestNotification - class PinRequestInitOp final : private PDUInitOp - { - public: - PinRequestInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (nsString& aArg1, nsString& aArg2, uint32_t& aArg3) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read remote address */ - nsresult rv = UnpackPDU( - pdu, UnpackConversion(aArg1)); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read remote name */ - rv = UnpackPDU( - pdu, UnpackConversion(aArg2)); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read CoD */ - rv = UnpackPDU(pdu, aArg3); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void PinRequestNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - PinRequestNotification::Dispatch( - &BluetoothNotificationHandler::PinRequestNotification, - PinRequestInitOp(aPDU)); - } - - // Init operator class for SspRequestNotification - class SspRequestInitOp final : private PDUInitOp - { - public: - SspRequestInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (nsString& aArg1, nsString& aArg2, uint32_t& aArg3, - BluetoothSspVariant& aArg4, uint32_t& aArg5) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read remote address */ - nsresult rv = UnpackPDU( - pdu, UnpackConversion(aArg1)); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read remote name */ - rv = UnpackPDU( - pdu, UnpackConversion(aArg2)); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read CoD */ - rv = UnpackPDU(pdu, aArg3); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read pairing variant */ - rv = UnpackPDU(pdu, aArg4); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read passkey */ - rv = UnpackPDU(pdu, aArg5); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void SspRequestNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - SspRequestNotification::Dispatch( - &BluetoothNotificationHandler::SspRequestNotification, - SspRequestInitOp(aPDU)); - } - - // Init operator class for BondStateChangedNotification - class BondStateChangedInitOp final : private PDUInitOp - { - public: - BondStateChangedInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (BluetoothStatus& aArg1, nsString& aArg2, - BluetoothBondState& aArg3) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read status */ - nsresult rv = UnpackPDU(pdu, aArg1); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read remote address */ - rv = UnpackPDU( - pdu, UnpackConversion(aArg2)); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read bond state */ - rv = UnpackPDU(pdu, aArg3); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void BondStateChangedNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - BondStateChangedNotification::Dispatch( - &BluetoothNotificationHandler::BondStateChangedNotification, - BondStateChangedInitOp(aPDU)); - } - - // Init operator class for AclStateChangedNotification - class AclStateChangedInitOp final : private PDUInitOp - { - public: - AclStateChangedInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (BluetoothStatus& aArg1, nsString& aArg2, bool& aArg3) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read status */ - nsresult rv = UnpackPDU(pdu, aArg1); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read remote address */ - rv = UnpackPDU( - pdu, UnpackConversion(aArg2)); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read ACL state */ - rv = UnpackPDU( - pdu, UnpackConversion(aArg3)); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void AclStateChangedNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - AclStateChangedNotification::Dispatch( - &BluetoothNotificationHandler::AclStateChangedNotification, - AclStateChangedInitOp(aPDU)); - } - - // Init operator class for DutModeRecvNotification - class DutModeRecvInitOp final : private PDUInitOp - { - public: - DutModeRecvInitOp(DaemonSocketPDU& aPDU) - : PDUInitOp(aPDU) - { } - - nsresult - operator () (uint16_t& aArg1, nsAutoArrayPtr& aArg2, - uint8_t& aArg3) const - { - DaemonSocketPDU& pdu = GetPDU(); - - /* Read opcode */ - nsresult rv = UnpackPDU(pdu, aArg1); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read length */ - rv = UnpackPDU(pdu, aArg3); - if (NS_FAILED(rv)) { - return rv; - } - - /* Read data */ - rv = UnpackPDU(pdu, UnpackArray(aArg2, aArg3)); - if (NS_FAILED(rv)) { - return rv; - } - WarnAboutTrailingData(); - return NS_OK; - } - }; - - void DutModeRecvNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - DutModeRecvNotification::Dispatch( - &BluetoothNotificationHandler::DutModeRecvNotification, - DutModeRecvInitOp(aPDU)); - } - - void LeTestModeNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU) - { - LeTestModeNotification::Dispatch( - &BluetoothNotificationHandler::LeTestModeNotification, - UnpackPDUInitOp(aPDU)); - } - - void HandleNtf(const DaemonSocketPDUHeader& aHeader, - DaemonSocketPDU& aPDU, DaemonSocketResultHandler* aRes) - { - static void (BluetoothDaemonCoreModule::* const HandleNtf[])( - const DaemonSocketPDUHeader&, DaemonSocketPDU&) = { - [0] = &BluetoothDaemonCoreModule::AdapterStateChangedNtf, - [1] = &BluetoothDaemonCoreModule::AdapterPropertiesNtf, - [2] = &BluetoothDaemonCoreModule::RemoteDevicePropertiesNtf, - [3] = &BluetoothDaemonCoreModule::DeviceFoundNtf, - [4] = &BluetoothDaemonCoreModule::DiscoveryStateChangedNtf, - [5] = &BluetoothDaemonCoreModule::PinRequestNtf, - [6] = &BluetoothDaemonCoreModule::SspRequestNtf, - [7] = &BluetoothDaemonCoreModule::BondStateChangedNtf, - [8] = &BluetoothDaemonCoreModule::AclStateChangedNtf, - [9] = &BluetoothDaemonCoreModule::DutModeRecvNtf, - [10] = &BluetoothDaemonCoreModule::LeTestModeNtf - }; - - MOZ_ASSERT(!NS_IsMainThread()); - - uint8_t index = aHeader.mOpcode - 0x81; - - if (NS_WARN_IF(!(index < MOZ_ARRAY_LENGTH(HandleNtf))) || - NS_WARN_IF(!HandleNtf[index])) { - return; - } - - (this->*(HandleNtf[index]))(aHeader, aPDU); - } -}; - -const int BluetoothDaemonCoreModule::MAX_NUM_CLIENTS = 1; - // // Protocol handling // @@ -1644,13 +477,13 @@ BluetoothDaemonInterface::Init( // here. unused << NS_WARN_IF(property_set("ctl.stop", "bluetoothd")); - sNotificationHandler = aNotificationHandler; - mResultHandlerQ.AppendElement(aRes); if (!mProtocol) { mProtocol = new BluetoothDaemonProtocol(); } + static_cast(mProtocol)->SetNotificationHandler( + aNotificationHandler); if (!mListenSocket) { mListenSocket = new ListenSocket(this, LISTEN_SOCKET); @@ -1760,8 +593,8 @@ private: void BluetoothDaemonInterface::Cleanup(BluetoothResultHandler* aRes) { - - sNotificationHandler = nullptr; + static_cast(mProtocol)->SetNotificationHandler( + nullptr); // Cleanup, step 1: Unregister Socket module nsresult rv = mProtocol->UnregisterModuleCmd( @@ -2246,17 +1079,22 @@ BluetoothDaemonInterface::OnDisconnect(int aIndex) break; } + BluetoothNotificationHandler* notificationHandler = + static_cast(mProtocol)-> + GetNotificationHandler(); + /* For recovery make sure all sockets disconnected, in order to avoid * the remaining disconnects interfere with the restart procedure. */ - if (sNotificationHandler && mResultHandlerQ.IsEmpty()) { + if (notificationHandler && mResultHandlerQ.IsEmpty()) { if (mListenSocket->GetConnectionStatus() == SOCKET_DISCONNECTED && mCmdChannel->GetConnectionStatus() == SOCKET_DISCONNECTED && mNtfChannel->GetConnectionStatus() == SOCKET_DISCONNECTED) { // Assume daemon crashed during regular service; notify // BluetoothServiceBluedroid to prepare restart-daemon procedure - sNotificationHandler->BackendErrorNotification(true); - sNotificationHandler = nullptr; + notificationHandler->BackendErrorNotification(true); + static_cast(mProtocol)-> + SetNotificationHandler(nullptr); } } } diff --git a/dom/bluetooth/moz.build b/dom/bluetooth/moz.build index 48cc87235ae..65844c35f74 100644 --- a/dom/bluetooth/moz.build +++ b/dom/bluetooth/moz.build @@ -74,6 +74,7 @@ if CONFIG['MOZ_B2G_BT']: 'bluedroid/BluetoothAvrcpManager.cpp', 'bluedroid/BluetoothDaemonA2dpInterface.cpp', 'bluedroid/BluetoothDaemonAvrcpInterface.cpp', + 'bluedroid/BluetoothDaemonCoreInterface.cpp', 'bluedroid/BluetoothDaemonGattInterface.cpp', 'bluedroid/BluetoothDaemonHandsfreeInterface.cpp', 'bluedroid/BluetoothDaemonHelpers.cpp',