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_LABEL UserIam::Common::LABEL_FACE_AUTH_SDK
39
40 namespace OHOS {
41 namespace UserIam {
42 namespace FaceAuth {
43 using namespace OHOS::UserIam;
FaceAuthStub()44 FaceAuthStub::FaceAuthStub()
45 {
46 IAM_LOGI("start");
47 RegisterKeyToHandle();
48 // For iface_cast<IBufferProducer> to work, static variable BrokerDelegator of BufferClientProducer must be
49 // initialized. Create an unused surface would include BufferClientProducer and ensure BrokerDelegator is
50 // initialized.
51 OHOS::Surface::CreateSurfaceAsConsumer("unused");
52 }
53
RegisterKeyToHandle()54 void FaceAuthStub::RegisterKeyToHandle()
55 {
56 keyToHandle_[IFaceAuthInterfaceCode::FACE_AUTH_SET_BUFFER_PRODUCER] = &FaceAuthStub::FaceAuthSetBufferProducer;
57 }
58
FaceAuthSetBufferProducer(MessageParcel & data,MessageParcel & reply)59 int32_t FaceAuthStub::FaceAuthSetBufferProducer(MessageParcel &data, MessageParcel &reply)
60 {
61 sptr<IBufferProducer> buffer(nullptr);
62 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
63 IAM_LOGI("read remote object %{public}s", Common::GetPointerNullStateString(remoteObj).c_str());
64 buffer = iface_cast<IBufferProducer>(remoteObj);
65 bool remoteObjNull = (remoteObj == nullptr);
66 bool bufferNull = (buffer == nullptr);
67 IF_FALSE_LOGE_AND_RETURN_VAL(bufferNull == remoteObjNull, FACE_AUTH_ERROR);
68 int32_t ret = SetBufferProducer(buffer);
69 IAM_LOGI("SetBufferProducer ret %{public}d", ret);
70 if (!reply.WriteInt32(ret)) {
71 IAM_LOGE("failed to WriteInt32(ret)");
72 return FACE_AUTH_ERROR;
73 }
74 return FACE_AUTH_SUCCESS;
75 }
76
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)77 int32_t FaceAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
78 {
79 IAM_LOGI("start");
80 if (data.ReadInterfaceToken() != FaceAuthStub::GetDescriptor()) {
81 IAM_LOGE("descriptor is not matched");
82 return FACE_AUTH_ERROR;
83 }
84 auto itFunc = keyToHandle_.find(code);
85 if (itFunc == keyToHandle_.end()) {
86 IAM_LOGE("key not match, send to IPCObjectStub on default");
87 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
88 }
89 auto requestFunc = itFunc->second;
90 IF_FALSE_LOGE_AND_RETURN_VAL(requestFunc != nullptr, FACE_AUTH_ERROR);
91 return (this->*requestFunc)(data, reply);
92 }
93 } // namespace FaceAuth
94 } // namespace UserIam
95 } // namespace OHOS