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_messenger_proxy.h"
17 
18 #include "iam_logger.h"
19 
20 #define LOG_TAG "AUTH_EXECUTOR_MGR_SDK"
21 
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
ExecutorMessengerProxy(const sptr<IRemoteObject> & impl)25 ExecutorMessengerProxy::ExecutorMessengerProxy(const sptr<IRemoteObject> &impl)
26     : IRemoteProxy<ExecutorMessengerInterface>(impl)
27 {
28 }
29 
SendData(uint64_t scheduleId,ExecutorRole dstRole,const std::vector<uint8_t> & msg)30 int32_t ExecutorMessengerProxy::SendData(uint64_t scheduleId,
31     ExecutorRole dstRole, const std::vector<uint8_t> &msg)
32 {
33     MessageParcel data;
34     MessageParcel reply;
35 
36     if (!data.WriteInterfaceToken(ExecutorMessengerProxy::GetDescriptor())) {
37         IAM_LOGE("failed to write descriptor");
38         return WRITE_PARCEL_ERROR;
39     }
40     if (!data.WriteUint64(scheduleId)) {
41         IAM_LOGE("failed to write scheduleId");
42         return WRITE_PARCEL_ERROR;
43     }
44     if (!data.WriteInt32(dstRole)) {
45         IAM_LOGE("failed to write dstRole");
46         return WRITE_PARCEL_ERROR;
47     }
48     if (!data.WriteUInt8Vector(msg)) {
49         IAM_LOGE("failed to write msg");
50         return WRITE_PARCEL_ERROR;
51     }
52 
53     bool ret = SendRequest(ExecutorMessengerInterfaceCode::CO_AUTH_SEND_DATA, data, reply);
54     if (!ret) {
55         IAM_LOGE("failed to send request");
56         return GENERAL_ERROR;
57     }
58     int32_t result = 0;
59     if (!reply.ReadInt32(result)) {
60         IAM_LOGE("failed to read result");
61         return READ_PARCEL_ERROR;
62     }
63     return result;
64 }
65 
Finish(uint64_t scheduleId,ResultCode resultCode,const std::shared_ptr<Attributes> & finalResult)66 int32_t ExecutorMessengerProxy::Finish(uint64_t scheduleId, ResultCode resultCode,
67     const std::shared_ptr<Attributes> &finalResult)
68 {
69     if (finalResult == nullptr) {
70         IAM_LOGE("finalResult is nullptr");
71         return INVALID_PARAMETERS;
72     }
73     MessageParcel data;
74     MessageParcel reply;
75 
76     if (!data.WriteInterfaceToken(ExecutorMessengerProxy::GetDescriptor())) {
77         IAM_LOGE("failed to write descriptor");
78         return WRITE_PARCEL_ERROR;
79     }
80     if (!data.WriteUint64(scheduleId)) {
81         IAM_LOGE("failed to write scheduleId");
82         return WRITE_PARCEL_ERROR;
83     }
84     if (!data.WriteInt32(resultCode)) {
85         IAM_LOGE("failed to write resultCode");
86         return WRITE_PARCEL_ERROR;
87     }
88     std::vector<uint8_t> buffer = finalResult->Serialize();
89     if (!data.WriteUInt8Vector(buffer)) {
90         IAM_LOGE("failed to write finalResult");
91         return WRITE_PARCEL_ERROR;
92     }
93 
94     bool ret = SendRequest(ExecutorMessengerInterfaceCode::CO_AUTH_FINISH, data, reply);
95     if (!ret) {
96         IAM_LOGE("failed to send request");
97         return GENERAL_ERROR;
98     }
99     int32_t result = 0;
100     if (!reply.ReadInt32(result)) {
101         IAM_LOGE("failed to read result");
102         return READ_PARCEL_ERROR;
103     }
104     return result;
105 }
106 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)107 bool ExecutorMessengerProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
108 {
109     IAM_LOGI("code = %{public}u", code);
110     sptr<IRemoteObject> remote = Remote();
111     if (remote == nullptr) {
112         IAM_LOGE("failed to get remote");
113         return false;
114     }
115     MessageOption option(MessageOption::TF_SYNC);
116     int32_t result = remote->SendRequest(code, data, reply, option);
117     if (result != OHOS::NO_ERROR) {
118         IAM_LOGE("failed to send request, result = %{public}d", result);
119         return false;
120     }
121     return true;
122 }
123 } // namespace UserAuth
124 } // namespace UserIam
125 } // namespace OHOS