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