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 "useriam_common.h"
22
23 #include "v1_2/user_auth_interface_stub.h"
24 #include "v1_2/user_auth_interface_service.h"
25
26 #define LOG_LABEL OHOS::UserIam::Common::LABEL_USER_AUTH_HDI
27
28 namespace {
29 struct HdfUserAuthInterfaceHost {
30 struct IDeviceIoService ioService;
31 OHOS::sptr<OHOS::IRemoteObject> stub;
32 };
33
UserAuthInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)34 int32_t UserAuthInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
35 struct HdfSBuf *reply)
36 {
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 *hdfUserAuthInterfaceHost = CONTAINER_OF(client->device->service, struct HdfUserAuthInterfaceHost, ioService);
44 if (hdfUserAuthInterfaceHost == nullptr || hdfUserAuthInterfaceHost->stub == nullptr) {
45 IAM_LOGE("hdfUserAuthInterfaceHost 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 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
53 IAM_LOGE("invalid data sbuf object to dispatch");
54 return HDF_ERR_INVALID_PARAM;
55 }
56 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
57 IAM_LOGE("invalid reply sbuf object to dispatch");
58 return HDF_ERR_INVALID_PARAM;
59 }
60
61 return hdfUserAuthInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
62 }
63
HdfUserAuthInterfaceDriverInit(struct HdfDeviceObject * deviceObject)64 int HdfUserAuthInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
65 {
66 IAM_LOGI("HdfUserAuthInterfaceDriverInit enter");
67 return HDF_SUCCESS;
68 }
69
HdfUserAuthInterfaceDriverBind(struct HdfDeviceObject * deviceObject)70 int HdfUserAuthInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
71 {
72 IAM_LOGI("HdfUserAuthInterfaceDriverBind enter");
73 if (deviceObject == nullptr) {
74 IAM_LOGE("deviceObject is nullptr");
75 return HDF_ERR_INVALID_PARAM;
76 }
77 auto *hdfUserAuthInterfaceHost = new (std::nothrow) HdfUserAuthInterfaceHost;
78 if (hdfUserAuthInterfaceHost == nullptr) {
79 IAM_LOGE("failed to create create HdfUserAuthInterfaceHost object");
80 return HDF_FAILURE;
81 }
82
83 hdfUserAuthInterfaceHost->ioService.Dispatch = UserAuthInterfaceDriverDispatch;
84 hdfUserAuthInterfaceHost->ioService.Open = nullptr;
85 hdfUserAuthInterfaceHost->ioService.Release = nullptr;
86
87 OHOS::sptr<OHOS::HDI::UserAuth::V1_2::UserAuthInterfaceService> serviceImpl(
88 new (std::nothrow) OHOS::HDI::UserAuth::V1_2::UserAuthInterfaceService());
89 if (serviceImpl == nullptr) {
90 IAM_LOGE("failed to get of implement service");
91 delete hdfUserAuthInterfaceHost;
92 return HDF_FAILURE;
93 }
94
95 hdfUserAuthInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(
96 serviceImpl, OHOS::HDI::UserAuth::V1_2::IUserAuthInterface::GetDescriptor());
97 if (hdfUserAuthInterfaceHost->stub == nullptr) {
98 IAM_LOGE("failed to get stub object");
99 delete hdfUserAuthInterfaceHost;
100 return HDF_FAILURE;
101 }
102
103 deviceObject->service = &hdfUserAuthInterfaceHost->ioService;
104 static_cast<void>(serviceImpl->Init());
105 return HDF_SUCCESS;
106 }
107
HdfUserAuthInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)108 void HdfUserAuthInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
109 {
110 IAM_LOGI("HdfUserAuthInterfaceDriverRelease enter");
111 if (deviceObject == nullptr || deviceObject->service == nullptr) {
112 IAM_LOGE("deviceObject is invalid");
113 return;
114 }
115 auto *hdfUserAuthInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfUserAuthInterfaceHost, ioService);
116 if (hdfUserAuthInterfaceHost == nullptr) {
117 IAM_LOGE("hdfUserAuthInterfaceHost is nullptr");
118 return;
119 }
120 delete hdfUserAuthInterfaceHost;
121 }
122
123 struct HdfDriverEntry g_userAuthInterfaceDriverEntry = {
124 .moduleVersion = 1,
125 .moduleName = "drivers_peripheral_user_auth",
126 .Bind = HdfUserAuthInterfaceDriverBind,
127 .Init = HdfUserAuthInterfaceDriverInit,
128 .Release = HdfUserAuthInterfaceDriverRelease,
129 };
130 } // namespace
131
132 #ifdef __cplusplus
133 extern "C" {
134 #endif /* __cplusplus */
135 HDF_INIT(g_userAuthInterfaceDriverEntry);
136 #ifdef __cplusplus
137 }
138 #endif /* __cplusplus */
139