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 OHOS_STA_MACHINE_H 17 #define OHOS_STA_MACHINE_H 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 #include "handler.h" 23 #include "message_queue.h" 24 #include "state.h" 25 26 namespace OHOS { 27 namespace Wifi { 28 #define CMD_SET_OPERATIONAL_MODE 1 29 /* Message Processed */ 30 static const bool EXECUTED = true; 31 /* The message is not processed. */ 32 static const bool NOT_EXECUTED = false; 33 34 class StateMachineHandler; 35 class StateMachine { 36 public: 37 /** 38 * @Description : StateMachine Initialization Function 39 * 40 * @return true :success, false : failed. 41 */ 42 bool InitialStateMachine(); 43 44 /** 45 * @Description : Start StateMachine. 46 * 47 */ 48 void StartStateMachine(); 49 50 /** 51 * @Description : Set Handler. 52 * 53 * @param handler - StateMachineHandler instance.[in] 54 */ 55 void SetHandler(StateMachineHandler *handler); 56 57 /** 58 * @Description : The Message is not handled. 59 * 60 * @param msg - Message object.[in] 61 */ 62 void NotExecutedMessage(const InternalMessage *msg); 63 64 /** 65 * @Description Stop Handler Thread. 66 * 67 */ 68 void StopHandlerThread(); 69 70 /** 71 * @Description : Start the timer. 72 * 73 * @param timerName - Timer Name.[in] 74 * @param interval - Timer duration, in milliseconds.[in] 75 */ 76 virtual void StartTimer(int timerName, int64_t interval); 77 78 /** 79 * @Description : Stop the timer. 80 * 81 * @param timerName - Timer Name.[in] 82 */ 83 virtual void StopTimer(int timerName); 84 85 /** 86 * @Description : Construct internal messages. 87 * 88 * @return InternalMessage* : Pointer to the constructed internal message. 89 */ 90 InternalMessage *CreateMessage(); 91 92 /** 93 * @Description : Construct an information message based on 94 * the original message. 95 * 96 * @param orig - Original message.[in] 97 * @return InternalMessage* : Pointer to the constructed internal message. 98 */ 99 InternalMessage *CreateMessage(const InternalMessage *orig); 100 101 /** 102 * @Description : Construct internal messages. 103 * 104 * @param msgName - Message Name.[in] 105 * @return InternalMessage* : Pointer to the constructed internal message. 106 */ 107 InternalMessage *CreateMessage(int msgName); 108 109 /** 110 * @Description : Construct internal messages. 111 * 112 * @param msgName - Message Name.[in] 113 * @param param1 - Message parameters.[in] 114 * @return InternalMessage* : Pointer to the constructed internal message. 115 */ 116 InternalMessage *CreateMessage(int msgName, int param1); 117 118 /** 119 * @Description : Construct internal messages. 120 * 121 * @param msgName - Message Name.[in] 122 * @param param1 - Message parameters.[in] 123 * @param param2 - Message parameters.[in] 124 * @return InternalMessage* : Pointer to the constructed internal message. 125 */ 126 InternalMessage *CreateMessage(int msgName, int param1, int param2); 127 128 /** 129 * @Description : Construct internal messages. 130 * 131 * @param msgName - Message Name.[in] 132 * @param messageObj - User-defined data 133 * @return InternalMessage* : Pointer to the constructed internal message. 134 */ 135 InternalMessage *CreateMessage(int msgName, const std::any &messageObj); 136 137 /** 138 * @Description : Constructs internal messages. 139 * 140 * @param msgName - Message name.[in] 141 * @param param1 - First Message parameter.[in] 142 * @param param2 - Second Message parameter.[in] 143 * @param messageObj - User-defined data 144 * @return InternalMessage* : Pointer to the constructed internal message. 145 */ 146 InternalMessage *CreateMessage(int msgName, int param1, int param2, const std::any &messageObj); 147 148 /** 149 * @Description : Constructs internal messages and places the 150 * messages in the message queue of the state machine. 151 * 152 * @param msgName - Message name.[in] 153 */ 154 virtual void SendMessage(int msgName); 155 156 /** 157 * @Description : Constructs internal messages and places the messages 158 * in the message queue of the state machine. 159 * 160 * @param msgName - Message name.[in] 161 * @param param1 - Message parameter.[in] 162 */ 163 virtual void SendMessage(int msgName, int param1); 164 165 /** 166 * @Description : Constructs internal messages and places the messages 167 * in the message queue of the state machine. 168 * 169 * @param msgName - Message name.[in] 170 * @param param1 - Message parameter.[in] 171 * @param param2 - Message parameter.[in] 172 */ 173 virtual void SendMessage(int msgName, int param1, int param2); 174 175 /** 176 * @Description : Puts messages into the message queue of the state machine. 177 * 178 * @param msg - Message to be sent.[in] 179 */ 180 virtual void SendMessage(InternalMessage *msg); 181 182 /** 183 * @Description : Puts messages into the message queue of the state machine. 184 * 185 * @param msgName - Message Name.[in] 186 * @param messageObj - User-defined data 187 */ 188 virtual void SendMessage(int msgName, const std::any &messageObj); 189 190 /** 191 * @Description : Puts messages into the message queue of the state machine. 192 * 193 * @param msgName - Message Name.[in] 194 * @param param1 - Message parameters.[in] 195 * @param param2 - Message parameters.[in] 196 * @param messageObj - User-defined data 197 */ 198 virtual void SendMessage(int msgName, int param1, int param2, const std::any &messageObj); 199 200 /** 201 * @Description Constructs internal messages and places them in the 202 * message queue of the state machine. The messages are processed 203 * after the specified delay time. 204 * 205 * @param msgName - Message Name.[in] 206 * @param delayTimeMs - Delay time, in milliseconds.[in] 207 */ 208 void MessageExecutedLater(int msgName, int64_t delayTimeMs); 209 210 /** 211 * @Description : Constructs internal messages and places them in the 212 * message queue of the state machine. The messages are processed 213 * after the specified delay time. 214 * 215 * @param msgName - Message Name.[in] 216 * @param param1 - Message parameters.[in] 217 * @param delayTimeMs - Delay time, in milliseconds.[in] 218 */ 219 void MessageExecutedLater(int msgName, int param1, int64_t delayTimeMs); 220 221 /** 222 * @Description : Constructs internal messages and places them in the 223 * message queue of the state machine. The messages are processed 224 * after the specified delay time. 225 * 226 * @param msgName - Message Name.[in] 227 * @param param1 - Message parameters.[in] 228 * @param param2 - Message parameters.[in] 229 * @param delayTimeMs - Delay time, in milliseconds.[in] 230 */ 231 void MessageExecutedLater(int msgName, int param1, int param2, int64_t delayTimeMs); 232 233 /** 234 * @Description : Constructs internal messages and places them in the 235 * message queue of the state machine. The messages are processed 236 * after the specified delay time. 237 * 238 * @param msg - Message to be sent.[in] 239 * @param delayTimeMs - Delay time, in milliseconds.[in] 240 */ 241 void MessageExecutedLater(InternalMessage *msg, int64_t delayTimeMs); 242 243 /** 244 * @Description : Constructs internal messages and places them in the 245 * message queue of the state machine. The messages are processed 246 * after the specified delay time. 247 * 248 * @param msgName - Message Name.[in] 249 * @param messageObj -User-defined data 250 * @param delayTimeMs - Delay time, in milliseconds.[in] 251 */ 252 void MessageExecutedLater(int msgName, const std::any &messageObj, int64_t delayTimeMs); 253 254 /** 255 * @Description : Constructs internal messages and places them in the 256 * message queue of the state machine. The messages are processed 257 * after the specified delay time. 258 * 259 * @param msgName - Message Name.[in] 260 * @param param1 - Message parameters.[in] 261 * @param param2 - Message parameters.[in] 262 * @param messageObj - User-defined data 263 * @param delayTimeMs - Delay time, in milliseconds.[in] 264 */ 265 void MessageExecutedLater(int msgName, int param1, int param2, const std::any &messageObj, int64_t delayTimeMs); 266 267 protected: 268 /** 269 * @Description : Construct a new State Machine:: State Machine object. 270 * 271 * @param name - State name.[in] 272 */ 273 explicit StateMachine(const std::string &name); 274 275 /** 276 * @Description : Destroy the State Machine:: State Machine object. 277 * 278 */ 279 virtual ~StateMachine(); 280 281 /** 282 * @Description : Add state. 283 * 284 * @param state - state.[in] 285 * @param upper - upper state.[in] 286 */ 287 void StatePlus(State *state, State *upper); 288 289 /** 290 * @Description : Remove state. 291 * 292 * @param state - state.[in] 293 */ 294 void StateDelete(State *state); 295 296 /** 297 * @Description : Set first state. 298 * 299 * @param firstState - First state.[in] 300 */ 301 void SetFirstState(State *firstState); 302 303 /** 304 * @Description : Transition to orther state. 305 * 306 * @param targetState - state.[in] 307 */ 308 void SwitchState(State *targetState); 309 310 /** 311 * @Description : Delay Message. 312 * 313 * @param msg - Message object.[in] 314 */ 315 void DelayMessage(const InternalMessage *msg); 316 317 private: 318 StateMachineHandler *pStateMachineHandler; 319 std::string mStateName; 320 }; 321 322 typedef struct StateInfo { 323 State *state; 324 StateInfo *upperStateInfo; 325 bool active; 326 } StateInfo; 327 328 class StateMachineHandler : public Handler { 329 public: 330 using StateInfoMap = std::map<std::string, StateInfo *>; 331 using StateVector = std::vector<StateInfo *>; 332 using DelayedMessage = std::vector<InternalMessage *>; 333 334 /** 335 * @Description : Construct a new state machine Handler:: StateMachine Handler object. 336 * 337 * @param pStateMgr - Handler pointer.[in] 338 */ 339 explicit StateMachineHandler(StateMachine *pStateMgr); 340 341 /** 342 * @Description : Destroy the StateMachine Handler:: StateMachine Handler object. 343 * 344 */ 345 ~StateMachineHandler(); 346 347 /** 348 * @Description : StateMachineHandler Initialization Function. 349 * 350 * @return true : success, false : failed. 351 */ 352 bool InitialSmHandler(); 353 354 /** 355 * @Description : Add a new state. 356 * 357 * @param state - State to be added.[in] 358 * @param upper - upper of state.[in] 359 * @return StateInfo* 360 */ 361 StateInfo *StatePlus(State *state, State *upper); 362 363 /** 364 * @Description : Delete a state. 365 * 366 * @param state - State to be deleted.[in] 367 */ 368 void StateDelete(State *state); 369 370 /** 371 * @Description : Sets the Initialization State. 372 * 373 * @param firstState - Initialization State.[in] 374 */ 375 void SetFirstState(State *firstState); 376 377 /** 378 * @Description : State transition function. 379 * 380 * @param targetState - Destination State.[in] 381 */ 382 void SwitchState(State *targetState); 383 384 /** 385 * @Description : Delay Message Processing Function. 386 * 387 * @param msg - Message body pointer.[in] 388 */ 389 void DelayMessage(const InternalMessage *msg); 390 391 /** 392 * @Description : The state machine is constructed. 393 * 394 */ 395 void BuildTreeComplete(); 396 397 private: 398 /** 399 * @Description : Sets the initial state sequence. 400 * 401 */ 402 void BuildStateInitVector(); 403 404 /** 405 * @Description : Writes the inactive upper states of targetState 406 * and targetState to the sequenceStateVector list. 407 * 408 * @param targetState - Target State Machine.[in] 409 * @return StateInfo* 410 */ 411 StateInfo *BuildSequenceStateVector(State *targetState); 412 413 /** 414 * @Description : Move Delayed Message At Front Of Queue. 415 * 416 */ 417 void PlaceDelayedMsgQueueTop(); 418 419 /** 420 * @Description : Release all messages in delayed Messages. 421 * 422 */ 423 void ReleaseDelayedMessages(); 424 425 /** 426 * @Description : Fill the status in the sequential status 427 * list in reverse order. 428 * 429 * @return int 430 */ 431 int MoveSequenceToStateVector(); 432 433 /** 434 * @Description : Invoke the ExecuteStateMsg interface of the current state 435 * to process messages sent to the state machine. The entry/exit of the 436 * state machine is also called, and the delayed messagei s put back 437 * into queue when transitioning to a new state. 438 * 439 * @param msg - Messages.[in] 440 */ 441 void ExecuteMessage(InternalMessage *msg) override; 442 443 /** 444 * @Description : Clean up After Quitting. 445 * 446 */ 447 void ClearWhenQuit(); 448 449 /** 450 * @Description : Performing Status Transitions. 451 * 452 * @param msgProcessedState - Message processing status.[in] 453 * @param msg - Messages.[in] 454 */ 455 void PerformSwitchState(State *msgProcessedState, InternalMessage *msg); 456 457 /** 458 * @Description : Process messages. If the current state does not process it, 459 * the upper state processing is called, and so on. If all upper states 460 * are not processed, invoke the NotExecutedMessage method of the state machine. 461 * 462 * @param msg - Message body pointer.[in] 463 * @return State* 464 */ 465 State *ExecuteTreeStateMsg(InternalMessage *msg); 466 467 /** 468 * @Description : Invoke GoOutState() for each state from the first 469 * state in the list to the public upper state. 470 * 471 * @param commonStateInfo - common upper state machine.[in] 472 */ 473 void CallTreeStateExits(StateInfo *commonStateInfo); 474 475 /** 476 * @Description : Call the GoInState method from the start state 477 * index to the top of the state stack. 478 * 479 * @param index - Start state index of the 480 * state machine list. 481 */ 482 void CallTreeStateEnters(int index); 483 484 private: 485 /* All state mappings of the state machine */ 486 StateInfoMap mStateInfoMap; 487 /* From child state to upper state list */ 488 StateVector mStateVector; 489 /* Top index of mStateVector */ 490 int mStateVectorTopIndex; 491 /* From upper state to child state list */ 492 StateVector mSequenceStateVector; 493 /* Top of mSequenceStateVector */ 494 int mSequenceStateVectorCount; 495 /* Delayed Message Queue */ 496 DelayedMessage mDelayedMessages; 497 /* State machine instance */ 498 StateMachine *pStateMachine; 499 /* Initial state */ 500 State *pFirstState; 501 /* Target Status */ 502 State *pTargetState; 503 /* StateMachine exit or not */ 504 bool mQuitFlag; 505 /* Whether the state machine has been built */ 506 bool mBuildCompleteFlag; 507 /* 508 * All State exit/enter calls are true before the 509 * last enter call in the target state. 510 */ 511 bool mSwitchingStateFlag; 512 /* Current Message */ 513 InternalMessage *pCurrentMsg; 514 }; 515 } // namespace Wifi 516 } // namespace OHOS 517 #endif 518