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_pool.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<ScheduleNode>,
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         uint32_t collectorTokenId {0};
44         PinSubType pinSubType {0};
45         uint64_t contextId {0};
46         uint64_t expiredTime {0};
47         std::vector<uint64_t> templateIdList {};
48         AuthType authType {PIN};
49         uint32_t executorMatcher {0};
50         ScheduleMode scheduleMode {AUTH};
51         std::weak_ptr<ResourceNode> collector;
52         std::weak_ptr<ResourceNode> verifier;
53         std::shared_ptr<ThreadHandler> threadHandler;
54         std::shared_ptr<ScheduleNodeCallback> callback;
55         bool endAfterFirstFail;
56         std::vector<uint8_t> collectorMessage;
57         std::vector<uint8_t> verifierMessage;
58         int32_t authIntent {0};
59         int32_t userId {0};
60     };
61     explicit ScheduleNodeImpl(ScheduleInfo &info);
62     ~ScheduleNodeImpl() override;
63     uint64_t GetScheduleId() const override;
64     uint64_t GetContextId() const override;
65     AuthType GetAuthType() const override;
66     uint64_t GetExecutorMatcher() const override;
67     ScheduleMode GetScheduleMode() const override;
68     std::weak_ptr<ResourceNode> GetCollectorExecutor() const override;
69     std::weak_ptr<ResourceNode> GetVerifyExecutor() const override;
70     std::optional<std::vector<uint64_t>> GetTemplateIdList() const override;
71     State GetCurrentScheduleState() const override;
72     std::shared_ptr<ScheduleNodeCallback> GetScheduleCallback() override;
73     int32_t GetAuthIntent() const override;
74     void ClearScheduleCallback() override;
75     bool StartSchedule() override;
76     bool StopSchedule() override;
77     bool StopSchedule(ResultCode errorCode) override;
78     bool SendMessage(ExecutorRole dstRole, const std::vector<uint8_t> &msg) override;
79     bool ContinueSchedule(ResultCode resultCode, const std::shared_ptr<Attributes> &finalResult) override;
80 
81 private:
82     std::shared_ptr<FiniteStateMachine> MakeFiniteStateMachine();
83     std::string GetDescription() const;
84     bool TryKickMachine(Event event);
85     void SetFwkResultCode(int32_t resultCode);
86     void SetExecutorResultCode(int32_t resultCode);
87     void SetScheduleResult(const std::shared_ptr<Attributes> &scheduleResult);
88     void StartTimer();
89     void StopTimer();
90     // fsm processes begins
91     void ProcessBeginVerifier(FiniteStateMachine &machine, uint32_t event);
92     void ProcessBeginCollector(FiniteStateMachine &machine, uint32_t event);
93     // fsm processes begins ack
94     void ProcessVerifierBeginFailed(FiniteStateMachine &machine, uint32_t event);
95     void ProcessCollectorBeginFailed(FiniteStateMachine &machine, uint32_t event);
96     // fsm processes wait
97     void ProcessScheduleResultReceived(FiniteStateMachine &machine, uint32_t event) const;
98     // fsm processes ends
99     void ProcessEndCollector(FiniteStateMachine &machine, uint32_t event);
100     void ProcessEndVerifier(FiniteStateMachine &machine, uint32_t event);
101 
102     void OnScheduleProcessing(FiniteStateMachine &machine, uint32_t event);
103     void OnScheduleFinished(FiniteStateMachine &machine, uint32_t event);
104 
105     void GetScheduleAttribute(bool isVerifier, Attributes &attribute) const;
106 
107     void NotifyCollectorReady();
108     uint32_t timerId_ {0};
109     // members
110     ScheduleInfo info_;
111     std::shared_ptr<FiniteStateMachine> machine_;
112     std::mutex mutex_;
113     std::shared_ptr<IamHitraceHelper> iamHitraceHelper_;
114     // result
115     int32_t executorResultCode_ {GENERAL_ERROR};
116     std::optional<int32_t> fwkResultCode_ {std::nullopt};
117     std::shared_ptr<Attributes> scheduleResult_ {nullptr};
118     std::shared_ptr<ResourceNodePool::ResourceNodePoolListener> resourceNodePoolListener_ {nullptr};
119 };
120 } // namespace UserAuth
121 } // namespace UserIam
122 } // namespace OHOS
123 #endif // IAM_SCHEDULE_NODE_IMPL_H