|
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_bluetootheventservice_h__ |
|
8 #define mozilla_dom_bluetooth_bluetootheventservice_h__ |
|
9 |
|
10 #include "BluetoothCommon.h" |
|
11 #include "BluetoothProfileManagerBase.h" |
|
12 #include "mozilla/dom/ipc/Blob.h" |
|
13 #include "nsAutoPtr.h" |
|
14 #include "nsClassHashtable.h" |
|
15 #include "nsIDOMFile.h" |
|
16 #include "nsIObserver.h" |
|
17 #include "nsTObserverArray.h" |
|
18 #include "nsThreadUtils.h" |
|
19 |
|
20 namespace mozilla { |
|
21 namespace ipc { |
|
22 class UnixSocketConsumer; |
|
23 } |
|
24 } |
|
25 |
|
26 BEGIN_BLUETOOTH_NAMESPACE |
|
27 |
|
28 class BluetoothManager; |
|
29 class BluetoothNamedValue; |
|
30 class BluetoothReplyRunnable; |
|
31 class BluetoothSignal; |
|
32 |
|
33 typedef mozilla::ObserverList<BluetoothSignal> BluetoothSignalObserverList; |
|
34 |
|
35 class BluetoothService : public nsIObserver |
|
36 , public BluetoothSignalObserver |
|
37 { |
|
38 class ToggleBtTask; |
|
39 friend class ToggleBtTask; |
|
40 |
|
41 class StartupTask; |
|
42 friend class StartupTask; |
|
43 |
|
44 public: |
|
45 class ToggleBtAck : public nsRunnable |
|
46 { |
|
47 public: |
|
48 ToggleBtAck(bool aEnabled); |
|
49 NS_IMETHOD Run(); |
|
50 |
|
51 private: |
|
52 bool mEnabled; |
|
53 }; |
|
54 friend class ToggleBtAck; |
|
55 |
|
56 NS_DECL_ISUPPORTS |
|
57 NS_DECL_NSIOBSERVER |
|
58 |
|
59 /** |
|
60 * Add a message handler object from message distribution observer. |
|
61 * Must be called from the main thread. |
|
62 * |
|
63 * @param aNodeName Node name of the object |
|
64 * @param aMsgHandler Weak pointer to the object |
|
65 */ |
|
66 virtual void |
|
67 RegisterBluetoothSignalHandler(const nsAString& aNodeName, |
|
68 BluetoothSignalObserver* aMsgHandler); |
|
69 |
|
70 /** |
|
71 * Remove a message handler object from message distribution observer. |
|
72 * Must be called from the main thread. |
|
73 * |
|
74 * @param aNodeName Node name of the object |
|
75 * @param aMsgHandler Weak pointer to the object |
|
76 */ |
|
77 virtual void |
|
78 UnregisterBluetoothSignalHandler(const nsAString& aNodeName, |
|
79 BluetoothSignalObserver* aMsgHandler); |
|
80 |
|
81 /** |
|
82 * Remove a message handlers for the given observer. |
|
83 * Must be called from the main thread. |
|
84 * |
|
85 * @param aMsgHandler Weak pointer to the object |
|
86 */ |
|
87 void |
|
88 UnregisterAllSignalHandlers(BluetoothSignalObserver* aMsgHandler); |
|
89 |
|
90 /** |
|
91 * Distribute a signal to the observer list |
|
92 * |
|
93 * @param aSignal Signal object to distribute |
|
94 * |
|
95 * @return NS_OK if signal distributed, NS_ERROR_FAILURE on error |
|
96 */ |
|
97 void |
|
98 DistributeSignal(const BluetoothSignal& aEvent); |
|
99 |
|
100 /** |
|
101 * Called when get a Bluetooth Signal from BluetoothDBusService |
|
102 * |
|
103 */ |
|
104 void |
|
105 Notify(const BluetoothSignal& aParam); |
|
106 |
|
107 /** |
|
108 * Returns the BluetoothService singleton. Only to be called from main thread. |
|
109 * |
|
110 * @param aService Pointer to return singleton into. |
|
111 * |
|
112 * @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise (if service |
|
113 * has not yet been started, for instance) |
|
114 */ |
|
115 static BluetoothService* |
|
116 Get(); |
|
117 |
|
118 static already_AddRefed<BluetoothService> |
|
119 FactoryCreate() |
|
120 { |
|
121 nsRefPtr<BluetoothService> service = Get(); |
|
122 return service.forget(); |
|
123 } |
|
124 |
|
125 /** |
|
126 * Returns the path of the default adapter, implemented via a platform |
|
127 * specific method. |
|
128 * |
|
129 * @return NS_OK on success, NS_ERROR_FAILURE otherwise |
|
130 */ |
|
131 virtual nsresult |
|
132 GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable) = 0; |
|
133 |
|
134 /** |
|
135 * Returns the properties of paired devices, implemented via a platform |
|
136 * specific method. |
|
137 * |
|
138 * @return NS_OK on success, NS_ERROR_FAILURE otherwise |
|
139 */ |
|
140 virtual nsresult |
|
141 GetPairedDevicePropertiesInternal(const nsTArray<nsString>& aDeviceAddresses, |
|
142 BluetoothReplyRunnable* aRunnable) = 0; |
|
143 |
|
144 /** |
|
145 * Returns the properties of connected devices regarding to specific profile, |
|
146 * implemented via a platform specific methood. |
|
147 * |
|
148 * @return NS_OK on success, NS_ERROR_FAILURE otherwise |
|
149 */ |
|
150 virtual nsresult |
|
151 GetConnectedDevicePropertiesInternal(uint16_t aServiceUuid, |
|
152 BluetoothReplyRunnable* aRunnable) = 0; |
|
153 |
|
154 /** |
|
155 * Stop device discovery (platform specific implementation) |
|
156 * |
|
157 * @return NS_OK if discovery stopped correctly, false otherwise |
|
158 */ |
|
159 virtual nsresult |
|
160 StopDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0; |
|
161 |
|
162 /** |
|
163 * Start device discovery (platform specific implementation) |
|
164 * |
|
165 * @return NS_OK if discovery stopped correctly, false otherwise |
|
166 */ |
|
167 virtual nsresult |
|
168 StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0; |
|
169 |
|
170 /** |
|
171 * Set a property for the specified object |
|
172 * |
|
173 * @param aPropName Name of the property |
|
174 * @param aValue Boolean value |
|
175 * @param aRunnable Runnable to run on async reply |
|
176 * |
|
177 * @return NS_OK if property is set correctly, NS_ERROR_FAILURE otherwise |
|
178 */ |
|
179 virtual nsresult |
|
180 SetProperty(BluetoothObjectType aType, |
|
181 const BluetoothNamedValue& aValue, |
|
182 BluetoothReplyRunnable* aRunnable) = 0; |
|
183 |
|
184 virtual nsresult |
|
185 CreatePairedDeviceInternal(const nsAString& aAddress, |
|
186 int aTimeout, |
|
187 BluetoothReplyRunnable* aRunnable) = 0; |
|
188 |
|
189 virtual nsresult |
|
190 RemoveDeviceInternal(const nsAString& aObjectPath, |
|
191 BluetoothReplyRunnable* aRunnable) = 0; |
|
192 |
|
193 /** |
|
194 * Get corresponding service channel of specific service on remote device. |
|
195 * It's usually the very first step of establishing an outbound connection. |
|
196 * |
|
197 * @param aObjectPath Object path of remote device |
|
198 * @param aServiceUuid UUID of the target service |
|
199 * @param aManager Instance which has callback function OnGetServiceChannel() |
|
200 * |
|
201 * @return NS_OK if the task begins, NS_ERROR_FAILURE otherwise |
|
202 */ |
|
203 virtual nsresult |
|
204 GetServiceChannel(const nsAString& aDeviceAddress, |
|
205 const nsAString& aServiceUuid, |
|
206 BluetoothProfileManagerBase* aManager) = 0; |
|
207 |
|
208 virtual bool |
|
209 UpdateSdpRecords(const nsAString& aDeviceAddress, |
|
210 BluetoothProfileManagerBase* aManager) = 0; |
|
211 |
|
212 virtual bool |
|
213 SetPinCodeInternal(const nsAString& aDeviceAddress, const nsAString& aPinCode, |
|
214 BluetoothReplyRunnable* aRunnable) = 0; |
|
215 |
|
216 virtual bool |
|
217 SetPasskeyInternal(const nsAString& aDeviceAddress, uint32_t aPasskey, |
|
218 BluetoothReplyRunnable* aRunnable) = 0; |
|
219 |
|
220 virtual bool |
|
221 SetPairingConfirmationInternal(const nsAString& aDeviceAddress, bool aConfirm, |
|
222 BluetoothReplyRunnable* aRunnable) = 0; |
|
223 |
|
224 virtual void |
|
225 Connect(const nsAString& aDeviceAddress, uint32_t aCod, uint16_t aServiceUuid, |
|
226 BluetoothReplyRunnable* aRunnable) = 0; |
|
227 |
|
228 virtual void |
|
229 Disconnect(const nsAString& aDeviceAddress, uint16_t aServiceUuid, |
|
230 BluetoothReplyRunnable* aRunnable) = 0; |
|
231 |
|
232 virtual bool |
|
233 IsConnected(uint16_t aServiceUuid) = 0; |
|
234 |
|
235 virtual void |
|
236 SendFile(const nsAString& aDeviceAddress, |
|
237 BlobParent* aBlobParent, |
|
238 BlobChild* aBlobChild, |
|
239 BluetoothReplyRunnable* aRunnable) = 0; |
|
240 |
|
241 virtual void |
|
242 SendFile(const nsAString& aDeviceAddress, |
|
243 nsIDOMBlob* aBlob, |
|
244 BluetoothReplyRunnable* aRunnable) = 0; |
|
245 |
|
246 virtual void |
|
247 StopSendingFile(const nsAString& aDeviceAddress, |
|
248 BluetoothReplyRunnable* aRunnable) = 0; |
|
249 |
|
250 virtual void |
|
251 ConfirmReceivingFile(const nsAString& aDeviceAddress, bool aConfirm, |
|
252 BluetoothReplyRunnable* aRunnable) = 0; |
|
253 |
|
254 virtual void |
|
255 ConnectSco(BluetoothReplyRunnable* aRunnable) = 0; |
|
256 |
|
257 virtual void |
|
258 DisconnectSco(BluetoothReplyRunnable* aRunnable) = 0; |
|
259 |
|
260 virtual void |
|
261 IsScoConnected(BluetoothReplyRunnable* aRunnable) = 0; |
|
262 |
|
263 #ifdef MOZ_B2G_RIL |
|
264 virtual void |
|
265 AnswerWaitingCall(BluetoothReplyRunnable* aRunnable) = 0; |
|
266 |
|
267 virtual void |
|
268 IgnoreWaitingCall(BluetoothReplyRunnable* aRunnable) = 0; |
|
269 |
|
270 virtual void |
|
271 ToggleCalls(BluetoothReplyRunnable* aRunnable) = 0; |
|
272 #endif |
|
273 |
|
274 virtual void |
|
275 SendMetaData(const nsAString& aTitle, |
|
276 const nsAString& aArtist, |
|
277 const nsAString& aAlbum, |
|
278 int64_t aMediaNumber, |
|
279 int64_t aTotalMediaCount, |
|
280 int64_t aDuration, |
|
281 BluetoothReplyRunnable* aRunnable) = 0; |
|
282 |
|
283 virtual void |
|
284 SendPlayStatus(int64_t aDuration, |
|
285 int64_t aPosition, |
|
286 const nsAString& aPlayStatus, |
|
287 BluetoothReplyRunnable* aRunnable) = 0; |
|
288 |
|
289 virtual void |
|
290 UpdatePlayStatus(uint32_t aDuration, |
|
291 uint32_t aPosition, |
|
292 ControlPlayStatus aPlayStatus) = 0; |
|
293 |
|
294 virtual nsresult |
|
295 SendSinkMessage(const nsAString& aDeviceAddresses, |
|
296 const nsAString& aMessage) = 0; |
|
297 |
|
298 virtual nsresult |
|
299 SendInputMessage(const nsAString& aDeviceAddresses, |
|
300 const nsAString& aMessage) = 0; |
|
301 |
|
302 bool |
|
303 IsEnabled() const |
|
304 { |
|
305 return mEnabled; |
|
306 } |
|
307 |
|
308 bool |
|
309 IsToggling() const; |
|
310 |
|
311 void |
|
312 RemoveObserverFromTable(const nsAString& key); |
|
313 |
|
314 /** |
|
315 * Below 2 function/variable are used for ensuring event 'AdapterAdded' will |
|
316 * be fired after event 'Enabled'. |
|
317 */ |
|
318 void TryFiringAdapterAdded(); |
|
319 void AdapterAddedReceived(); |
|
320 |
|
321 protected: |
|
322 BluetoothService() : mEnabled(false) |
|
323 , mAdapterAddedReceived(false) |
|
324 { |
|
325 } |
|
326 |
|
327 virtual ~BluetoothService(); |
|
328 |
|
329 bool |
|
330 Init(); |
|
331 |
|
332 void |
|
333 Cleanup(); |
|
334 |
|
335 nsresult |
|
336 StartBluetooth(bool aIsStartup); |
|
337 |
|
338 nsresult |
|
339 StopBluetooth(bool aIsStartup); |
|
340 |
|
341 nsresult |
|
342 StartStopBluetooth(bool aStart, bool aIsStartup); |
|
343 |
|
344 /** |
|
345 * Platform specific startup functions go here. Usually deals with member |
|
346 * variables, so not static. Guaranteed to be called outside of main thread. |
|
347 * |
|
348 * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise |
|
349 */ |
|
350 virtual nsresult |
|
351 StartInternal() = 0; |
|
352 |
|
353 /** |
|
354 * Platform specific startup functions go here. Usually deals with member |
|
355 * variables, so not static. Guaranteed to be called outside of main thread. |
|
356 * |
|
357 * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise |
|
358 */ |
|
359 virtual nsresult |
|
360 StopInternal() = 0; |
|
361 |
|
362 /** |
|
363 * Called when XPCOM first creates this service. |
|
364 */ |
|
365 virtual nsresult |
|
366 HandleStartup(); |
|
367 |
|
368 /** |
|
369 * Called when the startup settings check has completed. |
|
370 */ |
|
371 nsresult |
|
372 HandleStartupSettingsCheck(bool aEnable); |
|
373 |
|
374 /** |
|
375 * Called when "mozsettings-changed" observer topic fires. |
|
376 */ |
|
377 nsresult |
|
378 HandleSettingsChanged(const nsAString& aData); |
|
379 |
|
380 /** |
|
381 * Called when XPCOM is shutting down. |
|
382 */ |
|
383 virtual nsresult |
|
384 HandleShutdown(); |
|
385 |
|
386 // Called by ToggleBtAck. |
|
387 void |
|
388 SetEnabled(bool aEnabled); |
|
389 |
|
390 // Called by Get(). |
|
391 static BluetoothService* |
|
392 Create(); |
|
393 |
|
394 typedef nsClassHashtable<nsStringHashKey, BluetoothSignalObserverList > |
|
395 BluetoothSignalObserverTable; |
|
396 |
|
397 BluetoothSignalObserverTable mBluetoothSignalObserverTable; |
|
398 |
|
399 bool mEnabled; |
|
400 |
|
401 private: |
|
402 bool mAdapterAddedReceived; |
|
403 }; |
|
404 |
|
405 END_BLUETOOTH_NAMESPACE |
|
406 |
|
407 #endif |