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 "executor_callback_proxy.h"
17
18 #include "iam_logger.h"
19 #include "iam_common_defines.h"
20 #include "message_parcel.h"
21
22 #define LOG_TAG "USER_AUTH_SA"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
OnMessengerReady(sptr<ExecutorMessengerInterface> & messenger,const std::vector<uint8_t> & publicKey,const std::vector<uint64_t> & templateIdList)27 void ExecutorCallbackProxy::OnMessengerReady(sptr<ExecutorMessengerInterface> &messenger,
28 const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIdList)
29 {
30 if (messenger == nullptr) {
31 IAM_LOGE("messenger is nullptr");
32 return;
33 }
34 MessageParcel data;
35 MessageParcel reply;
36
37 if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
38 IAM_LOGE("failed to write descriptor");
39 return;
40 }
41
42 if (!data.WriteRemoteObject(messenger->AsObject())) {
43 IAM_LOGE("failed to write messenger failed");
44 return;
45 }
46 if (!data.WriteUInt8Vector(publicKey)) {
47 IAM_LOGE("failed to write publicKey");
48 return;
49 }
50 if (!data.WriteUInt64Vector(templateIdList)) {
51 IAM_LOGE("failed to write templateIdList");
52 return;
53 }
54
55 bool result = SendRequest(ExecutorCallbackInterfaceCode::ON_MESSENGER_READY, data, reply);
56 if (!result) {
57 IAM_LOGE("send request failed");
58 return;
59 }
60 }
61
OnBeginExecute(uint64_t scheduleId,const std::vector<uint8_t> & publicKey,const Attributes & command)62 int32_t ExecutorCallbackProxy::OnBeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
63 const Attributes &command)
64 {
65 MessageParcel data;
66 MessageParcel reply;
67
68 if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
69 IAM_LOGE("write descriptor failed");
70 return GENERAL_ERROR;
71 }
72 if (!data.WriteUint64(scheduleId)) {
73 IAM_LOGE("write scheduleId failed");
74 return GENERAL_ERROR;
75 }
76 if (!data.WriteUInt8Vector(publicKey)) {
77 IAM_LOGE("write publicKey failed");
78 return GENERAL_ERROR;
79 }
80 auto attr = command.Serialize();
81 if (!data.WriteUInt8Vector(attr)) {
82 IAM_LOGE("write command failed");
83 return GENERAL_ERROR;
84 }
85
86 bool ret = SendRequest(ExecutorCallbackInterfaceCode::ON_BEGIN_EXECUTE, data, reply);
87 if (!ret) {
88 IAM_LOGE("send request failed");
89 return GENERAL_ERROR;
90 }
91 int32_t result = GENERAL_ERROR;
92 if (!reply.ReadInt32(result)) {
93 IAM_LOGE("read request result failed");
94 return GENERAL_ERROR;
95 }
96 return result;
97 }
98
OnEndExecute(uint64_t scheduleId,const Attributes & command)99 int32_t ExecutorCallbackProxy::OnEndExecute(uint64_t scheduleId, const Attributes &command)
100 {
101 MessageParcel data;
102 MessageParcel reply;
103
104 if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
105 IAM_LOGE("write descriptor failed");
106 return GENERAL_ERROR;
107 }
108 if (!data.WriteUint64(scheduleId)) {
109 IAM_LOGE("write scheduleId failed");
110 return GENERAL_ERROR;
111 }
112 auto attr = command.Serialize();
113 if (!data.WriteUInt8Vector(attr)) {
114 IAM_LOGE("write command failed");
115 return GENERAL_ERROR;
116 }
117
118 bool ret = SendRequest(ExecutorCallbackInterfaceCode::ON_END_EXECUTE, data, reply);
119 if (!ret) {
120 IAM_LOGE("send request failed");
121 return GENERAL_ERROR;
122 }
123 int32_t result = GENERAL_ERROR;
124 if (!reply.ReadInt32(result)) {
125 IAM_LOGE("read request result failed");
126 return GENERAL_ERROR;
127 }
128 return result;
129 }
130
OnSetProperty(const Attributes & properties)131 int32_t ExecutorCallbackProxy::OnSetProperty(const Attributes &properties)
132 {
133 MessageParcel data;
134 MessageParcel reply;
135
136 if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
137 IAM_LOGE("write descriptor failed");
138 return GENERAL_ERROR;
139 }
140 auto attr = properties.Serialize();
141 if (!data.WriteUInt8Vector(attr)) {
142 IAM_LOGE("write properties failed");
143 return GENERAL_ERROR;
144 }
145
146 bool ret = SendRequest(ExecutorCallbackInterfaceCode::ON_SET_PROPERTY, data, reply);
147 if (!ret) {
148 IAM_LOGE("send request failed");
149 return GENERAL_ERROR;
150 }
151 int32_t result = GENERAL_ERROR;
152 if (!reply.ReadInt32(result)) {
153 IAM_LOGE("read request result failed");
154 return GENERAL_ERROR;
155 }
156 return result;
157 }
158
OnGetProperty(const Attributes & condition,Attributes & values)159 int32_t ExecutorCallbackProxy::OnGetProperty(const Attributes &condition, Attributes &values)
160 {
161 MessageParcel data;
162 MessageParcel reply;
163
164 if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
165 IAM_LOGE("write descriptor failed");
166 return GENERAL_ERROR;
167 }
168
169 if (!data.WriteUInt8Vector(condition.Serialize())) {
170 IAM_LOGE("write condition failed");
171 return GENERAL_ERROR;
172 }
173
174 bool ret = SendRequest(ExecutorCallbackInterfaceCode::ON_GET_PROPERTY, data, reply);
175 if (!ret) {
176 IAM_LOGE("send request failed");
177 return GENERAL_ERROR;
178 }
179 int32_t result = GENERAL_ERROR;
180 if (!reply.ReadInt32(result)) {
181 IAM_LOGE("read request result failed");
182 return GENERAL_ERROR;
183 }
184
185 std::vector<uint8_t> attr;
186 if (!reply.ReadUInt8Vector(&attr)) {
187 IAM_LOGE("read reply values failed");
188 return GENERAL_ERROR;
189 }
190 values = Attributes(attr);
191 return result;
192 }
193
OnSendData(uint64_t scheduleId,const Attributes & data)194 int32_t ExecutorCallbackProxy::OnSendData(uint64_t scheduleId, const Attributes &data)
195 {
196 MessageParcel dataParcel;
197 MessageParcel reply;
198
199 if (!dataParcel.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
200 IAM_LOGE("write descriptor failed");
201 return GENERAL_ERROR;
202 }
203 if (!dataParcel.WriteUint64(scheduleId)) {
204 IAM_LOGE("write scheduleId failed");
205 return GENERAL_ERROR;
206 }
207 auto attr = data.Serialize();
208 if (!dataParcel.WriteUInt8Vector(attr)) {
209 IAM_LOGE("write data failed");
210 return GENERAL_ERROR;
211 }
212
213 bool ret = SendRequest(ExecutorCallbackInterfaceCode::ON_SEND_DATA, dataParcel, reply);
214 if (!ret) {
215 IAM_LOGE("send request failed");
216 return GENERAL_ERROR;
217 }
218 int32_t result = GENERAL_ERROR;
219 if (!reply.ReadInt32(result)) {
220 IAM_LOGE("read request result failed");
221 return GENERAL_ERROR;
222 }
223 return result;
224 }
225
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)226 bool ExecutorCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
227 {
228 IAM_LOGI("code = %{public}u", code);
229 sptr<IRemoteObject> remote = Remote();
230 if (remote == nullptr) {
231 IAM_LOGE("get remote failed");
232 return false;
233 }
234 MessageOption option(MessageOption::TF_SYNC);
235 int32_t result = remote->SendRequest(code, data, reply, option);
236 if (result != OHOS::NO_ERROR) {
237 IAM_LOGE("send request failed, code = %{public}u, result = %{public}d", code, result);
238 return false;
239 }
240 return true;
241 }
242 } // namespace UserAuth
243 } // namespace UserIam
244 } // namespace OHOS