1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef POWER_DEVICE_H 17 #define POWER_DEVICE_H 18 19 #include "dispatcher.h" 20 #include "power_spec.h" 21 #include "raw_address.h" 22 #include "timer.h" 23 24 namespace OHOS { 25 namespace bluetooth { 26 /** 27 * @brief Represents power device. 28 * 29 * @since 6 30 */ 31 class PowerDevice : public std::enable_shared_from_this<PowerDevice> { 32 public: 33 /** 34 * @brief Construct PowerDevice object. 35 * 36 * @param addr Peer device bluetooth addr. 37 * @param dispatcher Reference of dispatcher resource. 38 * @since 6 39 */ 40 PowerDevice(const RawAddress rawAddr, utility::Dispatcher &dispatcher); 41 42 /** 43 * @brief Delete default Construct function. 44 * 45 * @since 6 46 */ 47 PowerDevice() = delete; 48 49 /** 50 * @brief Destruct PowerDevice object. 51 * 52 * @since 6 53 */ 54 ~PowerDevice(); 55 56 /** 57 * @brief Update Request Power Map, update profile status. 58 * 59 * @param profileName Profile Name. 60 * @param status Profile Status. 61 * @since 6 62 */ 63 void SetRequestPower(const std::string &profileName, const RequestStatus status) const; 64 65 /** 66 * @brief Update Request Power Map, delete profile status. 67 * 68 * @param profileName Profile Name. 69 * @since 6 70 */ 71 void DeleteRequestPower(const std::string &profileName) const; 72 73 /** 74 * @brief Set Power Mode, base on current connecting profiles and their status. 75 * 76 * @since 6 77 */ 78 void SetPowerMode(); 79 80 /** 81 * @brief Get power mode. 82 * 83 * @return Returns power mode grade. 84 * BTPowerMode::MODE_INVALID = 0x00, 85 * BTPowerMode::MODE_ACTIVE = 0x100, 86 * BTPowerMode::MODE_SNIFF_LEVEL_LOW = 0x201, 87 * BTPowerMode::MODE_SNIFF_LEVEL_MID = 0x202, 88 * BTPowerMode::MODE_SNIFF_LEVEL_HIG = 0x203, 89 * @since 6 90 */ 91 BTPowerMode GetPowerMode() const; 92 93 /** 94 * @brief Callback while acl link road power mode state update. 95 * 96 * @param status Hci status. 97 * @param currentMode Current acl link road power mode. 98 * @param interval Monitor interval. 99 * @since 6 100 */ 101 void ModeChangeCallBack(uint8_t status, uint8_t currentMode, uint16_t interval); 102 103 /** 104 * @brief Callback of Complete set sniff subrating params. 105 * 106 * @param status Hci status. 107 * @since 6 108 */ 109 void SniffSubratingCompleteCallback(uint8_t status) const; 110 111 private: 112 /** 113 * @brief Send SetActiveMode message to power state machine. 114 * 115 * @since 6 116 */ 117 void SetActiveMode(); 118 119 /** 120 * @brief Start sniffDelayTimer_ 121 * 122 * @param timeoutMs time-out period(ms). 123 * @since 6 124 */ 125 void SetSniffMode(PowerInfo requestPower); 126 127 /** 128 * @brief Calculate max power mode, base on current profiles and status. 129 * 130 * @since 6 131 */ 132 PowerInfo CalcMaxPower() const; 133 134 /** 135 * @brief Calculate lowest ssr level, base on current profiles and status. 136 * 137 * @since 6 138 */ 139 PowerSsrLevel CalcLowestSsrLevel() const; 140 141 /** 142 * @brief Set sniff subrating params to btm. 143 * 144 * @param ssrParam Sniff subrating params. 145 * @return 146 * @since 6 147 */ 148 int BtmSetSniffSubrating(const PowerSsrParam &ssrParam) const; 149 150 /** 151 * @brief Btm exit sniff subrating mode. 152 * 153 * @return 154 * @since 6 155 */ 156 int BtmExitSniffMode() const; 157 158 /** 159 * @brief Btm enter sniff subrating mode. 160 * 161 * @param param Sniff params. 162 * @return 163 * @since 6 164 */ 165 int BtmEnterSniffMode(const PowerParam ¶m) const; 166 167 /** 168 * @brief Get present sniff params, used by power state machine. 169 * 170 * @return std::pair<PowerSsrLevel, PowerModeLevel> 171 * @since 6 172 */ 173 const std::pair<PowerSsrLevel, PowerModeLevel> &GetRequestPowerLevel() const; 174 175 /** 176 * @brief Set present sniff params. 177 * 178 * @param sniffParams std::pair<PowerSsrLevel, PowerModeLevel>. 179 * @since 6 180 */ 181 void SetRequestPowerLevel(const PowerSsrLevel ssr, const PowerModeLevel power) const; 182 183 /** 184 * @brief update control sniff subrating level. 185 * 186 * @param ssr PowerSsrLevel. 187 * @since 6 188 */ 189 void UpdateControlSniffSubrating(const PowerSsrLevel ssr) const; 190 191 /** 192 * @brief update control sniff power level. 193 * 194 * @param power PowerModeLevel. 195 * @since 6 196 */ 197 void UpdatecontrolPowerLevel(const PowerModeLevel powerLevel) const; 198 199 /** 200 * @brief Get present sniff params, used by power state machine. 201 * 202 * @return std::pair<PowerSsrLevel, PowerModeLevel> 203 * @since 6 204 */ 205 const std::pair<PowerSsrLevel, PowerModeLevel> &GetControlPowerLevel() const; 206 207 /** 208 * @brief Sniff Delay Timer callback. 209 * 210 * @since 6 211 */ 212 static void DelayTimeoutCallback(const std::weak_ptr<PowerDevice>& weakDevice); 213 214 /** 215 * @brief Start Sniff Delay Timer. 216 * 217 * @param ms Time-out period(ms). 218 * @since 6 219 */ 220 void StartDelayTimer(int ms) const; 221 222 /** 223 * @brief Stop Sniff Delay Timer. 224 * 225 * @since 6 226 */ 227 void StopDelayTimer() const; 228 229 /** 230 * @brief Get current delaytimer remain time(ms). 231 * 232 * @return Remain time(ms). 233 * @since 6 234 */ 235 uint64_t GetDelayTimerRemainMs() const; 236 237 class PowerTimer : public utility::Timer { 238 public: 239 /** 240 * @brief Construct PowerTimer object. 241 * 242 * @param callback PowerTimer timeout callback. 243 * @since 6 244 */ PowerTimer(const std::function<void ()> & callback)245 explicit PowerTimer(const std::function<void()> &callback) : utility::Timer(callback){}; 246 247 /** 248 * @brief Delete PowerTimer default Construct function. 249 * 250 * @since 6 251 */ 252 PowerTimer() = delete; 253 254 /** 255 * @brief Start PowerTimer. 256 * 257 * @param ms Time-out period(ms). 258 * @param isPeriodic Whether PowerTimer is periodic. 259 * @return Returns <b>true</b> if the operation is successful; 260 * returns <b>false</b> if the operation fails. 261 * @since 6 262 */ 263 bool Start(int ms, bool isPeriodic = false); 264 265 /** 266 * @brief Stop PowerTimer. 267 * 268 * @return Returns <b>true</b> if the operation is successful; 269 * returns <b>false</b> if the operation fails. 270 * @since 6 271 */ 272 bool Stop(); 273 274 /** 275 * @brief Get PowerTimer remain time(ms). 276 * 277 * @return Return remain time(ms). 278 * @since 6 279 */ 280 uint64_t GetRemainMs(); 281 282 private: 283 #define MS_PER_SECOND 1000 284 #define NS_PER_MS 1000000 285 286 uint64_t deadLineMs_ = 0; 287 BT_DISALLOW_COPY_AND_ASSIGN(PowerTimer); 288 }; 289 290 friend class PowerActiveActivingState; 291 friend class PowerActiveSniffingState; 292 friend class PowerSniffActivingState; 293 friend class PowerSniffSniffingState; 294 friend class PowerSniffState; 295 296 BT_DISALLOW_COPY_AND_ASSIGN(PowerDevice); 297 DECLARE_IMPL(); 298 }; 299 } // namespace bluetooth 300 } // namespace OHOS 301 302 #endif // POWER_DEVICE_H