1 /*
2 * Copyright (c) 2022-2023 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 "co_auth_proxy.h"
17
18 #include <cinttypes>
19
20 #include "iam_common_defines.h"
21 #include "iam_logger.h"
22
23 #define LOG_LABEL UserIam::Common::LABEL_AUTH_EXECUTOR_MGR_SDK
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
CoAuthProxy(const sptr<IRemoteObject> & impl)28 CoAuthProxy::CoAuthProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<CoAuthInterface>(impl)
29 {
30 }
31
WriteExecutorInfo(const ExecutorRegisterInfo & info,MessageParcel & data)32 int32_t CoAuthProxy::WriteExecutorInfo(const ExecutorRegisterInfo &info, MessageParcel &data)
33 {
34 if (!data.WriteInt32(info.authType)) {
35 IAM_LOGE("failed to write authType");
36 return WRITE_PARCEL_ERROR;
37 }
38 if (!data.WriteInt32(info.executorRole)) {
39 IAM_LOGE("failed to write executorRole");
40 return WRITE_PARCEL_ERROR;
41 }
42 if (!data.WriteUint32(info.executorSensorHint)) {
43 IAM_LOGE("failed to write executorSensorHint");
44 return WRITE_PARCEL_ERROR;
45 }
46 if (!data.WriteUint32(info.executorMatcher)) {
47 IAM_LOGE("failed to write executorMatcher");
48 return WRITE_PARCEL_ERROR;
49 }
50 if (!data.WriteUint32(info.esl)) {
51 IAM_LOGE("failed to write esl");
52 return WRITE_PARCEL_ERROR;
53 }
54 if (!data.WriteUInt8Vector(info.publicKey)) {
55 IAM_LOGE("failed to write publicKey");
56 return WRITE_PARCEL_ERROR;
57 }
58 return SUCCESS;
59 }
60
ExecutorRegister(const ExecutorRegisterInfo & info,sptr<ExecutorCallbackInterface> & callback)61 uint64_t CoAuthProxy::ExecutorRegister(const ExecutorRegisterInfo &info, sptr<ExecutorCallbackInterface> &callback)
62 {
63 IAM_LOGI("start");
64 const uint64_t BAD_CONTEXT_ID = 0;
65 if (callback == nullptr) {
66 IAM_LOGE("callback is nullptr");
67 return BAD_CONTEXT_ID;
68 }
69 MessageParcel data;
70 MessageParcel reply;
71
72 if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) {
73 IAM_LOGE("failed to write descriptor");
74 return BAD_CONTEXT_ID;
75 }
76 if (WriteExecutorInfo(info, data) != SUCCESS) {
77 IAM_LOGE("failed to write executor info");
78 return BAD_CONTEXT_ID;
79 }
80 if (!data.WriteRemoteObject(callback->AsObject())) {
81 IAM_LOGE("failed to write callback");
82 return BAD_CONTEXT_ID;
83 }
84
85 bool ret = SendRequest(CoAuthInterfaceCode::CO_AUTH_EXECUTOR_REGISTER, data, reply);
86 if (!ret) {
87 IAM_LOGE("failed to send request");
88 return BAD_CONTEXT_ID;
89 }
90 uint64_t result = 0;
91 if (!reply.ReadUint64(result)) {
92 IAM_LOGE("failed to read result");
93 return BAD_CONTEXT_ID;
94 }
95 return result;
96 }
97
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)98 bool CoAuthProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
99 {
100 IAM_LOGI("code = %{public}u", code);
101 sptr<IRemoteObject> remote = Remote();
102 if (remote == nullptr) {
103 IAM_LOGE("failed to get remote");
104 return false;
105 }
106 MessageOption option(MessageOption::TF_SYNC);
107 int32_t result = remote->SendRequest(code, data, reply, option);
108 if (result != OHOS::NO_ERROR) {
109 IAM_LOGE("failed to send request, result = %{public}d", result);
110 return false;
111 }
112 return true;
113 }
114 } // namespace UserAuth
115 } // namespace UserIam
116 } // namespace OHOS