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 "pin_auth_proxy.h"
17 
18 #include "ipc_types.h"
19 
20 #include "iam_logger.h"
21 #include "inputer_get_data.h"
22 
23 #define LOG_TAG "PIN_AUTH_SDK"
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace PinAuth {
RegisterInputer(const sptr<InputerGetData> & inputer)28 bool PinAuthProxy::RegisterInputer(const sptr<InputerGetData> &inputer)
29 {
30     IAM_LOGI("start");
31     if (inputer == nullptr) {
32         IAM_LOGE("inputer is nullptr");
33         return false;
34     }
35 
36     MessageParcel data;
37     MessageParcel reply;
38 
39     if (!data.WriteInterfaceToken(PinAuthProxy::GetDescriptor())) {
40         IAM_LOGE("failed to write descriptor");
41         return false;
42     }
43 
44     if (!data.WriteRemoteObject(inputer->AsObject())) {
45         IAM_LOGE("failed to write inputer");
46         return false;
47     }
48 
49     bool ret = SendRequest(PinAuthInterfaceCode::REGISTER_INPUTER, data, reply);
50     if (!ret) {
51         return false;
52     }
53     bool result = false;
54     if (!reply.ReadBool(result)) {
55         IAM_LOGE("failed to read result");
56     }
57     return result;
58 }
59 
UnRegisterInputer()60 void PinAuthProxy::UnRegisterInputer()
61 {
62     IAM_LOGI("start");
63     MessageParcel data;
64     MessageParcel reply;
65 
66     if (!data.WriteInterfaceToken(PinAuthProxy::GetDescriptor())) {
67         IAM_LOGE("failed to write descriptor");
68         return;
69     }
70 
71     SendRequest(PinAuthInterfaceCode::UNREGISTER_INPUTER, data, reply);
72 }
73 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)74 bool PinAuthProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
75 {
76     IAM_LOGI("code = %{public}u", code);
77     sptr<IRemoteObject> remote = Remote();
78     if (remote == nullptr) {
79         IAM_LOGE("failed to get remote");
80         return false;
81     }
82     MessageOption option(MessageOption::TF_SYNC);
83     int32_t result = remote->SendRequest(code, data, reply, option);
84     if (result != OHOS::NO_ERROR) {
85         IAM_LOGE("failed to send request, result = %{public}d", result);
86         return false;
87     }
88     return true;
89 }
90 } // namespace PinAuth
91 } // namespace UserIam
92 } // namespace OHOS