Bug 1027504 - Implement BluetoothClassOfDevice, r=bz, r=echou
authorBen Tian <btian@mozilla.com>
Thu, 10 Jul 2014 14:42:10 +0800
changeset 193176 cff18929a775371a98e63e024c1de8e6c87aa5e7
parent 193175 2ddb73c3eae5ff6ebb8a06276bcecf6bbd16b714
child 193177 60596186aeddf6d3406103860eafb01e68881248
push id27111
push usercbook@mozilla.com
push dateThu, 10 Jul 2014 12:41:20 +0000
treeherdermozilla-central@41e1a83bd7f7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz, echou
bugs1027504
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1027504 - Implement BluetoothClassOfDevice, r=bz, r=echou
dom/bindings/Bindings.conf
dom/bluetooth2/BluetoothClassOfDevice.cpp
dom/bluetooth2/BluetoothClassOfDevice.h
dom/bluetooth2/BluetoothProfileController.h
dom/bluetooth2/moz.build
dom/webidl/BluetoothClassOfDevice.webidl
dom/webidl/moz.build
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -168,16 +168,20 @@ DOMInterfaces = {
 'BluetoothManager': {
     'nativeType': 'mozilla::dom::bluetooth::BluetoothManager',
 },
 
 'BluetoothDiscoveryHandle': {
     'nativeType': 'mozilla::dom::bluetooth::BluetoothDiscoveryHandle',
 },
 
+'BluetoothClassOfDevice': {
+    'nativeType': 'mozilla::dom::bluetooth::BluetoothClassOfDevice',
+},
+
 'CameraCapabilities': {
     'nativeType': 'mozilla::dom::CameraCapabilities',
     'headerFile': 'DOMCameraCapabilities.h'
 },
 
 'CameraControl': {
     'nativeType': 'mozilla::nsDOMCameraControl',
     'headerFile': 'DOMCameraControl.h',
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth2/BluetoothClassOfDevice.cpp
@@ -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);
+}
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth2/BluetoothClassOfDevice.h
@@ -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
--- a/dom/bluetooth2/BluetoothProfileController.h
+++ b/dom/bluetooth2/BluetoothProfileController.h
@@ -22,29 +22,29 @@ BEGIN_BLUETOOTH_NAMESPACE
  * |       | 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)
+#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)
 
 // Remote Control: sub-field of minor device class, Bit 5 ~ Bit 2 = 0x3
 #define IS_REMOTE_CONTROL(cod)       ((GET_MINOR_DEVICE_CLASS(cod) & 0xf) == 0x3)
 
 // Keyboard: sub-field of minor device class (Bit 6)
--- a/dom/bluetooth2/moz.build
+++ b/dom/bluetooth2/moz.build
@@ -2,16 +2,17 @@
 # vim: set filetype=python:
 # 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/.
 
 if CONFIG['MOZ_B2G_BT']:
     SOURCES += [
         'BluetoothAdapter.cpp',
+        'BluetoothClassOfDevice.cpp',
         'BluetoothDevice.cpp',
         'BluetoothDiscoveryHandle.cpp',
         'BluetoothHidManager.cpp',
         'BluetoothManager.cpp',
         'BluetoothProfileController.cpp',
         'BluetoothReplyRunnable.cpp',
         'BluetoothService.cpp',
         'BluetoothUuid.cpp',
@@ -88,16 +89,17 @@ if CONFIG['MOZ_B2G_BT']:
     ]
 
 EXPORTS.mozilla.dom.bluetooth.ipc += [
     'ipc/BluetoothMessageUtils.h',
 ]
 
 EXPORTS.mozilla.dom.bluetooth += [
     'BluetoothAdapter.h',
+    'BluetoothClassOfDevice.h',
     'BluetoothCommon.h',
     'BluetoothDevice.h',
     'BluetoothDiscoveryHandle.h',
     'BluetoothManager.h',
 ]
 
 IPDL_SOURCES += [
     'ipc/BluetoothTypes.ipdlh',
new file mode 100644
--- /dev/null
+++ b/dom/webidl/BluetoothClassOfDevice.webidl
@@ -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;
+};
--- a/dom/webidl/moz.build
+++ b/dom/webidl/moz.build
@@ -562,16 +562,17 @@ WEBIDL_FILES += [
 # the safe side.
 if CONFIG['MOZ_DEBUG']:
     WEBIDL_FILES += ['TestInterfaceJS.webidl']
 
 if CONFIG['MOZ_B2G_BT']:
     if CONFIG['MOZ_B2G_BT_API_V2']:
         WEBIDL_FILES += [
             'BluetoothAdapter2.webidl',
+            'BluetoothClassOfDevice.webidl',
             'BluetoothDevice2.webidl',
             'BluetoothDiscoveryHandle.webidl',
             'BluetoothManager2.webidl',
         ]
     else:
         WEBIDL_FILES += [
             'BluetoothAdapter.webidl',
             'BluetoothDevice.webidl',