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 bool endAfterFirstFail; 56 std::vector<uint8_t> extraInfo; 57 }; 58 explicit ScheduleNodeImpl(ScheduleInfo &info); 59 ~ScheduleNodeImpl() override = default; 60 uint64_t GetScheduleId() const override; 61 uint64_t GetContextId() const override; 62 AuthType GetAuthType() const override; 63 uint64_t GetExecutorMatcher() const override; 64 ScheduleMode GetScheduleMode() const override; 65 std::weak_ptr<ResourceNode> GetCollectorExecutor() const override; 66 std::weak_ptr<ResourceNode> GetVerifyExecutor() const override; 67 std::optional<std::vector<uint64_t>> GetTemplateIdList() const override; 68 State GetCurrentScheduleState() const override; 69 std::shared_ptr<ScheduleNodeCallback> GetScheduleCallback() override; 70 void ClearScheduleCallback() override; 71 bool StartSchedule() override; 72 bool StopSchedule() override; 73 bool ContinueSchedule(ExecutorRole srcRole, ExecutorRole dstRole, uint64_t transNum, 74 const std::vector<uint8_t> &msg) override; 75 bool ContinueSchedule(ResultCode resultCode, const std::shared_ptr<Attributes> &finalResult) override; 76 77 private: 78 std::shared_ptr<FiniteStateMachine> MakeFiniteStateMachine(); 79 std::string GetDescription() const; 80 bool TryKickMachine(Event event); 81 void SetFwkResultCode(int32_t resultCode); 82 void SetExecutorResultCode(int32_t resultCode); 83 void SetScheduleResult(const std::shared_ptr<Attributes> &scheduleResult); 84 void StartTimer(); 85 void StopTimer(); 86 // fsm processes begins 87 void ProcessBeginVerifier(FiniteStateMachine &machine, uint32_t event); 88 void ProcessBeginCollector(FiniteStateMachine &machine, uint32_t event); 89 // fsm processes begins ack 90 void ProcessVerifierBeginFailed(FiniteStateMachine &machine, uint32_t event); 91 void ProcessCollectorBeginFailed(FiniteStateMachine &machine, uint32_t event); 92 // fsm processes wait 93 void ProcessScheduleResultReceived(FiniteStateMachine &machine, uint32_t event) const; 94 // fsm processes ends 95 void ProcessEndCollector(FiniteStateMachine &machine, uint32_t event); 96 void ProcessEndVerifier(FiniteStateMachine &machine, uint32_t event); 97 98 void OnScheduleProcessing(FiniteStateMachine &machine, uint32_t event); 99 void OnScheduleFinished(FiniteStateMachine &machine, uint32_t event); 100 uint32_t timerId_ {0}; 101 // members 102 ScheduleInfo info_; 103 std::shared_ptr<FiniteStateMachine> machine_; 104 std::mutex mutex_; 105 std::shared_ptr<IamHitraceHelper> iamHitraceHelper_; 106 // result 107 int32_t executorResultCode_ {GENERAL_ERROR}; 108 std::optional<int32_t> fwkResultCode_ {std::nullopt}; 109 std::shared_ptr<Attributes> scheduleResult_ {nullptr}; 110 }; 111 } // namespace UserAuth 112 } // namespace UserIam 113 } // namespace OHOS 114 #endif // IAM_SCHEDULE_NODE_IMPL_H