Bug 1181478 - Expose BluetoothGattServer object in BluetoothAdapter. r=btian, r=mrbkap

This commit is contained in:
Bruce Sun 2015-07-24 16:32:35 +08:00
parent 258804a4dd
commit 4998a6532a
9 changed files with 188 additions and 0 deletions

View File

@ -174,6 +174,10 @@ DOMInterfaces = {
'nativeType': 'mozilla::dom::bluetooth::BluetoothGattDescriptor',
},
'BluetoothGattServer': {
'nativeType': 'mozilla::dom::bluetooth::BluetoothGattServer',
},
'BluetoothGattService': {
'nativeType': 'mozilla::dom::bluetooth::BluetoothGattService',
},

View File

@ -22,6 +22,7 @@
#include "mozilla/dom/bluetooth/BluetoothClassOfDevice.h"
#include "mozilla/dom/bluetooth/BluetoothDevice.h"
#include "mozilla/dom/bluetooth/BluetoothDiscoveryHandle.h"
#include "mozilla/dom/bluetooth/BluetoothGattServer.h"
#include "mozilla/dom/bluetooth/BluetoothPairingListener.h"
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
@ -361,6 +362,27 @@ BluetoothAdapter::Cleanup()
}
}
BluetoothGattServer*
BluetoothAdapter::GetGattServer()
{
/* Only expose GATT server if the adapter is enabled. It would be worth
* noting that the enabling state and the disabling state are just
* intermediate states, and the adapter would change into the enabled state
* or the disabled state sooner or later. So we invalidate and nullify the
* created GATT server object only when the adapter changes to a steady
* state, i.e., the disabled state.
*/
if (mState != BluetoothAdapterState::Enabled) {
return nullptr;
}
if (!mGattServer) {
mGattServer = new BluetoothGattServer(GetOwner());
}
return mGattServer;
}
void
BluetoothAdapter::GetPairedDeviceProperties(
const nsTArray<nsString>& aDeviceAddresses)
@ -391,6 +413,10 @@ BluetoothAdapter::SetPropertyByValue(const BluetoothNamedValue& aValue)
if (mState == BluetoothAdapterState::Disabled) {
mDevices.Clear();
mLeScanHandleArray.Clear();
if (mGattServer) {
mGattServer->Invalidate();
mGattServer = nullptr;
}
}
} else if (name.EqualsLiteral("Name")) {
mName = value.get_nsString();
@ -974,6 +1000,13 @@ BluetoothAdapter::SetAdapterState(BluetoothAdapterState aState)
mState = aState;
if (mState == BluetoothAdapterState::Disabled) {
if (mGattServer) {
mGattServer->Invalidate();
mGattServer = nullptr;
}
}
// Fire BluetoothAttributeEvent for changed adapter state
Sequence<nsString> types;
BT_APPEND_ENUM_STRING_FALLIBLE(types,

View File

@ -28,6 +28,7 @@ BEGIN_BLUETOOTH_NAMESPACE
class BluetoothDevice;
class BluetoothDiscoveryHandle;
class BluetoothGattServer;
class BluetoothNamedValue;
class BluetoothPairingListener;
class BluetoothSignal;
@ -77,6 +78,8 @@ public:
return mPairingReqs;
}
BluetoothGattServer* GetGattServer();
/****************************************************************************
* Event Handlers
***************************************************************************/
@ -357,6 +360,18 @@ private:
*/
bool mDiscovering;
/**
* GATT server object of this adapter.
*
* A new GATT server object will be created at the first time when
* |GetGattServer| is called after the adapter has been enabled. If the
* adapter has been disabled later on, the created GATT server will be
* discard by the adapter, and this GATT object should stop working till the
* end of its life. When |GetGattServer| is called after the adapter has been
* enabled again, a new GATT server object will be created.
*/
nsRefPtr<BluetoothGattServer> mGattServer;
/**
* Handle to fire pairing requests of different pairing types.
*/

View File

@ -0,0 +1,50 @@
/* -*- 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 "BluetoothGattServer.h"
using namespace mozilla;
using namespace mozilla::dom;
USING_BLUETOOTH_NAMESPACE
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BluetoothGattServer,
mOwner)
NS_IMPL_CYCLE_COLLECTING_ADDREF(BluetoothGattServer)
NS_IMPL_CYCLE_COLLECTING_RELEASE(BluetoothGattServer)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BluetoothGattServer)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
BluetoothGattServer::BluetoothGattServer(nsPIDOMWindow* aOwner)
: mOwner(aOwner)
, mValid(true)
{
}
BluetoothGattServer::~BluetoothGattServer()
{
Invalidate();
}
JSObject*
BluetoothGattServer::WrapObject(JSContext* aContext,
JS::Handle<JSObject*> aGivenProto)
{
return BluetoothGattServerBinding::Wrap(aContext, this, aGivenProto);
}
void
BluetoothGattServer::Invalidate()
{
mValid = false;
/* TODO: add tear down stuff here */
return;
}

View File

@ -0,0 +1,69 @@
/* -*- 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_bluetoothgattserver_h__
#define mozilla_dom_bluetooth_bluetoothgattserver_h__
#include "mozilla/dom/BluetoothGattServerBinding.h"
#include "mozilla/dom/bluetooth/BluetoothCommon.h"
#include "nsCOMPtr.h"
#include "nsPIDOMWindow.h"
#include "nsWrapperCache.h"
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothGattServer final : public nsISupports
, public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(BluetoothGattServer)
/****************************************************************************
* Attribute Getters
***************************************************************************/
/****************************************************************************
* Event Handlers
***************************************************************************/
/****************************************************************************
* Methods (Web API Implementation)
***************************************************************************/
/****************************************************************************
* Others
***************************************************************************/
nsPIDOMWindow* GetParentObject() const
{
return mOwner;
}
virtual JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
BluetoothGattServer(nsPIDOMWindow* aOwner);
/* Invalidate the GATT server.
* If the BluetoothAdapter turns off, existing BluetoothGattServer instances
* should stop working till the end of life.
*/
void Invalidate();
private:
~BluetoothGattServer();
/****************************************************************************
* Variables
***************************************************************************/
nsCOMPtr<nsPIDOMWindow> mOwner;
bool mValid;
};
END_BLUETOOTH_NAMESPACE
#endif

View File

@ -51,6 +51,7 @@ if CONFIG['MOZ_B2G_BT']:
'bluetooth2/BluetoothGatt.cpp',
'bluetooth2/BluetoothGattCharacteristic.cpp',
'bluetooth2/BluetoothGattDescriptor.cpp',
'bluetooth2/BluetoothGattServer.cpp',
'bluetooth2/BluetoothGattService.cpp',
'bluetooth2/BluetoothLeDeviceEvent.cpp',
'bluetooth2/BluetoothManager.cpp',
@ -189,6 +190,7 @@ else:
'bluetooth2/BluetoothGatt.h',
'bluetooth2/BluetoothGattCharacteristic.h',
'bluetooth2/BluetoothGattDescriptor.h',
'bluetooth2/BluetoothGattServer.h',
'bluetooth2/BluetoothGattService.h',
'bluetooth2/BluetoothLeDeviceEvent.h',
'bluetooth2/BluetoothManager.h',

View File

@ -40,6 +40,7 @@ interface BluetoothAdapter : EventTarget {
readonly attribute DOMString name;
readonly attribute boolean discoverable;
readonly attribute boolean discovering;
readonly attribute BluetoothGattServer? gattServer;
[AvailableIn=CertifiedApps]
readonly attribute BluetoothPairingListener pairingReqs;

View File

@ -0,0 +1,13 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*/
[CheckAnyPermissions="bluetooth"]
interface BluetoothGattServer
{
/* The implementation of BluetoothGattServer will come later.
* (see dependent bugs of bug 933358)
*/
};

View File

@ -673,6 +673,7 @@ if CONFIG['MOZ_B2G_BT']:
'BluetoothGatt.webidl',
'BluetoothGattCharacteristic.webidl',
'BluetoothGattDescriptor.webidl',
'BluetoothGattServer.webidl',
'BluetoothGattService.webidl',
'BluetoothLeDeviceEvent.webidl',
'BluetoothManager2.webidl',