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