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_bluetoothprofilecontroller_h__ michael@0: #define mozilla_dom_bluetooth_bluetoothprofilecontroller_h__ michael@0: michael@0: #include "BluetoothUuid.h" michael@0: #include "nsISupportsImpl.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsITimer.h" michael@0: michael@0: BEGIN_BLUETOOTH_NAMESPACE michael@0: michael@0: /* michael@0: * Class of Device(CoD): 32-bit unsigned integer michael@0: * michael@0: * 31 24 23 13 12 8 7 2 1 0 michael@0: * | | Major | Major | Minor | | michael@0: * | | service | device | device | | michael@0: * | | class | class | class | | michael@0: * | |<- 11 ->|<- 5 ->|<- 6 ->| | michael@0: * michael@0: * https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband michael@0: */ michael@0: michael@0: // Bit 23 ~ Bit 13: Major service class michael@0: #define GET_MAJOR_SERVICE_CLASS(cod) ((cod & 0xffe000) >> 13) michael@0: michael@0: // Bit 12 ~ Bit 8: Major device class michael@0: #define GET_MAJOR_DEVICE_CLASS(cod) ((cod & 0x1f00) >> 8) michael@0: michael@0: // Bit 7 ~ Bit 2: Minor device class michael@0: #define GET_MINOR_DEVICE_CLASS(cod) ((cod & 0xfc) >> 2) michael@0: michael@0: // Audio: Major service class = 0x100 (Bit 21 is set) michael@0: #define HAS_AUDIO(cod) (cod & 0x200000) michael@0: michael@0: // Rendering: Major service class = 0x20 (Bit 18 is set) michael@0: #define HAS_RENDERING(cod) (cod & 0x40000) michael@0: michael@0: // Peripheral: Major device class = 0x5 michael@0: #define IS_PERIPHERAL(cod) (GET_MAJOR_DEVICE_CLASS(cod) == 0x5) michael@0: michael@0: // Remote Control: sub-field of minor device class, Bit 5 ~ Bit 2 = 0x3 michael@0: #define IS_REMOTE_CONTROL(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0xf) == 0x3) michael@0: michael@0: // Keyboard: sub-field of minor device class (Bit 6) michael@0: #define IS_KEYBOARD(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x10) >> 4) michael@0: michael@0: // Pointing device: sub-field of minor device class (Bit 7) michael@0: #define IS_POINTING_DEVICE(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x20) >> 5) michael@0: michael@0: class BluetoothProfileManagerBase; michael@0: class BluetoothReplyRunnable; michael@0: typedef void (*BluetoothProfileControllerCallback)(); michael@0: michael@0: class BluetoothProfileController MOZ_FINAL michael@0: { michael@0: ~BluetoothProfileController(); michael@0: michael@0: public: michael@0: NS_INLINE_DECL_REFCOUNTING(BluetoothProfileController) michael@0: /** michael@0: * @param aConnect: If it's a connect request, the value should be set michael@0: * to true. For disconnect request, set it to false. michael@0: * @param aDeviceAddress: The address of remote device. michael@0: * @param aRunnable: Once the controller has done, the runnable will be michael@0: * replied. When all connection/disconnection attemps michael@0: * have failed, an error is fired. In other words, michael@0: * reply a success if any attemp successes. michael@0: * @param aCallback: The callback will be invoked after the runnable is michael@0: * replied. michael@0: * @param aServiceUuid: Connect/Disconnect to the specified profile. Please michael@0: * see enum BluetoothServiceClass for valid value. michael@0: * @param aCod: If aServiceUuid is not assigned, i.e. the value is michael@0: * 0, the controller connect multiple profiles based on michael@0: * aCod or disconnect all connected profiles. michael@0: */ michael@0: BluetoothProfileController(bool aConnect, michael@0: const nsAString& aDeviceAddress, michael@0: BluetoothReplyRunnable* aRunnable, michael@0: BluetoothProfileControllerCallback aCallback, michael@0: uint16_t aServiceUuid, michael@0: uint32_t aCod = 0); michael@0: michael@0: /** michael@0: * The controller starts connecting/disconnecting profiles one by one michael@0: * according to the order in array mProfiles. michael@0: */ michael@0: void StartSession(); michael@0: michael@0: /** michael@0: * The original DOM request would be fired in this function. michael@0: */ michael@0: void EndSession(); michael@0: michael@0: /** michael@0: * It would be invoked after connect/disconnect operation is completed. michael@0: * An error string would be returned when it fails. michael@0: */ michael@0: void NotifyCompletion(const nsAString& aErrorStr); michael@0: michael@0: /** michael@0: * It is invoked after a profile has reached timeout, reset mProfiles. michael@0: */ michael@0: void GiveupAndContinue(); michael@0: michael@0: private: michael@0: // Setup data member mProfiles michael@0: void SetupProfiles(bool aAssignServiceClass); michael@0: michael@0: // Add profiles into array with/without checking connection status michael@0: void AddProfile(BluetoothProfileManagerBase* aProfile, michael@0: bool aCheckConnected = false); michael@0: michael@0: // Add specified profile into array michael@0: void AddProfileWithServiceClass(BluetoothServiceClass aClass); michael@0: michael@0: // Connect/Disconnect next profile in the array michael@0: void Next(); michael@0: michael@0: const bool mConnect; michael@0: nsString mDeviceAddress; michael@0: nsRefPtr mRunnable; michael@0: BluetoothProfileControllerCallback mCallback; michael@0: michael@0: bool mCurrentProfileFinished; michael@0: bool mSuccess; michael@0: int8_t mProfilesIndex; michael@0: nsTArray mProfiles; michael@0: michael@0: // Either CoD or BluetoothServiceClass is assigned. michael@0: union { michael@0: uint32_t cod; michael@0: BluetoothServiceClass service; michael@0: } mTarget; michael@0: michael@0: nsCOMPtr mTimer; michael@0: nsCOMPtr mCheckProfileStatusCallback; michael@0: }; michael@0: michael@0: END_BLUETOOTH_NAMESPACE michael@0: michael@0: #endif