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