1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bluetooth/BluetoothProfileController.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_dom_bluetooth_bluetoothprofilecontroller_h__ 1.11 +#define mozilla_dom_bluetooth_bluetoothprofilecontroller_h__ 1.12 + 1.13 +#include "BluetoothUuid.h" 1.14 +#include "nsISupportsImpl.h" 1.15 +#include "nsAutoPtr.h" 1.16 +#include "nsITimer.h" 1.17 + 1.18 +BEGIN_BLUETOOTH_NAMESPACE 1.19 + 1.20 +/* 1.21 + * Class of Device(CoD): 32-bit unsigned integer 1.22 + * 1.23 + * 31 24 23 13 12 8 7 2 1 0 1.24 + * | | Major | Major | Minor | | 1.25 + * | | service | device | device | | 1.26 + * | | class | class | class | | 1.27 + * | |<- 11 ->|<- 5 ->|<- 6 ->| | 1.28 + * 1.29 + * https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband 1.30 + */ 1.31 + 1.32 +// Bit 23 ~ Bit 13: Major service class 1.33 +#define GET_MAJOR_SERVICE_CLASS(cod) ((cod & 0xffe000) >> 13) 1.34 + 1.35 +// Bit 12 ~ Bit 8: Major device class 1.36 +#define GET_MAJOR_DEVICE_CLASS(cod) ((cod & 0x1f00) >> 8) 1.37 + 1.38 +// Bit 7 ~ Bit 2: Minor device class 1.39 +#define GET_MINOR_DEVICE_CLASS(cod) ((cod & 0xfc) >> 2) 1.40 + 1.41 +// Audio: Major service class = 0x100 (Bit 21 is set) 1.42 +#define HAS_AUDIO(cod) (cod & 0x200000) 1.43 + 1.44 +// Rendering: Major service class = 0x20 (Bit 18 is set) 1.45 +#define HAS_RENDERING(cod) (cod & 0x40000) 1.46 + 1.47 +// Peripheral: Major device class = 0x5 1.48 +#define IS_PERIPHERAL(cod) (GET_MAJOR_DEVICE_CLASS(cod) == 0x5) 1.49 + 1.50 +// Remote Control: sub-field of minor device class, Bit 5 ~ Bit 2 = 0x3 1.51 +#define IS_REMOTE_CONTROL(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0xf) == 0x3) 1.52 + 1.53 +// Keyboard: sub-field of minor device class (Bit 6) 1.54 +#define IS_KEYBOARD(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x10) >> 4) 1.55 + 1.56 +// Pointing device: sub-field of minor device class (Bit 7) 1.57 +#define IS_POINTING_DEVICE(cod) ((GET_MINOR_DEVICE_CLASS(cod) & 0x20) >> 5) 1.58 + 1.59 +class BluetoothProfileManagerBase; 1.60 +class BluetoothReplyRunnable; 1.61 +typedef void (*BluetoothProfileControllerCallback)(); 1.62 + 1.63 +class BluetoothProfileController MOZ_FINAL 1.64 +{ 1.65 + ~BluetoothProfileController(); 1.66 + 1.67 +public: 1.68 + NS_INLINE_DECL_REFCOUNTING(BluetoothProfileController) 1.69 + /** 1.70 + * @param aConnect: If it's a connect request, the value should be set 1.71 + * to true. For disconnect request, set it to false. 1.72 + * @param aDeviceAddress: The address of remote device. 1.73 + * @param aRunnable: Once the controller has done, the runnable will be 1.74 + * replied. When all connection/disconnection attemps 1.75 + * have failed, an error is fired. In other words, 1.76 + * reply a success if any attemp successes. 1.77 + * @param aCallback: The callback will be invoked after the runnable is 1.78 + * replied. 1.79 + * @param aServiceUuid: Connect/Disconnect to the specified profile. Please 1.80 + * see enum BluetoothServiceClass for valid value. 1.81 + * @param aCod: If aServiceUuid is not assigned, i.e. the value is 1.82 + * 0, the controller connect multiple profiles based on 1.83 + * aCod or disconnect all connected profiles. 1.84 + */ 1.85 + BluetoothProfileController(bool aConnect, 1.86 + const nsAString& aDeviceAddress, 1.87 + BluetoothReplyRunnable* aRunnable, 1.88 + BluetoothProfileControllerCallback aCallback, 1.89 + uint16_t aServiceUuid, 1.90 + uint32_t aCod = 0); 1.91 + 1.92 + /** 1.93 + * The controller starts connecting/disconnecting profiles one by one 1.94 + * according to the order in array mProfiles. 1.95 + */ 1.96 + void StartSession(); 1.97 + 1.98 + /** 1.99 + * The original DOM request would be fired in this function. 1.100 + */ 1.101 + void EndSession(); 1.102 + 1.103 + /** 1.104 + * It would be invoked after connect/disconnect operation is completed. 1.105 + * An error string would be returned when it fails. 1.106 + */ 1.107 + void NotifyCompletion(const nsAString& aErrorStr); 1.108 + 1.109 + /** 1.110 + * It is invoked after a profile has reached timeout, reset mProfiles. 1.111 + */ 1.112 + void GiveupAndContinue(); 1.113 + 1.114 +private: 1.115 + // Setup data member mProfiles 1.116 + void SetupProfiles(bool aAssignServiceClass); 1.117 + 1.118 + // Add profiles into array with/without checking connection status 1.119 + void AddProfile(BluetoothProfileManagerBase* aProfile, 1.120 + bool aCheckConnected = false); 1.121 + 1.122 + // Add specified profile into array 1.123 + void AddProfileWithServiceClass(BluetoothServiceClass aClass); 1.124 + 1.125 + // Connect/Disconnect next profile in the array 1.126 + void Next(); 1.127 + 1.128 + const bool mConnect; 1.129 + nsString mDeviceAddress; 1.130 + nsRefPtr<BluetoothReplyRunnable> mRunnable; 1.131 + BluetoothProfileControllerCallback mCallback; 1.132 + 1.133 + bool mCurrentProfileFinished; 1.134 + bool mSuccess; 1.135 + int8_t mProfilesIndex; 1.136 + nsTArray<BluetoothProfileManagerBase*> mProfiles; 1.137 + 1.138 + // Either CoD or BluetoothServiceClass is assigned. 1.139 + union { 1.140 + uint32_t cod; 1.141 + BluetoothServiceClass service; 1.142 + } mTarget; 1.143 + 1.144 + nsCOMPtr<nsITimer> mTimer; 1.145 + nsCOMPtr<nsITimerCallback> mCheckProfileStatusCallback; 1.146 +}; 1.147 + 1.148 +END_BLUETOOTH_NAMESPACE 1.149 + 1.150 +#endif