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_bluetootheventservice_h__
8 #define mozilla_dom_bluetooth_bluetootheventservice_h__
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"
22 class UnixSocketConsumer
;
26 BEGIN_BLUETOOTH_NAMESPACE
28 class BluetoothManager
;
29 class BluetoothNamedValue
;
30 class BluetoothReplyRunnable
;
31 class BluetoothSignal
;
33 typedef mozilla::ObserverList
<BluetoothSignal
> BluetoothSignalObserverList
;
35 class BluetoothService
: public nsIObserver
36 , public BluetoothSignalObserver
39 friend class ToggleBtTask
;
42 friend class StartupTask
;
45 class ToggleBtAck
: public nsRunnable
48 ToggleBtAck(bool aEnabled
);
54 friend class ToggleBtAck
;
60 * Add a message handler object from message distribution observer.
61 * Must be called from the main thread.
63 * @param aNodeName Node name of the object
64 * @param aMsgHandler Weak pointer to the object
67 RegisterBluetoothSignalHandler(const nsAString
& aNodeName
,
68 BluetoothSignalObserver
* aMsgHandler
);
71 * Remove a message handler object from message distribution observer.
72 * Must be called from the main thread.
74 * @param aNodeName Node name of the object
75 * @param aMsgHandler Weak pointer to the object
78 UnregisterBluetoothSignalHandler(const nsAString
& aNodeName
,
79 BluetoothSignalObserver
* aMsgHandler
);
82 * Remove a message handlers for the given observer.
83 * Must be called from the main thread.
85 * @param aMsgHandler Weak pointer to the object
88 UnregisterAllSignalHandlers(BluetoothSignalObserver
* aMsgHandler
);
91 * Distribute a signal to the observer list
93 * @param aSignal Signal object to distribute
95 * @return NS_OK if signal distributed, NS_ERROR_FAILURE on error
98 DistributeSignal(const BluetoothSignal
& aEvent
);
101 * Called when get a Bluetooth Signal from BluetoothDBusService
105 Notify(const BluetoothSignal
& aParam
);
108 * Returns the BluetoothService singleton. Only to be called from main thread.
110 * @param aService Pointer to return singleton into.
112 * @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise (if service
113 * has not yet been started, for instance)
115 static BluetoothService
*
118 static already_AddRefed
<BluetoothService
>
121 nsRefPtr
<BluetoothService
> service
= Get();
122 return service
.forget();
126 * Returns the path of the default adapter, implemented via a platform
129 * @return NS_OK on success, NS_ERROR_FAILURE otherwise
132 GetDefaultAdapterPathInternal(BluetoothReplyRunnable
* aRunnable
) = 0;
135 * Returns the properties of paired devices, implemented via a platform
138 * @return NS_OK on success, NS_ERROR_FAILURE otherwise
141 GetPairedDevicePropertiesInternal(const nsTArray
<nsString
>& aDeviceAddresses
,
142 BluetoothReplyRunnable
* aRunnable
) = 0;
145 * Returns the properties of connected devices regarding to specific profile,
146 * implemented via a platform specific methood.
148 * @return NS_OK on success, NS_ERROR_FAILURE otherwise
151 GetConnectedDevicePropertiesInternal(uint16_t aServiceUuid
,
152 BluetoothReplyRunnable
* aRunnable
) = 0;
155 * Stop device discovery (platform specific implementation)
157 * @return NS_OK if discovery stopped correctly, false otherwise
160 StopDiscoveryInternal(BluetoothReplyRunnable
* aRunnable
) = 0;
163 * Start device discovery (platform specific implementation)
165 * @return NS_OK if discovery stopped correctly, false otherwise
168 StartDiscoveryInternal(BluetoothReplyRunnable
* aRunnable
) = 0;
171 * Set a property for the specified object
173 * @param aPropName Name of the property
174 * @param aValue Boolean value
175 * @param aRunnable Runnable to run on async reply
177 * @return NS_OK if property is set correctly, NS_ERROR_FAILURE otherwise
180 SetProperty(BluetoothObjectType aType
,
181 const BluetoothNamedValue
& aValue
,
182 BluetoothReplyRunnable
* aRunnable
) = 0;
185 CreatePairedDeviceInternal(const nsAString
& aAddress
,
187 BluetoothReplyRunnable
* aRunnable
) = 0;
190 RemoveDeviceInternal(const nsAString
& aObjectPath
,
191 BluetoothReplyRunnable
* aRunnable
) = 0;
194 * Get corresponding service channel of specific service on remote device.
195 * It's usually the very first step of establishing an outbound connection.
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()
201 * @return NS_OK if the task begins, NS_ERROR_FAILURE otherwise
204 GetServiceChannel(const nsAString
& aDeviceAddress
,
205 const nsAString
& aServiceUuid
,
206 BluetoothProfileManagerBase
* aManager
) = 0;
209 UpdateSdpRecords(const nsAString
& aDeviceAddress
,
210 BluetoothProfileManagerBase
* aManager
) = 0;
213 SetPinCodeInternal(const nsAString
& aDeviceAddress
, const nsAString
& aPinCode
,
214 BluetoothReplyRunnable
* aRunnable
) = 0;
217 SetPasskeyInternal(const nsAString
& aDeviceAddress
, uint32_t aPasskey
,
218 BluetoothReplyRunnable
* aRunnable
) = 0;
221 SetPairingConfirmationInternal(const nsAString
& aDeviceAddress
, bool aConfirm
,
222 BluetoothReplyRunnable
* aRunnable
) = 0;
225 Connect(const nsAString
& aDeviceAddress
, uint32_t aCod
, uint16_t aServiceUuid
,
226 BluetoothReplyRunnable
* aRunnable
) = 0;
229 Disconnect(const nsAString
& aDeviceAddress
, uint16_t aServiceUuid
,
230 BluetoothReplyRunnable
* aRunnable
) = 0;
233 IsConnected(const uint16_t aServiceUuid
, BluetoothReplyRunnable
* aRunnable
) = 0;
236 SendFile(const nsAString
& aDeviceAddress
,
237 BlobParent
* aBlobParent
,
238 BlobChild
* aBlobChild
,
239 BluetoothReplyRunnable
* aRunnable
) = 0;
242 SendFile(const nsAString
& aDeviceAddress
,
244 BluetoothReplyRunnable
* aRunnable
) = 0;
247 StopSendingFile(const nsAString
& aDeviceAddress
,
248 BluetoothReplyRunnable
* aRunnable
) = 0;
251 ConfirmReceivingFile(const nsAString
& aDeviceAddress
, bool aConfirm
,
252 BluetoothReplyRunnable
* aRunnable
) = 0;
255 ConnectSco(BluetoothReplyRunnable
* aRunnable
) = 0;
258 DisconnectSco(BluetoothReplyRunnable
* aRunnable
) = 0;
261 IsScoConnected(BluetoothReplyRunnable
* aRunnable
) = 0;
265 AnswerWaitingCall(BluetoothReplyRunnable
* aRunnable
) = 0;
268 IgnoreWaitingCall(BluetoothReplyRunnable
* aRunnable
) = 0;
271 ToggleCalls(BluetoothReplyRunnable
* aRunnable
) = 0;
275 SendMetaData(const nsAString
& aTitle
,
276 const nsAString
& aArtist
,
277 const nsAString
& aAlbum
,
278 int64_t aMediaNumber
,
279 int64_t aTotalMediaCount
,
281 BluetoothReplyRunnable
* aRunnable
) = 0;
284 SendPlayStatus(int64_t aDuration
,
286 const nsAString
& aPlayStatus
,
287 BluetoothReplyRunnable
* aRunnable
) = 0;
290 UpdatePlayStatus(uint32_t aDuration
,
292 ControlPlayStatus aPlayStatus
) = 0;
295 SendSinkMessage(const nsAString
& aDeviceAddresses
,
296 const nsAString
& aMessage
) = 0;
299 SendInputMessage(const nsAString
& aDeviceAddresses
,
300 const nsAString
& aMessage
) = 0;
312 * Below 2 function/variable are used for ensuring event 'AdapterAdded' will
313 * be fired after event 'Enabled'.
315 void TryFiringAdapterAdded();
316 void AdapterAddedReceived();
319 BluetoothService() : mEnabled(false)
320 , mAdapterAddedReceived(false)
324 virtual ~BluetoothService();
333 StartBluetooth(bool aIsStartup
);
336 StopBluetooth(bool aIsStartup
);
339 StartStopBluetooth(bool aStart
, bool aIsStartup
);
342 * Platform specific startup functions go here. Usually deals with member
343 * variables, so not static. Guaranteed to be called outside of main thread.
345 * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise
351 * Platform specific startup functions go here. Usually deals with member
352 * variables, so not static. Guaranteed to be called outside of main thread.
354 * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise
360 * Called when XPCOM first creates this service.
366 * Called when the startup settings check has completed.
369 HandleStartupSettingsCheck(bool aEnable
);
372 * Called when "mozsettings-changed" observer topic fires.
375 HandleSettingsChanged(const nsAString
& aData
);
378 * Called when XPCOM is shutting down.
383 // Called by ToggleBtAck.
385 SetEnabled(bool aEnabled
);
388 static BluetoothService
*
391 typedef nsClassHashtable
<nsStringHashKey
, BluetoothSignalObserverList
>
392 BluetoothSignalObserverTable
;
394 BluetoothSignalObserverTable mBluetoothSignalObserverTable
;
399 bool mAdapterAddedReceived
;
402 END_BLUETOOTH_NAMESPACE