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 ADAPTER_STATE_MACHINE_H 17 #define ADAPTER_STATE_MACHINE_H 18 19 #include "util/dispatcher.h" 20 #include "util/state_machine.h" 21 #include "util/timer.h" 22 23 #include "interface_adapter.h" 24 25 namespace OHOS { 26 namespace bluetooth { 27 // adapter state machine each state name 28 const std::string TURNING_ON_STATE = "TurningOn"; 29 const std::string TURN_ON_STATE = "TurnOn"; 30 const std::string TURNING_OFF_STATE = "TurningOff"; 31 const std::string TURN_OFF_STATE = "TurnOff"; 32 33 /** 34 * @brief Represents adapter state machine. 35 * 36 * @since 6 37 */ 38 class AdapterStateMachine : public utility::StateMachine { 39 public: 40 // define adapter state machine message kinds 41 enum AdapterStateMessage { 42 MSG_USER_ENABLE_REQ, 43 MSG_USER_DISABLE_REQ, 44 MSG_ADAPTER_ENABLE_CMP, 45 MSG_ADAPTER_DISABLE_CMP, 46 MSG_PROFILE_ENABLE_CMP, 47 MSG_PROFILE_DISABLE_CMP, 48 MSG_ADAPTER_ENABLE_TIME_OUT, 49 MSG_PROFILE_ENABLE_TIME_OUT, 50 MSG_ADAPTER_DISABLE_TIME_OUT, 51 MSG_PROFILE_DISABLE_TIME_OUT, 52 }; 53 54 /** 55 * @brief A constructor used to create an <b>AdapterStateMachine</b> instance. 56 * 57 * @param dispatch Adapter manager dispatch. 58 * @since 6 59 */ AdapterStateMachine(utility::Dispatcher & dispatch)60 explicit AdapterStateMachine(utility::Dispatcher &dispatch) : dispatch_(dispatch){}; 61 62 /** 63 * @brief A destructor used to delete the <b>AdapterStateMachine</b> instance. 64 * 65 * @since 6 66 */ 67 ~AdapterStateMachine() = default; 68 69 /** 70 * @brief A constructor used to create an <b>AdapterStateMachine</b> instance. 71 * 72 * @param adapter Adapter pointer(classic adapter or ble adapter). 73 * @since 6 74 */ 75 void Init(IAdapter &adapter); 76 77 /** 78 * @brief Get adapter state machine dispatch. 79 * 80 * @return Returns adapter state machine dispatch. 81 * @since 6 82 */ 83 utility::Dispatcher &GetDispatch() const; 84 85 private: 86 utility::Dispatcher &dispatch_; 87 AdapterStateMachine() = delete; 88 }; 89 90 /** 91 * @brief Represents basic adapter state. 92 * 93 * @since 6 94 */ 95 class AdapterState : public utility::StateMachine::State { 96 public: 97 /** 98 * @brief A constructor used to create an <b>AdapterState</b> instance. 99 * 100 * @param name Name of adapter state. 101 * @param stateMachine The state machine which this state belong to. 102 * @param adapter Adapter pointer(classic adapter or ble adapter). 103 * @since 6 104 */ AdapterState(const std::string & name,AdapterStateMachine & stateMachine,IAdapter & adapter)105 AdapterState(const std::string &name, AdapterStateMachine &stateMachine, IAdapter &adapter) 106 : State(name, stateMachine), adapter_(adapter), adapterStateMachine_(stateMachine){}; 107 /** 108 * @brief A destructor used to delete the <b>AdapterState</b> instance. 109 * 110 * @since 6 111 */ 112 ~AdapterState() = default; 113 114 protected: 115 IAdapter &adapter_; 116 AdapterStateMachine &adapterStateMachine_; 117 }; 118 119 class AdapterTurningOnState : public AdapterState { 120 public: 121 /** 122 * @brief A constructor used to create an <b>AdapterTurningOnState</b> instance. 123 * 124 * @param stateMachine The state machine which this state belong to. 125 * @param adapter Adapter pointer(classic adapter or ble adapter). 126 * @since 6 127 */ 128 AdapterTurningOnState(AdapterStateMachine &stateMachine, IAdapter &adapter); 129 130 /** 131 * @brief A destructor used to delete the <b>AdapterTurningOnState</b> instance. 132 * 133 * @since 6 134 */ 135 ~AdapterTurningOnState() = default; 136 /** 137 * @brief Entry adapter turning on state. 138 * 139 * @since 6 140 */ 141 virtual void Entry(); 142 143 /** 144 * @brief Exit adapter turning on state. 145 * 146 * @since 6 147 */ Exit()148 virtual void Exit(){}; 149 150 /** 151 * @brief Adapter turning on state's dispatch. 152 * 153 * @param msg Message context which is used in dispath. 154 * @return Returns <b>true</b> if the operation is accepted; 155 * returns <b>false</b> if the operation is rejected. 156 * @since 6 157 */ 158 virtual bool Dispatch(const utility::Message &msg); 159 160 private: 161 // adapter turning off state timer 162 std::unique_ptr<utility::Timer> adapterTimer_ = nullptr; 163 // profile turning off state timer 164 std::unique_ptr<utility::Timer> profileTimer_ = nullptr; 165 }; 166 167 class AdapterTurnOnState : public AdapterState { 168 public: 169 /** 170 * @brief A constructor used to create an <b>AdapterTurnOnState</b> instance. 171 * 172 * @param stateMachine The state machine which this state belong to. 173 * @param adapter Adapter pointer(classic adapter or ble adapter). 174 * @since 6 175 */ AdapterTurnOnState(AdapterStateMachine & stateMachine,IAdapter & adapter)176 AdapterTurnOnState(AdapterStateMachine &stateMachine, IAdapter &adapter) 177 : AdapterState(TURN_ON_STATE, stateMachine, adapter){}; 178 179 /** 180 * @brief A destructor used to delete the <b>AdapterTurnOnState</b> instance. 181 * 182 * @since 6 183 */ 184 ~AdapterTurnOnState() = default; 185 /** 186 * @brief Entry adapter turn on state. 187 * 188 * @since 6 189 */ 190 virtual void Entry(); 191 192 /** 193 * @brief Exit adapter turn on state. 194 * 195 * @since 6 196 */ Exit()197 virtual void Exit(){}; 198 199 /** 200 * @brief Adapter turn on state's dispatch. 201 * 202 * @param msg Message context which is used in dispath. 203 * @return Returns <b>true</b> if the operation is accepted; 204 * returns <b>false</b> if the operation is rejected. 205 * @since 6 206 */ 207 virtual bool Dispatch(const utility::Message &msg); 208 }; 209 210 class AdapterTurningOffState : public AdapterState { 211 public: 212 /** 213 * @brief A constructor used to create an <b>AdapterTurningOffState</b> instance. 214 * 215 * @param stateMachine The state machine which this state belong to. 216 * @param adapter Adapter pointer(classic adapter or ble adapter). 217 * @since 6 218 */ 219 AdapterTurningOffState(AdapterStateMachine &stateMachine, IAdapter &adapter); 220 221 /** 222 * @brief A destructor used to delete the <b>AdapterTurningOffState</b> instance. 223 * 224 * @since 6 225 */ 226 ~AdapterTurningOffState() = default; 227 228 /** 229 * @brief Entry adapter turning off state. 230 * 231 * @since 6 232 */ 233 virtual void Entry(); 234 235 /** 236 * @brief Exit adapter turning off state. 237 * 238 * @since 6 239 */ Exit()240 virtual void Exit(){}; 241 242 /** 243 * @brief Adapter turning off state's dispatch. 244 * 245 * @param msg Message context which is used in dispath. 246 * @return Returns <b>true</b> if the operation is accepted; 247 * returns <b>false</b> if the operation is rejected. 248 * @since 6 249 */ 250 virtual bool Dispatch(const utility::Message &msg); 251 252 private: 253 // adapter turning off state timer 254 std::unique_ptr<utility::Timer> adapterTimer_ = nullptr; 255 // profile turning off state timer 256 std::unique_ptr<utility::Timer> profileTimer_ = nullptr; 257 }; 258 259 class AdapterTurnOffState : public AdapterState { 260 public: 261 /** 262 * @brief A constructor used to create an <b>AdapterTurnOffState</b> instance. 263 * 264 * @param stateMachine The state machine which this state belong to. 265 * @param adapter Adapter pointer(classic adapter or ble adapter). 266 * @since 6 267 */ AdapterTurnOffState(AdapterStateMachine & stateMachine,IAdapter & adapter)268 AdapterTurnOffState(AdapterStateMachine &stateMachine, IAdapter &adapter) 269 : AdapterState(TURN_OFF_STATE, stateMachine, adapter){}; 270 271 /** 272 * @brief A destructor used to delete the <b>AdapterTurnOffState</b> instance. 273 * 274 * @since 6 275 */ 276 ~AdapterTurnOffState() = default; 277 278 /** 279 * @brief Entry adapter turn off state. 280 * 281 * @since 6 282 */ 283 virtual void Entry(); 284 285 /** 286 * @brief Exit adapter turn off state. 287 * 288 * @since 6 289 */ Exit()290 virtual void Exit(){}; 291 292 /** 293 * @brief Adapter turn off state's dispatch. 294 * 295 * @param msg Message context which is used in dispath. 296 * @return Returns <b>true</b> if the operation is accepted; 297 * returns <b>false</b> if the operation is rejected. 298 * @since 6 299 */ 300 virtual bool Dispatch(const utility::Message &msg); 301 }; 302 } // namespace bluetooth 303 } // namespace OHOS 304 305 #endif // ADAPTER_STATE_MACHINE_H