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