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