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 bool StartSchedule() override; 70 bool StopSchedule() override; 71 bool ContinueSchedule(ExecutorRole srcRole, ExecutorRole dstRole, uint64_t transNum, 72 const std::vector<uint8_t> &msg) override; 73 bool ContinueSchedule(ResultCode resultCode, const std::shared_ptr<Attributes> &finalResult) override; 74 75 private: 76 std::shared_ptr<FiniteStateMachine> MakeFiniteStateMachine(); 77 std::string GetDescription() const; 78 bool TryKickMachine(Event event); 79 void SetFwkResultCode(int32_t resultCode); 80 void SetExecutorResultCode(int32_t resultCode); 81 void SetScheduleResult(const std::shared_ptr<Attributes> &scheduleResult); 82 void StartTimer(); 83 void StopTimer(); 84 // fsm processes begins 85 void ProcessBeginVerifier(FiniteStateMachine &machine, uint32_t event); 86 void ProcessBeginCollector(FiniteStateMachine &machine, uint32_t event); 87 // fsm processes begins ack 88 void ProcessVerifierBeginFailed(FiniteStateMachine &machine, uint32_t event); 89 void ProcessCollectorBeginFailed(FiniteStateMachine &machine, uint32_t event); 90 // fsm processes wait 91 void ProcessScheduleResultReceived(FiniteStateMachine &machine, uint32_t event) const; 92 // fsm processes ends 93 void ProcessEndCollector(FiniteStateMachine &machine, uint32_t event); 94 void ProcessEndVerifier(FiniteStateMachine &machine, uint32_t event); 95 96 void OnScheduleProcessing(FiniteStateMachine &machine, uint32_t event) const; 97 void OnScheduleFinished(FiniteStateMachine &machine, uint32_t event); 98 uint32_t timerId_ {0}; 99 // members 100 ScheduleInfo info_; 101 std::shared_ptr<FiniteStateMachine> machine_; 102 std::mutex mutex_; 103 std::shared_ptr<IamHitraceHelper> iamHitraceHelper_; 104 // result 105 int32_t executorResultCode_ {GENERAL_ERROR}; 106 std::optional<int32_t> fwkResultCode_ {std::nullopt}; 107 std::shared_ptr<Attributes> scheduleResult_ {nullptr}; 108 }; 109 } // namespace UserAuth 110 } // namespace UserIam 111 } // namespace OHOS 112 #endif // IAM_SCHEDULE_NODE_IMPL_H