• 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 #include "identify_context.h"
16 
17 #include "iam_check.h"
18 #include "iam_logger.h"
19 #include "iam_ptr.h"
20 #include "schedule_node.h"
21 #include "schedule_node_callback.h"
22 
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
IdentifyContext(uint64_t contextId,std::shared_ptr<Identification> identify,std::shared_ptr<ContextCallback> callback)27 IdentifyContext::IdentifyContext(uint64_t contextId, std::shared_ptr<Identification> identify,
28     std::shared_ptr<ContextCallback> callback)
29     : BaseContext("Identify", contextId, callback),
30       identify_(identify)
31 {
32 }
33 
GetContextType() const34 ContextType IdentifyContext::GetContextType() const
35 {
36     return CONTEXT_IDENTIFY;
37 }
38 
OnStart()39 bool IdentifyContext::OnStart()
40 {
41     IAM_LOGI("%{public}s start", GetDescription());
42     IF_FALSE_LOGE_AND_RETURN_VAL(identify_ != nullptr, false);
43     bool startRet = identify_->Start(scheduleList_, shared_from_this());
44     if (!startRet) {
45         IAM_LOGE("%{public}s identify start fail", GetDescription());
46         SetLatestError(identify_->GetLatestError());
47         return startRet;
48     }
49     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
50     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
51     bool startScheduleRet = scheduleList_[0]->StartSchedule();
52     IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
53     IAM_LOGI("%{public}s success", GetDescription());
54     return true;
55 }
56 
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)57 void IdentifyContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
58 {
59     IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
60     Identification::IdentifyResultInfo resultInfo = {};
61     bool updateRet = UpdateScheduleResult(scheduleResultAttr, resultInfo);
62     if (!updateRet) {
63         IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
64         if (resultCode == SUCCESS) {
65             resultCode = GetLatestError();
66         }
67         resultInfo.result = resultCode;
68     }
69     InvokeResultCallback(resultInfo);
70     IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
71 }
72 
OnStop()73 bool IdentifyContext::OnStop()
74 {
75     IAM_LOGI("%{public}s start", GetDescription());
76     if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
77         scheduleList_[0]->StopSchedule();
78     }
79 
80     IF_FALSE_LOGE_AND_RETURN_VAL(identify_ != nullptr, false);
81     bool cancelRet = identify_->Cancel();
82     if (!cancelRet) {
83         IAM_LOGE("%{public}s identify cancel fail", GetDescription());
84         SetLatestError(identify_->GetLatestError());
85         return cancelRet;
86     }
87     return true;
88 }
89 
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,Identification::IdentifyResultInfo & resultInfo)90 bool IdentifyContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
91     Identification::IdentifyResultInfo &resultInfo)
92 {
93     IF_FALSE_LOGE_AND_RETURN_VAL(identify_ != nullptr, false);
94     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
95     std::vector<uint8_t> scheduleResult;
96     bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
97     IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
98     bool updateRet = identify_->Update(scheduleResult, resultInfo);
99     if (!updateRet) {
100         IAM_LOGE("%{public}s identify update fail", GetDescription());
101         SetLatestError(identify_->GetLatestError());
102         return updateRet;
103     }
104     return true;
105 }
106 
InvokeResultCallback(const Identification::IdentifyResultInfo & resultInfo) const107 void IdentifyContext::InvokeResultCallback(const Identification::IdentifyResultInfo &resultInfo) const
108 {
109     IAM_LOGI("%{public}s start", GetDescription());
110     IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
111     Attributes finalResult;
112     bool setResultCodeRet = finalResult.SetInt32Value(Attributes::ATTR_RESULT_CODE, resultInfo.result);
113     IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
114     bool setUserIdRet = finalResult.SetInt32Value(Attributes::ATTR_USER_ID, resultInfo.userId);
115     IF_FALSE_LOGE_AND_RETURN(setUserIdRet == true);
116     if (resultInfo.token.size() != 0) {
117         bool setSignatureResult = finalResult.SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, resultInfo.token);
118         IF_FALSE_LOGE_AND_RETURN(setSignatureResult == true);
119     }
120 
121     callback_->OnResult(resultInfo.result, finalResult);
122     IAM_LOGI("%{public}s invoke result callback success", GetDescription());
123 }
124 } // namespace UserAuth
125 } // namespace UserIam
126 } // namespace OHOS
127