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 #include "base_context.h"
16
17 #include <sstream>
18
19 #include "iam_check.h"
20 #include "iam_logger.h"
21 #include "iam_para2str.h"
22
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
BaseContext(const std::string & type,uint64_t contextId,std::shared_ptr<ContextCallback> callback)27 BaseContext::BaseContext(const std::string &type, uint64_t contextId, std::shared_ptr<ContextCallback> callback)
28 : callback_(callback),
29 contextId_(contextId)
30 {
31 std::ostringstream ss;
32 ss << "Context(type:" << type << ", contextId:" << GET_MASKED_STRING(contextId_) << ")";
33 description_ = ss.str();
34 }
35
SetLatestError(int32_t error)36 void BaseContext::SetLatestError(int32_t error)
37 {
38 if (error != ResultCode::SUCCESS) {
39 latestError_ = error;
40 }
41 }
42
GetLatestError() const43 int32_t BaseContext::GetLatestError() const
44 {
45 return latestError_;
46 }
47
GetContextId() const48 uint64_t BaseContext::GetContextId() const
49 {
50 return contextId_;
51 }
52
Start()53 bool BaseContext::Start()
54 {
55 std::lock_guard<std::mutex> guard(mutex_);
56 IAM_LOGI("%{public}s start", GetDescription());
57 if (hasStarted_) {
58 IAM_LOGI("%{public}s context has started, cannot start again", GetDescription());
59 return false;
60 }
61 hasStarted_ = true;
62 return OnStart();
63 }
64
Stop()65 bool BaseContext::Stop()
66 {
67 IAM_LOGI("%{public}s start", GetDescription());
68 return OnStop();
69 }
70
GetScheduleNode(uint64_t scheduleId) const71 std::shared_ptr<ScheduleNode> BaseContext::GetScheduleNode(uint64_t scheduleId) const
72 {
73 for (auto const &schedule : scheduleList_) {
74 if (schedule == nullptr) {
75 continue;
76 }
77 if (schedule->GetScheduleId() == scheduleId) {
78 return schedule;
79 }
80 }
81 return nullptr;
82 }
83
OnScheduleStarted()84 void BaseContext::OnScheduleStarted()
85 {
86 IAM_LOGI("%{public}s start", GetDescription());
87 }
88
OnScheduleProcessed(ExecutorRole src,int32_t moduleType,const std::vector<uint8_t> & acquireMsg)89 void BaseContext::OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg)
90 {
91 IAM_LOGI("%{public}s start", GetDescription());
92 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
93 callback_->OnAcquireInfo(src, moduleType, acquireMsg);
94 }
95
OnScheduleStoped(int32_t resultCode,const std::shared_ptr<Attributes> & finalResult)96 void BaseContext::OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult)
97 {
98 OnResult(resultCode, finalResult);
99 return;
100 }
101
GetDescription() const102 const char *BaseContext::GetDescription() const
103 {
104 return description_.c_str();
105 }
106 } // namespace UserAuth
107 } // namespace UserIam
108 } // namespace OHOS
109