• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_SCHEDULE_NODE_IMPL_H
17 #define IAM_SCHEDULE_NODE_IMPL_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <optional>
23 
24 #include "iam_hitrace_helper.h"
25 
26 #include "finite_state_machine.h"
27 #include "resource_node.h"
28 #include "schedule_node.h"
29 #include "schedule_node_callback.h"
30 
31 namespace OHOS {
32 namespace UserIam {
33 namespace UserAuth {
34 class ScheduleNodeImpl final : public ScheduleNode,
35                                public std::enable_shared_from_this<ScheduleNodeImpl>,
36                                public NoCopyable {
37 public:
38     friend class ScheduleNodeBuilder;
39     class Inner;
40     struct ScheduleInfo {
41         uint64_t scheduleId {0};
42         std::optional<uint32_t> tokenId;
43         PinSubType pinSubType {0};
44         uint64_t contextId {0};
45         uint64_t expiredTime {0};
46         std::vector<uint64_t> templateIdList {};
47         AuthType authType {PIN};
48         uint32_t executorMatcher {0};
49         ScheduleMode scheduleMode {AUTH};
50         std::weak_ptr<ResourceNode> collector;
51         std::weak_ptr<ResourceNode> verifier;
52         std::shared_ptr<ThreadHandler> threadHandler;
53         std::shared_ptr<ScheduleNodeCallback> callback;
54         std::shared_ptr<Attributes> parameters;
55     };
56     explicit ScheduleNodeImpl(ScheduleInfo &info);
57     ~ScheduleNodeImpl() override = default;
58     uint64_t GetScheduleId() const override;
59     uint64_t GetContextId() const override;
60     AuthType GetAuthType() const override;
61     uint64_t GetExecutorMatcher() const override;
62     ScheduleMode GetScheduleMode() const override;
63     std::weak_ptr<ResourceNode> GetCollectorExecutor() const override;
64     std::weak_ptr<ResourceNode> GetVerifyExecutor() const override;
65     std::optional<std::vector<uint64_t>> GetTemplateIdList() const override;
66     State GetCurrentScheduleState() const override;
67     bool StartSchedule() override;
68     bool StopSchedule() override;
69     bool ContinueSchedule(ExecutorRole srcRole, ExecutorRole dstRole, uint64_t transNum,
70         const std::vector<uint8_t> &msg) override;
71     bool ContinueSchedule(ResultCode resultCode, const std::shared_ptr<Attributes> &finalResult) override;
72 
73 private:
74     std::shared_ptr<FiniteStateMachine> MakeFiniteStateMachine();
75     std::string GetDescription() const;
76     bool TryKickMachine(Event event);
77     void SetFwkResultCode(int32_t resultCode);
78     void SetExecutorResultCode(int32_t resultCode);
79     void SetScheduleResult(const std::shared_ptr<Attributes> &scheduleResult);
80     void StartTimer();
81     void StopTimer();
82     // fsm processes begins
83     void ProcessBeginVerifier(FiniteStateMachine &machine, uint32_t event);
84     void ProcessBeginCollector(FiniteStateMachine &machine, uint32_t event);
85     // fsm processes begins ack
86     void ProcessVerifierBeginFailed(FiniteStateMachine &machine, uint32_t event);
87     void ProcessCollectorBeginFailed(FiniteStateMachine &machine, uint32_t event);
88     // fsm processes wait
89     void ProcessScheduleResultReceived(FiniteStateMachine &machine, uint32_t event) const;
90     // fsm processes ends
91     void ProcessEndCollector(FiniteStateMachine &machine, uint32_t event);
92     void ProcessEndVerifier(FiniteStateMachine &machine, uint32_t event);
93 
94     void OnScheduleProcessing(FiniteStateMachine &machine, uint32_t event) const;
95     void OnScheduleFinished(FiniteStateMachine &machine, uint32_t event);
96     uint32_t timerId_ {0};
97     // members
98     ScheduleInfo info_;
99     std::shared_ptr<FiniteStateMachine> machine_;
100     std::mutex mutex_;
101     std::shared_ptr<IamHitraceHelper> iamHitraceHelper_;
102     // result
103     int32_t executorResultCode_ {GENERAL_ERROR};
104     std::optional<int32_t> fwkResultCode_ {std::nullopt};
105     std::shared_ptr<Attributes> scheduleResult_ {nullptr};
106 };
107 } // namespace UserAuth
108 } // namespace UserIam
109 } // namespace OHOS
110 #endif // IAM_SCHEDULE_NODE_IMPL_H