• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "finite_state_machine_builder.h"
16 
17 #include <future>
18 #include <mutex>
19 #include <queue>
20 #include <set>
21 #include <unordered_map>
22 
23 #include "iam_logger.h"
24 #include "iam_ptr.h"
25 
26 #define LOG_LABEL UserIam::Common::LABEL_FACE_AUTH_SA
27 namespace OHOS {
28 namespace UserIam {
29 namespace FaceAuth {
30 using namespace OHOS;
FiniteStateMachineBuilder(std::string name,uint32_t initialState)31 FiniteStateMachineBuilder::FiniteStateMachineBuilder(std::string name, uint32_t initialState)
32     : name_(std::move(name)),
33       initstate_(initialState),
34       valid_(true)
35 {
36 }
37 
38 FiniteStateMachineBuilder::~FiniteStateMachineBuilder() = default;
39 
MakeTransition(uint32_t state,uint32_t event,uint32_t nextState,const FiniteStateMachine::Action & action)40 std::shared_ptr<FiniteStateMachine::Builder> FiniteStateMachineBuilder::MakeTransition(uint32_t state, uint32_t event,
41     uint32_t nextState, const FiniteStateMachine::Action &action)
42 {
43     auto ret = transitionMap_.try_emplace(FiniteStateMachineImpl::GetTransitionIndex(state, event), nextState, action);
44     if (!ret.second) {
45         IAM_LOGE("%{public}s state %{public}u and event %{public}u insert failed", name_.c_str(), state, event);
46         valid_ = false;
47     }
48     return shared_from_this();
49 }
50 
MakeTransition(uint32_t state,uint32_t event,uint32_t nextState)51 std::shared_ptr<FiniteStateMachine::Builder> FiniteStateMachineBuilder::MakeTransition(uint32_t state, uint32_t event,
52     uint32_t nextState)
53 {
54     MakeTransition(state, event, nextState, nullptr);
55     return shared_from_this();
56 }
57 
MakeOnStateEnter(uint32_t state,const FiniteStateMachine::Action & action)58 std::shared_ptr<FiniteStateMachine::Builder> FiniteStateMachineBuilder::MakeOnStateEnter(uint32_t state,
59     const FiniteStateMachine::Action &action)
60 {
61     auto ret = enterMap_.try_emplace(state, action);
62     if (!ret.second) {
63         IAM_LOGE("%{public}s enter state action %{public}u insert failed", name_.c_str(), state);
64         valid_ = false;
65     }
66     return shared_from_this();
67 }
68 
MakeOnStateLeave(uint32_t state,const FiniteStateMachine::Action & action)69 std::shared_ptr<FiniteStateMachine::Builder> FiniteStateMachineBuilder::MakeOnStateLeave(uint32_t state,
70     const FiniteStateMachine::Action &action)
71 {
72     auto ret = leaveMap_.try_emplace(state, action);
73     if (!ret.second) {
74         IAM_LOGE("%{public}s leave state action %{public}u insert failed", name_.c_str(), state);
75         valid_ = false;
76     }
77     return shared_from_this();
78 }
79 
Build()80 std::shared_ptr<FiniteStateMachine> FiniteStateMachineBuilder::Build()
81 {
82     if (!valid_) {
83         IAM_LOGI("machine %{public}s builded failed", name_.c_str());
84         return nullptr;
85     }
86     valid_ = false;
87     return Common::MakeShared<FiniteStateMachineImpl>(name_, initstate_, transitionMap_, enterMap_, leaveMap_);
88 }
89 
New(const std::string & name,uint32_t initialState)90 std::shared_ptr<FiniteStateMachine::Builder> FiniteStateMachine::Builder::New(const std::string &name,
91     uint32_t initialState)
92 {
93     return Common::MakeShared<FiniteStateMachineBuilder>(name, initialState);
94 }
95 } // namespace FaceAuth
96 } // namespace UserIam
97 } // namespace OHOS
98