Bumping manifests a=b2g-bump
[gecko.git] / dom / bluetooth / BluetoothProfileController.h
blob42d8631f1a5a0b24236c18b2a979051f1ceb13fb
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_bluetooth_bluetoothprofilecontroller_h__
8 #define mozilla_dom_bluetooth_bluetoothprofilecontroller_h__
10 #include "BluetoothUuid.h"
11 #include "nsISupportsImpl.h"
12 #include "nsAutoPtr.h"
13 #include "nsITimer.h"
15 BEGIN_BLUETOOTH_NAMESPACE
18 * Class of Device(CoD): 32-bit unsigned integer
20 * 31 24 23 13 12 8 7 2 1 0
21 * | | Major | Major | Minor | |
22 * | | service | device | device | |
23 * | | class | class | class | |
24 * | |<- 11 ->|<- 5 ->|<- 6 ->| |
26 * https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband
29 // Bit 23 ~ Bit 13: Major service class
30 #define GET_MAJOR_SERVICE_CLASS(cod) ((cod & 0xffe000) >> 13)
32 // Bit 12 ~ Bit 8: Major device class
33 #define GET_MAJOR_DEVICE_CLASS(cod) ((cod & 0x1f00) >> 8)
35 // Bit 7 ~ Bit 2: Minor device class
36 #define GET_MINOR_DEVICE_CLASS(cod) ((cod & 0xfc) >> 2)
38 // Audio: Major service class = 0x100 (Bit 21 is set)
39 #define HAS_AUDIO(cod) (cod & 0x200000)
41 // Rendering: Major service class = 0x20 (Bit 18 is set)
42 #define HAS_RENDERING(cod) (cod & 0x40000)
44 // Peripheral: Major device class = 0x5
45 #define IS_PERIPHERAL(cod) (GET_MAJOR_DEVICE_CLASS(cod) == 0x5)
47 // Remote Control: sub-field of minor device class, Bit 5 ~ Bit 2 = 0x3
48 #define IS_REMOTE_CONTROL(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0xf) == 0x3)
50 // Keyboard: sub-field of minor device class (Bit 6)
51 #define IS_KEYBOARD(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x10) >> 4)
53 // Pointing device: sub-field of minor device class (Bit 7)
54 #define IS_POINTING_DEVICE(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x20) >> 5)
56 /**
57 * Check whether the value of CoD is invalid. (i.e. Bit 31 ~ Bit 24 != 0x0)
59 * According to Bluetooth core spec v4.1. Vol 2, Sec. 7.3, the data length of
60 * CoD (class of device) is 3 bytes. The two least significant bits are used to
61 * indicate 'format type'. The following 22 bits are used to indicate category
62 * of service class and device type. The remaining 8 bits (Bit 31 ~ Bit 24)
63 * should be unassigned bits, since BlueDroid uses uint32_t to store CoD.
65 #define IS_INVALID_COD(cod) (cod >> 24)
67 class BluetoothProfileManagerBase;
68 class BluetoothReplyRunnable;
69 typedef void (*BluetoothProfileControllerCallback)();
71 class BluetoothProfileController MOZ_FINAL
73 ~BluetoothProfileController();
75 public:
76 NS_INLINE_DECL_REFCOUNTING(BluetoothProfileController)
77 /**
78 * @param aConnect: If it's a connect request, the value should be set
79 * to true. For disconnect request, set it to false.
80 * @param aDeviceAddress: The address of remote device.
81 * @param aRunnable: Once the controller has done, the runnable will be
82 * replied. When all connection/disconnection attemps
83 * have failed, an error is fired. In other words,
84 * reply a success if any attemp successes.
85 * @param aCallback: The callback will be invoked after the runnable is
86 * replied.
87 * @param aServiceUuid: Connect/Disconnect to the specified profile. Please
88 * see enum BluetoothServiceClass for valid value.
89 * @param aCod: If aServiceUuid is not assigned, i.e. the value is
90 * 0, the controller connect multiple profiles based on
91 * aCod or disconnect all connected profiles.
93 BluetoothProfileController(bool aConnect,
94 const nsAString& aDeviceAddress,
95 BluetoothReplyRunnable* aRunnable,
96 BluetoothProfileControllerCallback aCallback,
97 uint16_t aServiceUuid,
98 uint32_t aCod = 0);
101 * The controller starts connecting/disconnecting profiles one by one
102 * according to the order in array mProfiles.
104 void StartSession();
107 * The original DOM request would be fired in this function.
109 void EndSession();
112 * It would be invoked after connect/disconnect operation is completed.
113 * An error string would be returned when it fails.
115 void NotifyCompletion(const nsAString& aErrorStr);
118 * It is invoked after a profile has reached timeout, reset mProfiles.
120 void GiveupAndContinue();
122 private:
123 // Setup data member mProfiles
124 void SetupProfiles(bool aAssignServiceClass);
126 // Add profiles into array with/without checking connection status
127 void AddProfile(BluetoothProfileManagerBase* aProfile,
128 bool aCheckConnected = false);
130 // Add specified profile into array
131 void AddProfileWithServiceClass(BluetoothServiceClass aClass);
133 // Connect/Disconnect next profile in the array
134 void Next();
136 // Is Bluetooth service available for profile connection/disconnection ?
137 bool IsBtServiceAvailable() const;
139 const bool mConnect;
140 nsString mDeviceAddress;
141 nsRefPtr<BluetoothReplyRunnable> mRunnable;
142 BluetoothProfileControllerCallback mCallback;
144 bool mCurrentProfileFinished;
145 bool mSuccess;
146 int8_t mProfilesIndex;
147 nsTArray<BluetoothProfileManagerBase*> mProfiles;
149 // Either CoD or BluetoothServiceClass is assigned.
150 union {
151 uint32_t cod;
152 BluetoothServiceClass service;
153 } mTarget;
155 nsCOMPtr<nsITimer> mTimer;
158 END_BLUETOOTH_NAMESPACE
160 #endif