michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_bluetooth_bluetootheventservice_h__ michael@0: #define mozilla_dom_bluetooth_bluetootheventservice_h__ michael@0: michael@0: #include "BluetoothCommon.h" michael@0: #include "BluetoothProfileManagerBase.h" michael@0: #include "mozilla/dom/ipc/Blob.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsClassHashtable.h" michael@0: #include "nsIDOMFile.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsTObserverArray.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: class UnixSocketConsumer; michael@0: } michael@0: } michael@0: michael@0: BEGIN_BLUETOOTH_NAMESPACE michael@0: michael@0: class BluetoothManager; michael@0: class BluetoothNamedValue; michael@0: class BluetoothReplyRunnable; michael@0: class BluetoothSignal; michael@0: michael@0: typedef mozilla::ObserverList BluetoothSignalObserverList; michael@0: michael@0: class BluetoothService : public nsIObserver michael@0: , public BluetoothSignalObserver michael@0: { michael@0: class ToggleBtTask; michael@0: friend class ToggleBtTask; michael@0: michael@0: class StartupTask; michael@0: friend class StartupTask; michael@0: michael@0: public: michael@0: class ToggleBtAck : public nsRunnable michael@0: { michael@0: public: michael@0: ToggleBtAck(bool aEnabled); michael@0: NS_IMETHOD Run(); michael@0: michael@0: private: michael@0: bool mEnabled; michael@0: }; michael@0: friend class ToggleBtAck; michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: /** michael@0: * Add a message handler object from message distribution observer. michael@0: * Must be called from the main thread. michael@0: * michael@0: * @param aNodeName Node name of the object michael@0: * @param aMsgHandler Weak pointer to the object michael@0: */ michael@0: virtual void michael@0: RegisterBluetoothSignalHandler(const nsAString& aNodeName, michael@0: BluetoothSignalObserver* aMsgHandler); michael@0: michael@0: /** michael@0: * Remove a message handler object from message distribution observer. michael@0: * Must be called from the main thread. michael@0: * michael@0: * @param aNodeName Node name of the object michael@0: * @param aMsgHandler Weak pointer to the object michael@0: */ michael@0: virtual void michael@0: UnregisterBluetoothSignalHandler(const nsAString& aNodeName, michael@0: BluetoothSignalObserver* aMsgHandler); michael@0: michael@0: /** michael@0: * Remove a message handlers for the given observer. michael@0: * Must be called from the main thread. michael@0: * michael@0: * @param aMsgHandler Weak pointer to the object michael@0: */ michael@0: void michael@0: UnregisterAllSignalHandlers(BluetoothSignalObserver* aMsgHandler); michael@0: michael@0: /** michael@0: * Distribute a signal to the observer list michael@0: * michael@0: * @param aSignal Signal object to distribute michael@0: * michael@0: * @return NS_OK if signal distributed, NS_ERROR_FAILURE on error michael@0: */ michael@0: void michael@0: DistributeSignal(const BluetoothSignal& aEvent); michael@0: michael@0: /** michael@0: * Called when get a Bluetooth Signal from BluetoothDBusService michael@0: * michael@0: */ michael@0: void michael@0: Notify(const BluetoothSignal& aParam); michael@0: michael@0: /** michael@0: * Returns the BluetoothService singleton. Only to be called from main thread. michael@0: * michael@0: * @param aService Pointer to return singleton into. michael@0: * michael@0: * @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise (if service michael@0: * has not yet been started, for instance) michael@0: */ michael@0: static BluetoothService* michael@0: Get(); michael@0: michael@0: static already_AddRefed michael@0: FactoryCreate() michael@0: { michael@0: nsRefPtr service = Get(); michael@0: return service.forget(); michael@0: } michael@0: michael@0: /** michael@0: * Returns the path of the default adapter, implemented via a platform michael@0: * specific method. michael@0: * michael@0: * @return NS_OK on success, NS_ERROR_FAILURE otherwise michael@0: */ michael@0: virtual nsresult michael@0: GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: /** michael@0: * Returns the properties of paired devices, implemented via a platform michael@0: * specific method. michael@0: * michael@0: * @return NS_OK on success, NS_ERROR_FAILURE otherwise michael@0: */ michael@0: virtual nsresult michael@0: GetPairedDevicePropertiesInternal(const nsTArray& aDeviceAddresses, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: /** michael@0: * Returns the properties of connected devices regarding to specific profile, michael@0: * implemented via a platform specific methood. michael@0: * michael@0: * @return NS_OK on success, NS_ERROR_FAILURE otherwise michael@0: */ michael@0: virtual nsresult michael@0: GetConnectedDevicePropertiesInternal(uint16_t aServiceUuid, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: /** michael@0: * Stop device discovery (platform specific implementation) michael@0: * michael@0: * @return NS_OK if discovery stopped correctly, false otherwise michael@0: */ michael@0: virtual nsresult michael@0: StopDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: /** michael@0: * Start device discovery (platform specific implementation) michael@0: * michael@0: * @return NS_OK if discovery stopped correctly, false otherwise michael@0: */ michael@0: virtual nsresult michael@0: StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: /** michael@0: * Set a property for the specified object michael@0: * michael@0: * @param aPropName Name of the property michael@0: * @param aValue Boolean value michael@0: * @param aRunnable Runnable to run on async reply michael@0: * michael@0: * @return NS_OK if property is set correctly, NS_ERROR_FAILURE otherwise michael@0: */ michael@0: virtual nsresult michael@0: SetProperty(BluetoothObjectType aType, michael@0: const BluetoothNamedValue& aValue, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual nsresult michael@0: CreatePairedDeviceInternal(const nsAString& aAddress, michael@0: int aTimeout, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual nsresult michael@0: RemoveDeviceInternal(const nsAString& aObjectPath, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: /** michael@0: * Get corresponding service channel of specific service on remote device. michael@0: * It's usually the very first step of establishing an outbound connection. michael@0: * michael@0: * @param aObjectPath Object path of remote device michael@0: * @param aServiceUuid UUID of the target service michael@0: * @param aManager Instance which has callback function OnGetServiceChannel() michael@0: * michael@0: * @return NS_OK if the task begins, NS_ERROR_FAILURE otherwise michael@0: */ michael@0: virtual nsresult michael@0: GetServiceChannel(const nsAString& aDeviceAddress, michael@0: const nsAString& aServiceUuid, michael@0: BluetoothProfileManagerBase* aManager) = 0; michael@0: michael@0: virtual bool michael@0: UpdateSdpRecords(const nsAString& aDeviceAddress, michael@0: BluetoothProfileManagerBase* aManager) = 0; michael@0: michael@0: virtual bool michael@0: SetPinCodeInternal(const nsAString& aDeviceAddress, const nsAString& aPinCode, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual bool michael@0: SetPasskeyInternal(const nsAString& aDeviceAddress, uint32_t aPasskey, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual bool michael@0: SetPairingConfirmationInternal(const nsAString& aDeviceAddress, bool aConfirm, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: Connect(const nsAString& aDeviceAddress, uint32_t aCod, uint16_t aServiceUuid, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: Disconnect(const nsAString& aDeviceAddress, uint16_t aServiceUuid, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual bool michael@0: IsConnected(uint16_t aServiceUuid) = 0; michael@0: michael@0: virtual void michael@0: SendFile(const nsAString& aDeviceAddress, michael@0: BlobParent* aBlobParent, michael@0: BlobChild* aBlobChild, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: SendFile(const nsAString& aDeviceAddress, michael@0: nsIDOMBlob* aBlob, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: StopSendingFile(const nsAString& aDeviceAddress, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: ConfirmReceivingFile(const nsAString& aDeviceAddress, bool aConfirm, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: ConnectSco(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: DisconnectSco(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: IsScoConnected(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: #ifdef MOZ_B2G_RIL michael@0: virtual void michael@0: AnswerWaitingCall(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: IgnoreWaitingCall(BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: ToggleCalls(BluetoothReplyRunnable* aRunnable) = 0; michael@0: #endif michael@0: michael@0: virtual void michael@0: SendMetaData(const nsAString& aTitle, michael@0: const nsAString& aArtist, michael@0: const nsAString& aAlbum, michael@0: int64_t aMediaNumber, michael@0: int64_t aTotalMediaCount, michael@0: int64_t aDuration, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: SendPlayStatus(int64_t aDuration, michael@0: int64_t aPosition, michael@0: const nsAString& aPlayStatus, michael@0: BluetoothReplyRunnable* aRunnable) = 0; michael@0: michael@0: virtual void michael@0: UpdatePlayStatus(uint32_t aDuration, michael@0: uint32_t aPosition, michael@0: ControlPlayStatus aPlayStatus) = 0; michael@0: michael@0: virtual nsresult michael@0: SendSinkMessage(const nsAString& aDeviceAddresses, michael@0: const nsAString& aMessage) = 0; michael@0: michael@0: virtual nsresult michael@0: SendInputMessage(const nsAString& aDeviceAddresses, michael@0: const nsAString& aMessage) = 0; michael@0: michael@0: bool michael@0: IsEnabled() const michael@0: { michael@0: return mEnabled; michael@0: } michael@0: michael@0: bool michael@0: IsToggling() const; michael@0: michael@0: void michael@0: RemoveObserverFromTable(const nsAString& key); michael@0: michael@0: /** michael@0: * Below 2 function/variable are used for ensuring event 'AdapterAdded' will michael@0: * be fired after event 'Enabled'. michael@0: */ michael@0: void TryFiringAdapterAdded(); michael@0: void AdapterAddedReceived(); michael@0: michael@0: protected: michael@0: BluetoothService() : mEnabled(false) michael@0: , mAdapterAddedReceived(false) michael@0: { michael@0: } michael@0: michael@0: virtual ~BluetoothService(); michael@0: michael@0: bool michael@0: Init(); michael@0: michael@0: void michael@0: Cleanup(); michael@0: michael@0: nsresult michael@0: StartBluetooth(bool aIsStartup); michael@0: michael@0: nsresult michael@0: StopBluetooth(bool aIsStartup); michael@0: michael@0: nsresult michael@0: StartStopBluetooth(bool aStart, bool aIsStartup); michael@0: michael@0: /** michael@0: * Platform specific startup functions go here. Usually deals with member michael@0: * variables, so not static. Guaranteed to be called outside of main thread. michael@0: * michael@0: * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise michael@0: */ michael@0: virtual nsresult michael@0: StartInternal() = 0; michael@0: michael@0: /** michael@0: * Platform specific startup functions go here. Usually deals with member michael@0: * variables, so not static. Guaranteed to be called outside of main thread. michael@0: * michael@0: * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise michael@0: */ michael@0: virtual nsresult michael@0: StopInternal() = 0; michael@0: michael@0: /** michael@0: * Called when XPCOM first creates this service. michael@0: */ michael@0: virtual nsresult michael@0: HandleStartup(); michael@0: michael@0: /** michael@0: * Called when the startup settings check has completed. michael@0: */ michael@0: nsresult michael@0: HandleStartupSettingsCheck(bool aEnable); michael@0: michael@0: /** michael@0: * Called when "mozsettings-changed" observer topic fires. michael@0: */ michael@0: nsresult michael@0: HandleSettingsChanged(const nsAString& aData); michael@0: michael@0: /** michael@0: * Called when XPCOM is shutting down. michael@0: */ michael@0: virtual nsresult michael@0: HandleShutdown(); michael@0: michael@0: // Called by ToggleBtAck. michael@0: void michael@0: SetEnabled(bool aEnabled); michael@0: michael@0: // Called by Get(). michael@0: static BluetoothService* michael@0: Create(); michael@0: michael@0: typedef nsClassHashtable michael@0: BluetoothSignalObserverTable; michael@0: michael@0: BluetoothSignalObserverTable mBluetoothSignalObserverTable; michael@0: michael@0: bool mEnabled; michael@0: michael@0: private: michael@0: bool mAdapterAddedReceived; michael@0: }; michael@0: michael@0: END_BLUETOOTH_NAMESPACE michael@0: michael@0: #endif