1 /*
2  * Copyright (c) 2022-2024 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 "inputer_get_data_proxy.h"
17 
18 #include "iam_logger.h"
19 
20 #define LOG_TAG "PIN_AUTH_SA"
21 
22 namespace OHOS {
23 namespace UserIam {
24 namespace PinAuth {
WriteInputerGetDataParam(MessageParcel & data,const InputerGetDataParam & getDataParam)25 bool InputerGetDataProxy::WriteInputerGetDataParam(MessageParcel &data, const InputerGetDataParam &getDataParam)
26 {
27     if (!data.WriteInt32(getDataParam.mode)) {
28         IAM_LOGE("write mode fail");
29         return false;
30     }
31     if (!data.WriteInt32(getDataParam.authSubType)) {
32         IAM_LOGE("write authSubType fail");
33         return false;
34     }
35     if (!data.WriteUint32(getDataParam.algoVersion)) {
36         IAM_LOGE("write algoVersion fail");
37         return false;
38     }
39     if (!data.WriteUInt8Vector(getDataParam.algoParameter)) {
40         IAM_LOGE("write algoParameter fail");
41         return false;
42     }
43     if (!data.WriteUInt8Vector(getDataParam.challenge)) {
44         IAM_LOGE("write challenge fail");
45         return false;
46     }
47     if (!data.WriteInt32(getDataParam.userId)) {
48         IAM_LOGE("write userId fail");
49         return false;
50     }
51     if (!data.WriteString(getDataParam.complexityReg)) {
52         IAM_LOGE("write complexityReg fail");
53         return false;
54     }
55     if (!data.WriteInt32(getDataParam.authIntent)) {
56         IAM_LOGE("write authIntent fail");
57         return false;
58     }
59     if (!data.WriteRemoteObject(getDataParam.inputerSetData->AsObject())) {
60         IAM_LOGE("write inputerData fail");
61         return false;
62     }
63     return true;
64 }
65 
OnGetData(const InputerGetDataParam & getDataParam)66 void InputerGetDataProxy::OnGetData(const InputerGetDataParam &getDataParam)
67 {
68     IAM_LOGI("start");
69     if (getDataParam.inputerSetData == nullptr) {
70         IAM_LOGE("inputerSetData is nullptr");
71         return;
72     }
73     MessageParcel data;
74     MessageParcel reply;
75     if (!data.WriteInterfaceToken(InputerGetDataProxy::GetDescriptor())) {
76         IAM_LOGE("write descriptor fail");
77         return;
78     }
79     if (!WriteInputerGetDataParam(data, getDataParam)) {
80         IAM_LOGE("WriteInputerGetDataParam fail");
81         return;
82     }
83     if (SendRequest(InputerGetDataInterfaceCode::ON_GET_DATA, data, reply)) {
84         int32_t result = reply.ReadInt32();
85         IAM_LOGI("result = %{public}d", result);
86     }
87 }
88 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)89 bool InputerGetDataProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
90 {
91     IAM_LOGI("code = %{public}u", code);
92     sptr<IRemoteObject> remote = Remote();
93     if (remote == nullptr) {
94         IAM_LOGE("failed to get remote");
95         return false;
96     }
97     MessageOption option(MessageOption::TF_SYNC);
98     int32_t result = remote->SendRequest(code, data, reply, option);
99     if (result != OHOS::NO_ERROR) {
100         IAM_LOGE("failed to send request, result = %{public}d", result);
101         return false;
102     }
103     return true;
104 }
105 } // namespace PinAuth
106 } // namespace UserIam
107 } // namespace OHOS
108