1 /* 2 * Copyright (c) 2023 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 IAM_FINITE_STATE_MACHINE_IMPL_H 17 #define IAM_FINITE_STATE_MACHINE_IMPL_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <mutex> 22 #include <queue> 23 #include <string> 24 #include <unordered_map> 25 26 #include "nocopyable.h" 27 #include "safe_queue.h" 28 29 #include "finite_state_machine.h" 30 #include "thread_handler.h" 31 32 namespace OHOS { 33 namespace UserIam { 34 namespace FaceAuth { 35 class FiniteStateMachineImpl final : public FiniteStateMachine, 36 public std::enable_shared_from_this<FiniteStateMachineImpl>, 37 public NoCopyable { 38 public: 39 class Inner; 40 using TransitionMap = std::unordered_map<uint64_t, std::pair<uint32_t, FiniteStateMachine::Action>>; 41 using EnterMap = std::unordered_map<uint32_t, FiniteStateMachine::Action>; 42 using LeaveMap = std::unordered_map<uint32_t, FiniteStateMachine::Action>; 43 44 FiniteStateMachineImpl(std::string name, uint32_t initialState, TransitionMap &transitionMap, EnterMap &enterMap, 45 LeaveMap &leaveMap); 46 ~FiniteStateMachineImpl() override; 47 48 void Schedule(uint32_t event) override; 49 uint32_t GetCurrentState() const override; 50 uint32_t EnsureCurrentState() override; 51 void SetThreadHandler(const std::shared_ptr<ThreadHandler> &threadHandler) override; 52 const std::string &GetMachineName() const override; 53 friend class FiniteStateMachineBuilder; 54 55 private: 56 static constexpr uint32_t MAX_SCHEDULE_TIMES = 100; GetTransitionIndex(uint32_t state,uint32_t event)57 constexpr static inline uint64_t GetTransitionIndex(uint32_t state, uint32_t event) 58 { 59 constexpr uint32_t uint32WidthLen = 32; 60 return (static_cast<uint64_t>(state) << uint32WidthLen) | event; 61 } 62 void ScheduleInner(FiniteStateMachine &machine); 63 void DealWithStateLeaveAndEnter(FiniteStateMachine &machine, uint32_t oldState, uint32_t newState); 64 65 const std::string name_; 66 uint32_t currentState_; 67 std::shared_ptr<ThreadHandler> threadHandler_; 68 TransitionMap transitionMap_; 69 FiniteStateMachineImpl::EnterMap enterMap_; 70 FiniteStateMachineImpl::LeaveMap leaveMap_; 71 72 std::mutex mutex_; 73 SafeQueue<uint32_t> pendingEvents_ {}; 74 }; 75 76 class FiniteStateMachineImpl::Inner final : public FiniteStateMachine, public NoCopyable { 77 public: 78 explicit Inner(std::shared_ptr<FiniteStateMachineImpl> &machine); 79 ~Inner() override = default; 80 void Schedule(uint32_t event) override; 81 uint32_t GetCurrentState() const override; 82 uint32_t EnsureCurrentState() override; 83 const std::string &GetMachineName() const override; 84 void SetThreadHandler(const std::shared_ptr<ThreadHandler> &threadHandler) override; 85 86 private: 87 std::shared_ptr<FiniteStateMachineImpl> &machine_; 88 }; 89 } // namespace FaceAuth 90 } // namespace UserIam 91 } // namespace OHOS 92 93 #endif // IAM_FINITE_STATE_MACHINE_IMPL_H