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_stub.h"
17 
18 #include "iam_logger.h"
19 #include "iam_common_defines.h"
20 
21 #define LOG_TAG "PIN_AUTH_SA"
22 
23 namespace OHOS {
24 namespace UserIam {
25 namespace PinAuth {
26 
27 // When true is passed into IRemoteStub, sa will process request serially.
PinAuthStub()28 PinAuthStub::PinAuthStub() : IRemoteStub(true) {};
29 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t PinAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32     IAM_LOGI("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
33     if (PinAuthStub::GetDescriptor() != data.ReadInterfaceToken()) {
34         IAM_LOGE("descriptor is not matched");
35         return UserAuth::GENERAL_ERROR;
36     }
37     switch (code) {
38         case PinAuthInterfaceCode::REGISTER_INPUTER:
39             RegisterInputerStub(data, reply);
40             return UserAuth::SUCCESS;
41         case PinAuthInterfaceCode::UNREGISTER_INPUTER:
42             UnRegisterInputerStub(data, reply);
43             return UserAuth::SUCCESS;
44         default:
45             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
46     }
47 }
48 
RegisterInputerStub(MessageParcel & data,MessageParcel & reply)49 void PinAuthStub::RegisterInputerStub(MessageParcel &data, MessageParcel &reply)
50 {
51     IAM_LOGI("start");
52     sptr<IRemoteObject> obj = data.ReadRemoteObject();
53     if (obj == nullptr) {
54         IAM_LOGE("failed to read remote object");
55         return;
56     }
57     sptr<InputerGetData> inputer = iface_cast<InputerGetData>(obj);
58     if (inputer == nullptr) {
59         IAM_LOGE("inputer is nullptr");
60         return;
61     }
62     bool ret = RegisterInputer(inputer);
63     if (!reply.WriteBool(ret)) {
64         IAM_LOGE("failed to write result");
65     }
66 }
67 
UnRegisterInputerStub(MessageParcel & data,MessageParcel & reply)68 void PinAuthStub::UnRegisterInputerStub(MessageParcel &data, MessageParcel &reply)
69 {
70     (void)data;
71     (void)reply;
72     IAM_LOGI("start");
73     UnRegisterInputer();
74 }
75 } // namespace PinAuth
76 } // namespace UserIam
77 } // namespace OHOS