|
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/. */ |
|
6 |
|
7 #ifndef mozilla_dom_bluetooth_bluetoothprofilecontroller_h__ |
|
8 #define mozilla_dom_bluetooth_bluetoothprofilecontroller_h__ |
|
9 |
|
10 #include "BluetoothUuid.h" |
|
11 #include "nsISupportsImpl.h" |
|
12 #include "nsAutoPtr.h" |
|
13 #include "nsITimer.h" |
|
14 |
|
15 BEGIN_BLUETOOTH_NAMESPACE |
|
16 |
|
17 /* |
|
18 * Class of Device(CoD): 32-bit unsigned integer |
|
19 * |
|
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 ->| | |
|
25 * |
|
26 * https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband |
|
27 */ |
|
28 |
|
29 // Bit 23 ~ Bit 13: Major service class |
|
30 #define GET_MAJOR_SERVICE_CLASS(cod) ((cod & 0xffe000) >> 13) |
|
31 |
|
32 // Bit 12 ~ Bit 8: Major device class |
|
33 #define GET_MAJOR_DEVICE_CLASS(cod) ((cod & 0x1f00) >> 8) |
|
34 |
|
35 // Bit 7 ~ Bit 2: Minor device class |
|
36 #define GET_MINOR_DEVICE_CLASS(cod) ((cod & 0xfc) >> 2) |
|
37 |
|
38 // Audio: Major service class = 0x100 (Bit 21 is set) |
|
39 #define HAS_AUDIO(cod) (cod & 0x200000) |
|
40 |
|
41 // Rendering: Major service class = 0x20 (Bit 18 is set) |
|
42 #define HAS_RENDERING(cod) (cod & 0x40000) |
|
43 |
|
44 // Peripheral: Major device class = 0x5 |
|
45 #define IS_PERIPHERAL(cod) (GET_MAJOR_DEVICE_CLASS(cod) == 0x5) |
|
46 |
|
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) |
|
49 |
|
50 // Keyboard: sub-field of minor device class (Bit 6) |
|
51 #define IS_KEYBOARD(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x10) >> 4) |
|
52 |
|
53 // Pointing device: sub-field of minor device class (Bit 7) |
|
54 #define IS_POINTING_DEVICE(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x20) >> 5) |
|
55 |
|
56 class BluetoothProfileManagerBase; |
|
57 class BluetoothReplyRunnable; |
|
58 typedef void (*BluetoothProfileControllerCallback)(); |
|
59 |
|
60 class BluetoothProfileController MOZ_FINAL |
|
61 { |
|
62 ~BluetoothProfileController(); |
|
63 |
|
64 public: |
|
65 NS_INLINE_DECL_REFCOUNTING(BluetoothProfileController) |
|
66 /** |
|
67 * @param aConnect: If it's a connect request, the value should be set |
|
68 * to true. For disconnect request, set it to false. |
|
69 * @param aDeviceAddress: The address of remote device. |
|
70 * @param aRunnable: Once the controller has done, the runnable will be |
|
71 * replied. When all connection/disconnection attemps |
|
72 * have failed, an error is fired. In other words, |
|
73 * reply a success if any attemp successes. |
|
74 * @param aCallback: The callback will be invoked after the runnable is |
|
75 * replied. |
|
76 * @param aServiceUuid: Connect/Disconnect to the specified profile. Please |
|
77 * see enum BluetoothServiceClass for valid value. |
|
78 * @param aCod: If aServiceUuid is not assigned, i.e. the value is |
|
79 * 0, the controller connect multiple profiles based on |
|
80 * aCod or disconnect all connected profiles. |
|
81 */ |
|
82 BluetoothProfileController(bool aConnect, |
|
83 const nsAString& aDeviceAddress, |
|
84 BluetoothReplyRunnable* aRunnable, |
|
85 BluetoothProfileControllerCallback aCallback, |
|
86 uint16_t aServiceUuid, |
|
87 uint32_t aCod = 0); |
|
88 |
|
89 /** |
|
90 * The controller starts connecting/disconnecting profiles one by one |
|
91 * according to the order in array mProfiles. |
|
92 */ |
|
93 void StartSession(); |
|
94 |
|
95 /** |
|
96 * The original DOM request would be fired in this function. |
|
97 */ |
|
98 void EndSession(); |
|
99 |
|
100 /** |
|
101 * It would be invoked after connect/disconnect operation is completed. |
|
102 * An error string would be returned when it fails. |
|
103 */ |
|
104 void NotifyCompletion(const nsAString& aErrorStr); |
|
105 |
|
106 /** |
|
107 * It is invoked after a profile has reached timeout, reset mProfiles. |
|
108 */ |
|
109 void GiveupAndContinue(); |
|
110 |
|
111 private: |
|
112 // Setup data member mProfiles |
|
113 void SetupProfiles(bool aAssignServiceClass); |
|
114 |
|
115 // Add profiles into array with/without checking connection status |
|
116 void AddProfile(BluetoothProfileManagerBase* aProfile, |
|
117 bool aCheckConnected = false); |
|
118 |
|
119 // Add specified profile into array |
|
120 void AddProfileWithServiceClass(BluetoothServiceClass aClass); |
|
121 |
|
122 // Connect/Disconnect next profile in the array |
|
123 void Next(); |
|
124 |
|
125 const bool mConnect; |
|
126 nsString mDeviceAddress; |
|
127 nsRefPtr<BluetoothReplyRunnable> mRunnable; |
|
128 BluetoothProfileControllerCallback mCallback; |
|
129 |
|
130 bool mCurrentProfileFinished; |
|
131 bool mSuccess; |
|
132 int8_t mProfilesIndex; |
|
133 nsTArray<BluetoothProfileManagerBase*> mProfiles; |
|
134 |
|
135 // Either CoD or BluetoothServiceClass is assigned. |
|
136 union { |
|
137 uint32_t cod; |
|
138 BluetoothServiceClass service; |
|
139 } mTarget; |
|
140 |
|
141 nsCOMPtr<nsITimer> mTimer; |
|
142 nsCOMPtr<nsITimerCallback> mCheckProfileStatusCallback; |
|
143 }; |
|
144 |
|
145 END_BLUETOOTH_NAMESPACE |
|
146 |
|
147 #endif |