1 /*
2 * Copyright (c) 2022 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 <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_sbuf_ipc.h>
19
20 #include "iam_logger.h"
21 #include "v1_0/face_auth_interface_stub.h"
22
23 #define LOG_LABEL OHOS::UserIam::Common::LABEL_FACE_AUTH_HDI
24
25 using namespace OHOS::HDI::FaceAuth::V1_0;
26
27 struct HdfFaceAuthInterfaceHost {
28 struct IDeviceIoService ioService;
29 OHOS::sptr<OHOS::IRemoteObject> stub;
30 };
31
32 namespace {
FaceAuthInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)33 int32_t FaceAuthInterfaceDriverDispatch(
34 struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
35 {
36 IAM_LOGI("start");
37 if (client == nullptr || data == nullptr || reply == nullptr || client->device == nullptr ||
38 client->device->service == nullptr) {
39 IAM_LOGE("invalid param");
40 return HDF_ERR_INVALID_PARAM;
41 }
42
43 auto *hdfFaceAuthInterfaceHost = CONTAINER_OF(client->device->service, struct HdfFaceAuthInterfaceHost, ioService);
44 if (hdfFaceAuthInterfaceHost == nullptr || hdfFaceAuthInterfaceHost->stub == nullptr) {
45 IAM_LOGE("hdfFaceAuthInterfaceHost is invalid");
46 return HDF_ERR_INVALID_PARAM;
47 }
48
49 OHOS::MessageParcel *dataParcel = nullptr;
50 OHOS::MessageParcel *replyParcel = nullptr;
51 OHOS::MessageOption option;
52
53 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
54 IAM_LOGE("invalid data sbuf object to dispatch");
55 return HDF_ERR_INVALID_PARAM;
56 }
57 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
58 IAM_LOGE("invalid reply sbuf object to dispatch");
59 return HDF_ERR_INVALID_PARAM;
60 }
61
62 return hdfFaceAuthInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
63 }
64
HdfFaceAuthInterfaceDriverInit(struct HdfDeviceObject * deviceObject)65 int HdfFaceAuthInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
66 {
67 IAM_LOGI("start");
68 if (deviceObject == nullptr) {
69 IAM_LOGE("deviceObject is nullptr");
70 return HDF_ERR_INVALID_PARAM;
71 }
72 if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_USERAUTH)) {
73 IAM_LOGE("set face auth hdf class failed");
74 return HDF_FAILURE;
75 }
76 return HDF_SUCCESS;
77 }
78
HdfFaceAuthInterfaceDriverBind(struct HdfDeviceObject * deviceObject)79 int HdfFaceAuthInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
80 {
81 IAM_LOGI("start");
82 if (deviceObject == nullptr) {
83 IAM_LOGE("deviceObject is nullptr");
84 return HDF_ERR_INVALID_PARAM;
85 }
86 auto *hdfFaceAuthInterfaceHost = new (std::nothrow) HdfFaceAuthInterfaceHost;
87 if (hdfFaceAuthInterfaceHost == nullptr) {
88 IAM_LOGE("failed to create create HdfFaceAuthInterfaceHost object");
89 return HDF_FAILURE;
90 }
91
92 hdfFaceAuthInterfaceHost->ioService.Dispatch = FaceAuthInterfaceDriverDispatch;
93 hdfFaceAuthInterfaceHost->ioService.Open = NULL;
94 hdfFaceAuthInterfaceHost->ioService.Release = NULL;
95
96 auto serviceImpl = IFaceAuthInterface::Get(true);
97 if (serviceImpl == nullptr) {
98 IAM_LOGE("failed to get of implement service");
99 delete hdfFaceAuthInterfaceHost;
100 return HDF_FAILURE;
101 }
102
103 hdfFaceAuthInterfaceHost->stub =
104 OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, IFaceAuthInterface::GetDescriptor());
105 if (hdfFaceAuthInterfaceHost->stub == nullptr) {
106 IAM_LOGE("failed to get stub object");
107 delete hdfFaceAuthInterfaceHost;
108 return HDF_FAILURE;
109 }
110
111 deviceObject->service = &hdfFaceAuthInterfaceHost->ioService;
112 IAM_LOGI("success");
113 return HDF_SUCCESS;
114 }
115
HdfFaceAuthInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)116 void HdfFaceAuthInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
117 {
118 IAM_LOGI("start");
119 if (deviceObject == nullptr || deviceObject->service == nullptr) {
120 IAM_LOGE("deviceObject is invalid");
121 return;
122 }
123 auto *hdfFaceAuthInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfFaceAuthInterfaceHost, ioService);
124 if (hdfFaceAuthInterfaceHost == nullptr) {
125 IAM_LOGE("hdfFaceAuthInterfaceHost is nullptr");
126 return;
127 }
128 delete hdfFaceAuthInterfaceHost;
129 IAM_LOGI("success");
130 }
131
132 struct HdfDriverEntry g_faceAuthInterfaceDriverEntry = {
133 .moduleVersion = 1,
134 .moduleName = "drivers_peripheral_face_auth",
135 .Bind = HdfFaceAuthInterfaceDriverBind,
136 .Init = HdfFaceAuthInterfaceDriverInit,
137 .Release = HdfFaceAuthInterfaceDriverRelease,
138 };
139 } // namespace
140
141 #ifdef __cplusplus
142 extern "C" {
143 #endif /* __cplusplus */
144 HDF_INIT(g_faceAuthInterfaceDriverEntry);
145 #ifdef __cplusplus
146 }
147 #endif /* __cplusplus */
148