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 "user_auth_callback_proxy.h"
17 
18 #include "iam_logger.h"
19 #include "iam_common_defines.h"
20 #include "user_auth_interface.h"
21 
22 #define LOG_TAG "USER_AUTH_SA"
23 
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
OnResult(int32_t result,const Attributes & extraInfo)27 void UserAuthCallbackProxy::OnResult(int32_t result, const Attributes &extraInfo)
28 {
29     IAM_LOGI("start");
30 
31     MessageParcel data;
32     MessageParcel reply;
33 
34     if (!data.WriteInterfaceToken(UserAuthCallbackProxy::GetDescriptor())) {
35         IAM_LOGE("write descriptor failed");
36         return;
37     }
38     if (!data.WriteInt32(result)) {
39         IAM_LOGE("write result failed");
40         return;
41     }
42     auto buffer = extraInfo.Serialize();
43     if (!data.WriteUInt8Vector(buffer)) {
44         IAM_LOGE("write buffer failed");
45         return;
46     }
47 
48     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_ON_RESULT, data, reply);
49     if (!ret) {
50         IAM_LOGE("send request failed");
51     }
52 }
53 
OnAcquireInfo(int32_t module,int32_t acquireInfo,const Attributes & extraInfo)54 void UserAuthCallbackProxy::OnAcquireInfo(int32_t module, int32_t acquireInfo, const Attributes &extraInfo)
55 {
56     IAM_LOGI("start");
57 
58     MessageParcel data;
59     MessageParcel reply;
60 
61     if (!data.WriteInterfaceToken(UserAuthCallbackProxy::GetDescriptor())) {
62         IAM_LOGE("write descriptor failed");
63         return;
64     }
65     if (!data.WriteInt32(module)) {
66         IAM_LOGE("write module failed");
67         return;
68     }
69     if (!data.WriteInt32(acquireInfo)) {
70         IAM_LOGE("write acquireInfo failed");
71         return;
72     }
73     auto buffer = extraInfo.Serialize();
74     if (!data.WriteUInt8Vector(buffer)) {
75         IAM_LOGE("write buffer failed");
76         return;
77     }
78 
79     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_ACQUIRE_INFO, data, reply);
80     if (!ret) {
81         IAM_LOGE("send request failed");
82     }
83 }
84 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)85 bool UserAuthCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
86 {
87     IAM_LOGI("start code = %{public}u", code);
88     sptr<IRemoteObject> remote = Remote();
89     if (remote == nullptr) {
90         IAM_LOGE("get remote failed");
91         return false;
92     }
93     MessageOption option(MessageOption::TF_ASYNC);
94     int32_t result = remote->SendRequest(code, data, reply, option);
95     if (result != OHOS::NO_ERROR) {
96         IAM_LOGE("send request failed, result = %{public}d", result);
97         return false;
98     }
99     IAM_LOGI("end");
100     return true;
101 }
102 
OnGetExecutorPropertyResult(int32_t result,const Attributes & attributes)103 void GetExecutorPropertyCallbackProxy::OnGetExecutorPropertyResult(int32_t result, const Attributes &attributes)
104 {
105     IAM_LOGI("start");
106 
107     MessageParcel data;
108     MessageParcel reply;
109 
110     if (!data.WriteInterfaceToken(GetExecutorPropertyCallbackProxy::GetDescriptor())) {
111         IAM_LOGE("write descriptor failed");
112         return;
113     }
114     if (!data.WriteInt32(result)) {
115         IAM_LOGE("write result failed");
116         return;
117     }
118     auto buffer = attributes.Serialize();
119     if (!data.WriteUInt8Vector(buffer)) {
120         IAM_LOGE("write buffer failed");
121         return;
122     }
123 
124     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_EX_PROP, data, reply);
125     if (!ret) {
126         IAM_LOGE("send request failed");
127     }
128 }
129 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)130 bool GetExecutorPropertyCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
131 {
132     IAM_LOGI("start");
133     sptr<IRemoteObject> remote = Remote();
134     if (remote == nullptr) {
135         IAM_LOGE("get remote failed");
136         return false;
137     }
138     MessageOption option(MessageOption::TF_ASYNC);
139     int32_t result = remote->SendRequest(code, data, reply, option);
140     if (result != OHOS::NO_ERROR) {
141         IAM_LOGE("send request failed, result = %{public}d", result);
142         return false;
143     }
144     IAM_LOGI("end");
145     return true;
146 }
147 
OnSetExecutorPropertyResult(int32_t result)148 void SetExecutorPropertyCallbackProxy::OnSetExecutorPropertyResult(int32_t result)
149 {
150     IAM_LOGI("start");
151 
152     MessageParcel data;
153     MessageParcel reply;
154 
155     if (!data.WriteInterfaceToken(SetExecutorPropertyCallbackProxy::GetDescriptor())) {
156         IAM_LOGE("write descriptor failed");
157         return;
158     }
159     if (!data.WriteInt32(result)) {
160         IAM_LOGE("write result failed");
161         return;
162     }
163 
164     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_SET_EX_PROP, data, reply);
165     if (!ret) {
166         IAM_LOGE("send request failed");
167     }
168 }
169 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)170 bool SetExecutorPropertyCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
171 {
172     IAM_LOGI("start");
173     sptr<IRemoteObject> remote = Remote();
174     if (remote == nullptr) {
175         IAM_LOGE("get remote failed");
176         return false;
177     }
178     MessageOption option(MessageOption::TF_ASYNC);
179     int32_t result = remote->SendRequest(code, data, reply, option);
180     if (result != OHOS::NO_ERROR) {
181         IAM_LOGE("send request failed, result = %{public}d", result);
182         return false;
183     }
184     IAM_LOGI("end");
185     return true;
186 }
187 } // namespace UserAuth
188 } // namespace UserIam
189 } // namespace OHOS