1 /*
2 * Copyright (c) 2024 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 #include "remote_iam_callback.h"
17
18 #include "iam_check.h"
19 #include "iam_ptr.h"
20 #include "remote_connect_manager.h"
21 #include "remote_msg_util.h"
22
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
RemoteIamCallback(std::string & connectionName)26 RemoteIamCallback::RemoteIamCallback(std::string &connectionName) : connectionName_(connectionName)
27 {
28 endPointName_ = REMOTE_CALLBACK_ENDPOINT_NAME;
29 }
30
~RemoteIamCallback()31 RemoteIamCallback::~RemoteIamCallback()
32 {
33 }
34
OnResult(int32_t resultCode,const std::vector<uint8_t> & extraInfo)35 int32_t RemoteIamCallback::OnResult(int32_t resultCode, const std::vector<uint8_t> &extraInfo)
36 {
37 IAM_LOGI("result: %{public}d", resultCode);
38
39 std::shared_ptr<Attributes> request = Common::MakeShared<Attributes>(extraInfo);
40 IF_FALSE_LOGE_AND_RETURN_VAL(request != nullptr, GENERAL_ERROR);
41
42 bool setMsgTypeRet = request->SetInt32Value(Attributes::ATTR_MSG_TYPE, SEND_REMOTE_AUTH_RESULT);
43 IF_FALSE_LOGE_AND_RETURN_VAL(setMsgTypeRet, GENERAL_ERROR);
44
45 bool setResultRet = request->SetInt32Value(Attributes::ATTR_RESULT, resultCode);
46 IF_FALSE_LOGE_AND_RETURN_VAL(setResultRet, GENERAL_ERROR);
47
48 MsgCallback msgCallback = [](const std::shared_ptr<Attributes> &) { IAM_LOGI("message sent"); };
49
50 ResultCode sendMsgRet = RemoteConnectionManager::GetInstance().SendMessage(connectionName_, endPointName_,
51 REMOTE_AUTH_INVOKER_CONTEXT_ENDPOINT_NAME, request, msgCallback);
52 IF_FALSE_LOGE_AND_RETURN_VAL(sendMsgRet == ResultCode::SUCCESS, GENERAL_ERROR);
53
54 IAM_LOGI("success");
55 return SUCCESS;
56 }
57
OnAcquireInfo(int32_t module,int32_t acquireInfo,const std::vector<uint8_t> & extraInfo)58 int32_t RemoteIamCallback::OnAcquireInfo(int32_t module, int32_t acquireInfo,
59 const std::vector<uint8_t> &extraInfo)
60 {
61 IAM_LOGI("module: %{public}d, acquireInfo: %{public}d", module, acquireInfo);
62
63 std::shared_ptr<Attributes> request = Common::MakeShared<Attributes>(extraInfo);
64 IF_FALSE_LOGE_AND_RETURN_VAL(request != nullptr, GENERAL_ERROR);
65
66 bool setMsgTypeRet = request->SetInt32Value(Attributes::ATTR_MSG_TYPE, SEND_REMOTE_AUTH_TIP);
67 IF_FALSE_LOGE_AND_RETURN_VAL(setMsgTypeRet, GENERAL_ERROR);
68
69 bool setModuleRet = request->SetInt32Value(Attributes::ATTR_DEST_ROLE, module);
70 IF_FALSE_LOGE_AND_RETURN_VAL(setModuleRet, GENERAL_ERROR);
71
72 bool setAcquireInfoRet = request->SetInt32Value(Attributes::ATTR_TIP_INFO, acquireInfo);
73 IF_FALSE_LOGE_AND_RETURN_VAL(setAcquireInfoRet, GENERAL_ERROR);
74
75 MsgCallback msgCallback = [](const std::shared_ptr<Attributes> &) { IAM_LOGI("message sent"); };
76
77 ResultCode sendMsgRet = RemoteConnectionManager::GetInstance().SendMessage(connectionName_, endPointName_,
78 REMOTE_AUTH_INVOKER_CONTEXT_ENDPOINT_NAME, request, msgCallback);
79 IF_FALSE_LOGE_AND_RETURN_VAL(sendMsgRet == ResultCode::SUCCESS, GENERAL_ERROR);
80
81 IAM_LOGI("success");
82 return SUCCESS;
83 }
84
AsObject()85 sptr<IRemoteObject> RemoteIamCallback::AsObject()
86 {
87 return nullptr;
88 }
89 } // namespace UserAuth
90 } // namespace UserIam
91 } // namespace OHOS