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/fingerprint_auth_interface_stub.h"
22
23 #define LOG_LABEL OHOS::UserIam::Common::LABEL_FINGERPRINT_AUTH_HDI
24
25 using namespace OHOS::HDI::FingerprintAuth::V1_0;
26
27 struct HdfFingerprintAuthInterfaceHost {
28 struct IDeviceIoService ioService;
29 OHOS::sptr<OHOS::IRemoteObject> stub;
30 };
31
32 namespace {
FingerprintAuthInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)33 int32_t FingerprintAuthInterfaceDriverDispatch(
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 auto *hdfFingerprintAuthInterfaceHost = CONTAINER_OF(client->device->service,
43 struct HdfFingerprintAuthInterfaceHost, ioService);
44 if (hdfFingerprintAuthInterfaceHost == nullptr || hdfFingerprintAuthInterfaceHost->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 hdfFingerprintAuthInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
63 }
64
HdfFingerprintAuthInterfaceDriverInit(struct HdfDeviceObject * deviceObject)65 int HdfFingerprintAuthInterfaceDriverInit(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 fingerprint auth hdf class failed");
74 return HDF_FAILURE;
75 }
76 return HDF_SUCCESS;
77 }
78
HdfFingerprintAuthInterfaceDriverBind(struct HdfDeviceObject * deviceObject)79 int HdfFingerprintAuthInterfaceDriverBind(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 *hdfFingerprintAuthInterfaceHost = new (std::nothrow) HdfFingerprintAuthInterfaceHost;
87 if (hdfFingerprintAuthInterfaceHost == nullptr) {
88 IAM_LOGE("failed to create create HdfFingerprinteAuthInterfaceHost object");
89 return HDF_FAILURE;
90 }
91
92 hdfFingerprintAuthInterfaceHost->ioService.Dispatch =FingerprintAuthInterfaceDriverDispatch;
93 hdfFingerprintAuthInterfaceHost->ioService.Open = NULL;
94 hdfFingerprintAuthInterfaceHost->ioService.Release = NULL;
95
96 auto serviceImpl = IFingerprintAuthInterface::Get(true);
97 if (serviceImpl == nullptr) {
98 IAM_LOGE("failed to get of implement service");
99 delete hdfFingerprintAuthInterfaceHost;
100 return HDF_FAILURE;
101 }
102
103 hdfFingerprintAuthInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
104 IFingerprintAuthInterface::GetDescriptor());
105 if (hdfFingerprintAuthInterfaceHost->stub == nullptr) {
106 IAM_LOGE("failed to get stub object");
107 delete hdfFingerprintAuthInterfaceHost;
108 return HDF_FAILURE;
109 }
110
111 deviceObject->service = &hdfFingerprintAuthInterfaceHost->ioService;
112 IAM_LOGI("success");
113 return HDF_SUCCESS;
114 }
115
HdfFingerprintAuthInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)116 void HdfFingerprintAuthInterfaceDriverRelease(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 *hdfFingerprintAuthInterfaceHost = CONTAINER_OF(deviceObject->service,
124 struct HdfFingerprintAuthInterfaceHost, ioService);
125 if (hdfFingerprintAuthInterfaceHost == nullptr) {
126 IAM_LOGE("hdfFaceAuthInterfaceHost is nullptr");
127 return;
128 }
129 delete hdfFingerprintAuthInterfaceHost;
130 IAM_LOGI("success");
131 }
132
133 struct HdfDriverEntry g_fingerprintAuthInterfaceDriverEntry = {
134 .moduleVersion = 1,
135 .moduleName = "drivers_peripheral_fingerprint_auth",
136 .Bind = HdfFingerprintAuthInterfaceDriverBind,
137 .Init = HdfFingerprintAuthInterfaceDriverInit,
138 .Release = HdfFingerprintAuthInterfaceDriverRelease,
139 };
140 } // namespace
141
142 #ifdef __cplusplus
143 extern "C" {
144 #endif /* __cplusplus */
145 HDF_INIT(g_fingerprintAuthInterfaceDriverEntry);
146 #ifdef __cplusplus
147 }
148 #endif /* __cplusplus */
149
150