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 "face_auth_stub.h"
17 
18 #include <cstdint>
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <utility>
23 
24 #include "ibuffer_producer.h"
25 #include "ipc_object_stub.h"
26 #include "iremote_broker.h"
27 #include "message_parcel.h"
28 #include "refbase.h"
29 #include "surface.h"
30 
31 #include "iam_check.h"
32 #include "iam_logger.h"
33 #include "iam_para2str.h"
34 
35 #include "face_auth_defines.h"
36 #include "iface_auth.h"
37 
38 #define LOG_TAG "FACE_AUTH_SDK"
39 
40 namespace OHOS {
41 namespace UserIam {
42 namespace FaceAuth {
43 using namespace OHOS::UserIam;
44 
45 // When true is passed into IRemoteStub, sa will process request serially.
FaceAuthStub()46 FaceAuthStub::FaceAuthStub() : IRemoteStub(true)
47 {
48     IAM_LOGI("start");
49     // For iface_cast<IBufferProducer> to work, static variable BrokerDelegator of BufferClientProducer must be
50     // initialized. Create an unused surface would include BufferClientProducer and ensure BrokerDelegator is
51     // initialized.
52     OHOS::Surface::CreateSurfaceAsConsumer("unused");
53 }
54 
FaceAuthSetBufferProducer(MessageParcel & data,MessageParcel & reply)55 int32_t FaceAuthStub::FaceAuthSetBufferProducer(MessageParcel &data, MessageParcel &reply)
56 {
57     sptr<IBufferProducer> buffer(nullptr);
58     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
59     IAM_LOGI("read remote object %{public}s", Common::GetPointerNullStateString(remoteObj).c_str());
60     buffer = iface_cast<IBufferProducer>(remoteObj);
61     bool remoteObjNull = (remoteObj == nullptr);
62     bool bufferNull = (buffer == nullptr);
63     IF_FALSE_LOGE_AND_RETURN_VAL(bufferNull == remoteObjNull, FACE_AUTH_ERROR);
64     int32_t ret = SetBufferProducer(buffer);
65     IAM_LOGI("SetBufferProducer ret %{public}d", ret);
66     if (!reply.WriteInt32(ret)) {
67         IAM_LOGE("failed to WriteInt32(ret)");
68         return FACE_AUTH_ERROR;
69     }
70     return FACE_AUTH_SUCCESS;
71 }
72 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)73 int32_t FaceAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
74 {
75     IAM_LOGI("start");
76     if (data.ReadInterfaceToken() != FaceAuthStub::GetDescriptor()) {
77         IAM_LOGE("descriptor is not matched");
78         return FACE_AUTH_ERROR;
79     }
80     if (code == IFaceAuthInterfaceCode::FACE_AUTH_SET_BUFFER_PRODUCER) {
81         return this->FaceAuthSetBufferProducer(data, reply);
82     }
83     IAM_LOGE("key not match, send to IPCObjectStub on default");
84     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
85 }
86 } // namespace FaceAuth
87 } // namespace UserIam
88 } // namespace OHOS