Bug 1027504 - Implement BluetoothClassOfDevice, r=bz, r=echou

This commit is contained in:
Ben Tian 2014-07-10 14:42:10 +08:00
parent 59f7f3a5cf
commit 85a5b3fe70
7 changed files with 251 additions and 5 deletions

View File

@ -173,6 +173,10 @@ DOMInterfaces = {
'nativeType': 'mozilla::dom::bluetooth::BluetoothDiscoveryHandle',
},
'BluetoothClassOfDevice': {
'nativeType': 'mozilla::dom::bluetooth::BluetoothClassOfDevice',
},
'CameraCapabilities': {
'nativeType': 'mozilla::dom::CameraCapabilities',
'headerFile': 'DOMCameraCapabilities.h'

View File

@ -0,0 +1,105 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "BluetoothClassOfDevice.h"
#include "mozilla/dom/BluetoothClassOfDeviceBinding.h"
#include "nsThreadUtils.h"
USING_BLUETOOTH_NAMESPACE
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BluetoothClassOfDevice, mOwnerWindow)
NS_IMPL_CYCLE_COLLECTING_ADDREF(BluetoothClassOfDevice)
NS_IMPL_CYCLE_COLLECTING_RELEASE(BluetoothClassOfDevice)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BluetoothClassOfDevice)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
/*
* Class of Device(CoD): 32-bit unsigned integer
*
* 31 24 23 13 12 8 7 2 1 0
* | | Major | Major | Minor | |
* | | service | device | device | |
* | | class | class | class | |
* | |<- 11 ->|<- 5 ->|<- 6 ->| |
*
* https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband
*/
// Bit 23 ~ Bit 13: Major service class
#define GET_MAJOR_SERVICE_CLASS(cod) (((cod) & 0xffe000) >> 13)
// Bit 12 ~ Bit 8: Major device class
#define GET_MAJOR_DEVICE_CLASS(cod) (((cod) & 0x1f00) >> 8)
// Bit 7 ~ Bit 2: Minor device class
#define GET_MINOR_DEVICE_CLASS(cod) (((cod) & 0xfc) >> 2)
BluetoothClassOfDevice::BluetoothClassOfDevice(nsPIDOMWindow* aOwner)
: mOwnerWindow(aOwner)
{
MOZ_ASSERT(aOwner);
SetIsDOMBinding();
Reset();
}
BluetoothClassOfDevice::~BluetoothClassOfDevice()
{}
void
BluetoothClassOfDevice::Reset()
{
mMajorServiceClass = 0x1; // LIMITED_DISCOVERABILITY
mMajorDeviceClass = 0x1F; // UNCATEGORIZED
mMinorDeviceClass = 0;
}
bool
BluetoothClassOfDevice::Equals(const uint32_t aValue)
{
return (mMajorServiceClass == GET_MAJOR_SERVICE_CLASS(aValue) &&
mMajorDeviceClass == GET_MAJOR_DEVICE_CLASS(aValue) &&
mMinorDeviceClass == GET_MINOR_DEVICE_CLASS(aValue));
}
uint32_t
BluetoothClassOfDevice::ToUint32()
{
return (mMajorServiceClass & 0x7ff) << 13 |
(mMajorDeviceClass & 0x1f) << 8 |
(mMinorDeviceClass & 0x3f) << 2;
}
void
BluetoothClassOfDevice::Update(const uint32_t aValue)
{
mMajorServiceClass = GET_MAJOR_SERVICE_CLASS(aValue);
mMajorDeviceClass = GET_MAJOR_DEVICE_CLASS(aValue);
mMinorDeviceClass = GET_MINOR_DEVICE_CLASS(aValue);
BT_API2_LOGR("aValue %x => majorService %x majorDevice %x minorDevice %x",
aValue, mMajorServiceClass, mMajorDeviceClass, mMinorDeviceClass);
}
// static
already_AddRefed<BluetoothClassOfDevice>
BluetoothClassOfDevice::Create(nsPIDOMWindow* aOwner)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aOwner);
nsRefPtr<BluetoothClassOfDevice> cod = new BluetoothClassOfDevice(aOwner);
return cod.forget();
}
JSObject*
BluetoothClassOfDevice::WrapObject(JSContext* aCx)
{
return BluetoothClassOfDeviceBinding::Wrap(aCx, this);
}

View File

@ -0,0 +1,92 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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_bluetoothclassofdevice_h
#define mozilla_dom_bluetooth_bluetoothclassofdevice_h
#include "BluetoothCommon.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "nsCycleCollectionParticipant.h"
#include "nsPIDOMWindow.h"
#include "nsWrapperCache.h"
struct JSContext;
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothClassOfDevice MOZ_FINAL : public nsISupports,
public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(BluetoothClassOfDevice)
static already_AddRefed<BluetoothClassOfDevice>
Create(nsPIDOMWindow* aOwner);
uint16_t MajorServiceClass() const
{
return mMajorServiceClass;
}
uint8_t MajorDeviceClass() const
{
return mMajorDeviceClass;
}
uint8_t MinorDeviceClass() const
{
return mMinorDeviceClass;
}
/**
* Compare whether CoD equals to CoD value.
*
* @param aValue [in] CoD value to compare
*/
bool Equals(const uint32_t aValue);
/**
* Convert CoD to uint32_t CoD value.
*
* TODO: Remove this function once we replace uint32_t cod value with
* BluetoothClassOfDevice in BluetoothProfileController.
*/
uint32_t ToUint32();
/**
* Update CoD.
*
* @param aValue [in] CoD value to update
*/
void Update(const uint32_t aValue);
nsPIDOMWindow* GetParentObject() const
{
return mOwnerWindow;
}
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
private:
BluetoothClassOfDevice(nsPIDOMWindow* aOwner);
~BluetoothClassOfDevice();
/**
* Reset CoD to default value.
*/
void Reset();
uint16_t mMajorServiceClass;
uint8_t mMajorDeviceClass;
uint8_t mMinorDeviceClass;
nsCOMPtr<nsPIDOMWindow> mOwnerWindow;
};
END_BLUETOOTH_NAMESPACE
#endif // mozilla_dom_bluetooth_bluetoothclassofdevice_h

View File

@ -27,19 +27,19 @@ BEGIN_BLUETOOTH_NAMESPACE
*/
// Bit 23 ~ Bit 13: Major service class
#define GET_MAJOR_SERVICE_CLASS(cod) ((cod & 0xffe000) >> 13)
#define GET_MAJOR_SERVICE_CLASS(cod) (((cod) & 0xffe000) >> 13)
// Bit 12 ~ Bit 8: Major device class
#define GET_MAJOR_DEVICE_CLASS(cod) ((cod & 0x1f00) >> 8)
#define GET_MAJOR_DEVICE_CLASS(cod) (((cod) & 0x1f00) >> 8)
// Bit 7 ~ Bit 2: Minor device class
#define GET_MINOR_DEVICE_CLASS(cod) ((cod & 0xfc) >> 2)
#define GET_MINOR_DEVICE_CLASS(cod) (((cod) & 0xfc) >> 2)
// Audio: Major service class = 0x100 (Bit 21 is set)
#define HAS_AUDIO(cod) (cod & 0x200000)
#define HAS_AUDIO(cod) ((cod) & 0x200000)
// Rendering: Major service class = 0x20 (Bit 18 is set)
#define HAS_RENDERING(cod) (cod & 0x40000)
#define HAS_RENDERING(cod) ((cod) & 0x40000)
// Peripheral: Major device class = 0x5
#define IS_PERIPHERAL(cod) (GET_MAJOR_DEVICE_CLASS(cod) == 0x5)

View File

@ -7,6 +7,7 @@
if CONFIG['MOZ_B2G_BT']:
SOURCES += [
'BluetoothAdapter.cpp',
'BluetoothClassOfDevice.cpp',
'BluetoothDevice.cpp',
'BluetoothDiscoveryHandle.cpp',
'BluetoothHidManager.cpp',
@ -93,6 +94,7 @@ EXPORTS.mozilla.dom.bluetooth.ipc += [
EXPORTS.mozilla.dom.bluetooth += [
'BluetoothAdapter.h',
'BluetoothClassOfDevice.h',
'BluetoothCommon.h',
'BluetoothDevice.h',
'BluetoothDiscoveryHandle.h',

View File

@ -0,0 +1,42 @@
/* -*- 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/. */
[CheckPermissions="bluetooth"]
interface BluetoothClassOfDevice
{
/**
* The following constants are defined in Assigned Numbers of bluetooth
* General Access Profile (GAP) spec. For more information see
* https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband
*/
// Major service class
const unsigned short LIMITED_DISCOVERABILITY = 0x0001;
const unsigned short POSITIONING = 0x0008;
const unsigned short NETWORKING = 0x0010;
const unsigned short RENDERING = 0x0020;
const unsigned short CAPTURING = 0x0040;
const unsigned short OBJECT_TRANSFER = 0x0080;
const unsigned short AUDIO = 0x0100;
const unsigned short TELEPHONY = 0x0200;
const unsigned short INFORMATION = 0x0400;
// Major device class
const octet MISC = 0x00;
const octet COMPUTER = 0x01;
const octet PHONE = 0x02;
const octet NETWORK = 0x03;
const octet AUDIO_VIDEO = 0x04;
const octet PERIPHERAL = 0x05;
const octet IMAGING = 0x06;
const octet WEARABLE = 0x07;
const octet TOY = 0x08;
const octet HEALTH = 0x09;
const octet UNCATEGORIZED = 0x1F;
readonly attribute unsigned short majorServiceClass;
readonly attribute octet majorDeviceClass;
readonly attribute octet minorDeviceClass;
};

View File

@ -567,6 +567,7 @@ if CONFIG['MOZ_B2G_BT']:
if CONFIG['MOZ_B2G_BT_API_V2']:
WEBIDL_FILES += [
'BluetoothAdapter2.webidl',
'BluetoothClassOfDevice.webidl',
'BluetoothDevice2.webidl',
'BluetoothDiscoveryHandle.webidl',
'BluetoothManager2.webidl',