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_stub.h"
17 
18 #include "executor_messenger_proxy.h"
19 #include "iam_common_defines.h"
20 #include "iam_logger.h"
21 
22 #define LOG_TAG "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 ExecutorCallbackInterfaceCode::ON_MESSENGER_READY:
37             return OnMessengerReadyStub(data, reply);
38         case ExecutorCallbackInterfaceCode::ON_BEGIN_EXECUTE:
39             return OnBeginExecuteStub(data, reply);
40         case ExecutorCallbackInterfaceCode::ON_END_EXECUTE:
41             return OnEndExecuteStub(data, reply);
42         case ExecutorCallbackInterfaceCode::ON_SET_PROPERTY:
43             return OnSetPropertyStub(data, reply);
44         case ExecutorCallbackInterfaceCode::ON_GET_PROPERTY:
45             return OnGetPropertyStub(data, reply);
46         case ExecutorCallbackInterfaceCode::ON_SEND_DATA:
47             return OnSendDataStub(data, reply);
48         default:
49             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50     }
51 }
52 
OnMessengerReadyStub(MessageParcel & data,MessageParcel & reply)53 int32_t ExecutorCallbackStub::OnMessengerReadyStub(MessageParcel &data, MessageParcel &reply)
54 {
55     sptr<IRemoteObject> obj = data.ReadRemoteObject();
56     if (obj == nullptr) {
57         IAM_LOGE("failed to read remote object");
58         return READ_PARCEL_ERROR;
59     }
60 
61     sptr<ExecutorMessengerInterface> messenger = iface_cast<ExecutorMessengerProxy>(obj);
62     if (messenger == nullptr) {
63         IAM_LOGE("executor messenger is nullptr");
64         return GENERAL_ERROR;
65     }
66 
67     std::vector<uint8_t> publicKey;
68     std::vector<uint64_t> templateIds;
69 
70     if (!data.ReadUInt8Vector(&publicKey)) {
71         IAM_LOGE("failed to read publicKey");
72         return READ_PARCEL_ERROR;
73     }
74     if (!data.ReadUInt64Vector(&templateIds)) {
75         IAM_LOGE("failed to read templateIds");
76         return READ_PARCEL_ERROR;
77     }
78 
79     OnMessengerReady(messenger, publicKey, templateIds);
80     return SUCCESS;
81 }
82 
OnBeginExecuteStub(MessageParcel & data,MessageParcel & reply)83 int32_t ExecutorCallbackStub::OnBeginExecuteStub(MessageParcel &data, MessageParcel &reply)
84 {
85     uint64_t scheduleId;
86     std::vector<uint8_t> publicKey;
87     std::vector<uint8_t> buffer;
88 
89     if (!data.ReadUint64(scheduleId)) {
90         IAM_LOGE("failed to read scheduleId");
91         return READ_PARCEL_ERROR;
92     }
93     if (!data.ReadUInt8Vector(&publicKey)) {
94         IAM_LOGE("failed to read publicKey");
95         return READ_PARCEL_ERROR;
96     }
97     if (!data.ReadUInt8Vector(&buffer)) {
98         IAM_LOGE("failed to read command");
99         return READ_PARCEL_ERROR;
100     }
101     Attributes commandAttrs(buffer);
102 
103     int32_t result = OnBeginExecute(scheduleId, publicKey, commandAttrs);
104     if (!reply.WriteInt32(result)) {
105         IAM_LOGE("failed to write OnBeginExecute result");
106         return WRITE_PARCEL_ERROR;
107     }
108     return SUCCESS;
109 }
110 
OnEndExecuteStub(MessageParcel & data,MessageParcel & reply)111 int32_t ExecutorCallbackStub::OnEndExecuteStub(MessageParcel &data, MessageParcel &reply)
112 {
113     uint64_t scheduleId;
114     std::vector<uint8_t> buffer;
115 
116     if (!data.ReadUint64(scheduleId)) {
117         IAM_LOGE("failed to read scheduleId");
118         return READ_PARCEL_ERROR;
119     }
120     if (!data.ReadUInt8Vector(&buffer)) {
121         IAM_LOGE("failed to read command");
122         return READ_PARCEL_ERROR;
123     }
124     Attributes consumerAttr(buffer);
125 
126     int32_t result = OnEndExecute(scheduleId, consumerAttr);
127     if (!reply.WriteInt32(result)) {
128         IAM_LOGE("failed to write OnEndExecute result");
129         return WRITE_PARCEL_ERROR;
130     }
131     return SUCCESS;
132 }
133 
OnSetPropertyStub(MessageParcel & data,MessageParcel & reply)134 int32_t ExecutorCallbackStub::OnSetPropertyStub(MessageParcel &data, MessageParcel &reply)
135 {
136     std::vector<uint8_t> buffer;
137     if (!data.ReadUInt8Vector(&buffer)) {
138         IAM_LOGE("failed to read properties");
139         return READ_PARCEL_ERROR;
140     }
141     Attributes properties(buffer);
142 
143     int32_t result = OnSetProperty(properties);
144     if (!reply.WriteInt32(result)) {
145         IAM_LOGE("failed to write OnSetProperty result");
146         return WRITE_PARCEL_ERROR;
147     }
148     return SUCCESS;
149 }
150 
OnGetPropertyStub(MessageParcel & data,MessageParcel & reply)151 int32_t ExecutorCallbackStub::OnGetPropertyStub(MessageParcel &data, MessageParcel &reply)
152 {
153     std::vector<uint8_t> buffer;
154     if (!data.ReadUInt8Vector(&buffer)) {
155         IAM_LOGE("failed to read conditions");
156         return READ_PARCEL_ERROR;
157     }
158     Attributes conditions(buffer);
159     Attributes values;
160 
161     int32_t result = OnGetProperty(conditions, values);
162     if (!reply.WriteInt32(result)) {
163         IAM_LOGE("failed to write OnGetProperty result");
164         return WRITE_PARCEL_ERROR;
165     }
166 
167     std::vector<uint8_t> replyBuffer = values.Serialize();
168     if (!reply.WriteUInt8Vector(replyBuffer)) {
169         IAM_LOGE("failed to write replyBuffer");
170         return WRITE_PARCEL_ERROR;
171     }
172     return SUCCESS;
173 }
174 
OnSendDataStub(MessageParcel & data,MessageParcel & reply)175 int32_t ExecutorCallbackStub::OnSendDataStub(MessageParcel &data, MessageParcel &reply)
176 {
177     uint64_t scheduleId;
178     std::vector<uint8_t> buffer;
179 
180     if (!data.ReadUint64(scheduleId)) {
181         IAM_LOGE("failed to read scheduleId");
182         return READ_PARCEL_ERROR;
183     }
184     if (!data.ReadUInt8Vector(&buffer)) {
185         IAM_LOGE("failed to read data");
186         return READ_PARCEL_ERROR;
187     }
188     Attributes dataAttr(buffer);
189 
190     int32_t result = OnSendData(scheduleId, dataAttr);
191     if (!reply.WriteInt32(result)) {
192         IAM_LOGE("failed to write OnSendData result");
193         return WRITE_PARCEL_ERROR;
194     }
195     return SUCCESS;
196 }
197 } // namespace UserAuth
198 } // namespace UserIam
199 } // namespace OHOS