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
16 #include "executor_callback_stub.h"
17
18 #include "executor_messenger_proxy.h"
19 #include "iam_common_defines.h"
20 #include "iam_logger.h"
21
22 #define LOG_LABEL UserIam::Common::LABEL_AUTH_EXECUTOR_MGR_SDK
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)27 int32_t ExecutorCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
28 MessageParcel &reply, MessageOption &option)
29 {
30 IAM_LOGD("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
31 if (ExecutorCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
32 IAM_LOGE("descriptor is not matched");
33 return GENERAL_ERROR;
34 }
35 switch (code) {
36 case ExecutorCallbackInterface::ON_MESSENGER_READY:
37 return OnMessengerReadyStub(data, reply);
38 case ExecutorCallbackInterface::ON_BEGIN_EXECUTE:
39 return OnBeginExecuteStub(data, reply);
40 case ExecutorCallbackInterface::ON_END_EXECUTE:
41 return OnEndExecuteStub(data, reply);
42 case ExecutorCallbackInterface::ON_SET_PROPERTY:
43 return OnSetPropertyStub(data, reply);
44 case ExecutorCallbackInterface::ON_GET_PROPERTY:
45 return OnGetPropertyStub(data, reply);
46 default:
47 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48 }
49 }
50
OnMessengerReadyStub(MessageParcel & data,MessageParcel & reply)51 int32_t ExecutorCallbackStub::OnMessengerReadyStub(MessageParcel &data, MessageParcel &reply)
52 {
53 sptr<IRemoteObject> obj = data.ReadRemoteObject();
54 if (obj == nullptr) {
55 IAM_LOGE("failed to read remote object");
56 return READ_PARCEL_ERROR;
57 }
58 sptr<ExecutorMessengerInterface> messenger = iface_cast<ExecutorMessengerProxy>(obj);
59 if (messenger == nullptr) {
60 IAM_LOGE("executor messenger is nullptr");
61 return GENERAL_ERROR;
62 }
63
64 std::vector<uint8_t> publicKey;
65 std::vector<uint64_t> templateIds;
66
67 if (!data.ReadUInt8Vector(&publicKey)) {
68 IAM_LOGE("failed to read publicKey");
69 return READ_PARCEL_ERROR;
70 }
71 if (!data.ReadUInt64Vector(&templateIds)) {
72 IAM_LOGE("failed to read templateIds");
73 return READ_PARCEL_ERROR;
74 }
75
76 OnMessengerReady(messenger, publicKey, templateIds);
77 return SUCCESS;
78 }
79
OnBeginExecuteStub(MessageParcel & data,MessageParcel & reply)80 int32_t ExecutorCallbackStub::OnBeginExecuteStub(MessageParcel &data, MessageParcel &reply)
81 {
82 uint64_t scheduleId;
83 std::vector<uint8_t> publicKey;
84 std::vector<uint8_t> buffer;
85
86 if (!data.ReadUint64(scheduleId)) {
87 IAM_LOGE("failed to read scheduleId");
88 return READ_PARCEL_ERROR;
89 }
90 if (!data.ReadUInt8Vector(&publicKey)) {
91 IAM_LOGE("failed to read publicKey");
92 return READ_PARCEL_ERROR;
93 }
94 if (!data.ReadUInt8Vector(&buffer)) {
95 IAM_LOGE("failed to read command");
96 return READ_PARCEL_ERROR;
97 }
98 Attributes commandAttrs(buffer);
99
100 int32_t result = OnBeginExecute(scheduleId, publicKey, commandAttrs);
101 if (!reply.WriteInt32(result)) {
102 IAM_LOGE("failed to write OnBeginExecute result");
103 return WRITE_PARCEL_ERROR;
104 }
105 return SUCCESS;
106 }
107
OnEndExecuteStub(MessageParcel & data,MessageParcel & reply)108 int32_t ExecutorCallbackStub::OnEndExecuteStub(MessageParcel &data, MessageParcel &reply)
109 {
110 uint64_t scheduleId;
111 std::vector<uint8_t> buffer;
112
113 if (!data.ReadUint64(scheduleId)) {
114 IAM_LOGE("failed to read scheduleId");
115 return READ_PARCEL_ERROR;
116 }
117 if (!data.ReadUInt8Vector(&buffer)) {
118 IAM_LOGE("failed to read command");
119 return READ_PARCEL_ERROR;
120 }
121 Attributes consumerAttr(buffer);
122
123 int32_t result = OnEndExecute(scheduleId, consumerAttr);
124 if (!reply.WriteInt32(result)) {
125 IAM_LOGE("failed to write OnEndExecute result");
126 return WRITE_PARCEL_ERROR;
127 }
128 return SUCCESS;
129 }
130
OnSetPropertyStub(MessageParcel & data,MessageParcel & reply)131 int32_t ExecutorCallbackStub::OnSetPropertyStub(MessageParcel &data, MessageParcel &reply)
132 {
133 std::vector<uint8_t> buffer;
134 if (!data.ReadUInt8Vector(&buffer)) {
135 IAM_LOGE("failed to read properties");
136 return READ_PARCEL_ERROR;
137 }
138 Attributes properties(buffer);
139
140 int32_t result = OnSetProperty(properties);
141 if (!reply.WriteInt32(result)) {
142 IAM_LOGE("failed to write OnSetProperty result");
143 return WRITE_PARCEL_ERROR;
144 }
145 return SUCCESS;
146 }
147
OnGetPropertyStub(MessageParcel & data,MessageParcel & reply)148 int32_t ExecutorCallbackStub::OnGetPropertyStub(MessageParcel &data, MessageParcel &reply)
149 {
150 std::vector<uint8_t> buffer;
151 if (!data.ReadUInt8Vector(&buffer)) {
152 IAM_LOGE("failed to read conditions");
153 return READ_PARCEL_ERROR;
154 }
155 Attributes conditions(buffer);
156 Attributes values;
157
158 int32_t result = OnGetProperty(conditions, values);
159 if (!reply.WriteInt32(result)) {
160 IAM_LOGE("failed to write OnGetProperty result");
161 return WRITE_PARCEL_ERROR;
162 }
163
164 std::vector<uint8_t> replyBuffer = values.Serialize();
165 if (!reply.WriteUInt8Vector(replyBuffer)) {
166 IAM_LOGE("failed to write replyBuffer");
167 return WRITE_PARCEL_ERROR;
168 }
169 return SUCCESS;
170 }
171 } // namespace UserAuth
172 } // namespace UserIam
173 } // namespace OHOS