• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_proxy.h"
17 
18 #include "iam_logger.h"
19 #include "iam_common_defines.h"
20 #include "message_parcel.h"
21 
22 #define LOG_LABEL UserIam::Common::LABEL_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     if (!data.WriteRemoteObject(messenger->AsObject())) {
42         IAM_LOGE("failed to write messenger failed");
43         return;
44     }
45     if (!data.WriteUInt8Vector(publicKey)) {
46         IAM_LOGE("failed to write publicKey");
47         return;
48     }
49     if (!data.WriteUInt64Vector(templateIdList)) {
50         IAM_LOGE("failed to write templateIdList");
51         return;
52     }
53 
54     bool result = SendRequest(ExecutorCallbackInterface::ON_MESSENGER_READY, data, reply);
55     if (!result) {
56         IAM_LOGE("send request failed");
57         return;
58     }
59 }
60 
OnBeginExecute(uint64_t scheduleId,const std::vector<uint8_t> & publicKey,const Attributes & command)61 int32_t ExecutorCallbackProxy::OnBeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
62     const Attributes &command)
63 {
64     MessageParcel data;
65     MessageParcel reply;
66 
67     if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
68         IAM_LOGE("write descriptor failed");
69         return GENERAL_ERROR;
70     }
71     if (!data.WriteUint64(scheduleId)) {
72         IAM_LOGE("write scheduleId failed");
73         return GENERAL_ERROR;
74     }
75     if (!data.WriteUInt8Vector(publicKey)) {
76         IAM_LOGE("write publicKey failed");
77         return GENERAL_ERROR;
78     }
79     auto attr = command.Serialize();
80     if (!data.WriteUInt8Vector(attr)) {
81         IAM_LOGE("write command failed");
82         return GENERAL_ERROR;
83     }
84 
85     bool ret = SendRequest(ExecutorCallbackInterface::ON_BEGIN_EXECUTE, data, reply);
86     if (!ret) {
87         IAM_LOGE("send request failed");
88         return GENERAL_ERROR;
89     }
90     int32_t result = GENERAL_ERROR;
91     if (!reply.ReadInt32(result)) {
92         IAM_LOGE("read request result failed");
93         return GENERAL_ERROR;
94     }
95     return result;
96 }
97 
OnEndExecute(uint64_t scheduleId,const Attributes & command)98 int32_t ExecutorCallbackProxy::OnEndExecute(uint64_t scheduleId, const Attributes &command)
99 {
100     MessageParcel data;
101     MessageParcel reply;
102 
103     if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
104         IAM_LOGE("write descriptor failed");
105         return GENERAL_ERROR;
106     }
107     if (!data.WriteUint64(scheduleId)) {
108         IAM_LOGE("write scheduleId failed");
109         return GENERAL_ERROR;
110     }
111     auto attr = command.Serialize();
112     if (!data.WriteUInt8Vector(attr)) {
113         IAM_LOGE("write command failed");
114         return GENERAL_ERROR;
115     }
116 
117     bool ret = SendRequest(ExecutorCallbackInterface::ON_END_EXECUTE, data, reply);
118     if (!ret) {
119         IAM_LOGE("send request failed");
120         return GENERAL_ERROR;
121     }
122     int32_t result = GENERAL_ERROR;
123     if (!reply.ReadInt32(result)) {
124         IAM_LOGE("read request result failed");
125         return GENERAL_ERROR;
126     }
127     return result;
128 }
129 
OnSetProperty(const Attributes & properties)130 int32_t ExecutorCallbackProxy::OnSetProperty(const Attributes &properties)
131 {
132     MessageParcel data;
133     MessageParcel reply;
134 
135     if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
136         IAM_LOGE("write descriptor failed");
137         return GENERAL_ERROR;
138     }
139     auto attr = properties.Serialize();
140     if (!data.WriteUInt8Vector(attr)) {
141         IAM_LOGE("write properties failed");
142         return GENERAL_ERROR;
143     }
144 
145     bool ret = SendRequest(ExecutorCallbackInterface::ON_SET_PROPERTY, data, reply);
146     if (!ret) {
147         IAM_LOGE("send request failed");
148         return GENERAL_ERROR;
149     }
150     int32_t result = GENERAL_ERROR;
151     if (!reply.ReadInt32(result)) {
152         IAM_LOGE("read request result failed");
153         return GENERAL_ERROR;
154     }
155     return result;
156 }
157 
OnGetProperty(const Attributes & condition,Attributes & values)158 int32_t ExecutorCallbackProxy::OnGetProperty(const Attributes &condition, Attributes &values)
159 {
160     MessageParcel data;
161     MessageParcel reply;
162 
163     if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) {
164         IAM_LOGE("write descriptor failed");
165         return GENERAL_ERROR;
166     }
167 
168     if (!data.WriteUInt8Vector(condition.Serialize())) {
169         IAM_LOGE("write condition failed");
170         return GENERAL_ERROR;
171     }
172 
173     bool ret = SendRequest(ExecutorCallbackInterface::ON_GET_PROPERTY, data, reply);
174     if (!ret) {
175         IAM_LOGE("send request failed");
176         return GENERAL_ERROR;
177     }
178     int32_t result = GENERAL_ERROR;
179     if (!reply.ReadInt32(result)) {
180         IAM_LOGE("read request result failed");
181         return GENERAL_ERROR;
182     }
183 
184     std::vector<uint8_t> attr;
185     if (!reply.ReadUInt8Vector(&attr)) {
186         IAM_LOGE("read reply values failed");
187         return GENERAL_ERROR;
188     }
189     values = Attributes(attr);
190     return result;
191 }
192 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)193 bool ExecutorCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
194 {
195     IAM_LOGI("code = %{public}u", code);
196     sptr<IRemoteObject> remote = Remote();
197     if (remote == nullptr) {
198         IAM_LOGE("get remote failed");
199         return false;
200     }
201     MessageOption option(MessageOption::TF_SYNC);
202     int32_t result = remote->SendRequest(code, data, reply, option);
203     if (result != OHOS::NO_ERROR) {
204         IAM_LOGE("send request failed, code = %{public}u, result = %{public}d", code, result);
205         return false;
206     }
207     return true;
208 }
209 } // namespace UserAuth
210 } // namespace UserIam
211 } // namespace OHOS