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 "v1_1/pin_auth_interface_stub.h"
21 #include "pin_auth.h"
22 #include "iam_logger.h"
23 #include "iam_ptr.h"
24
25 #define LOG_LABEL OHOS::UserIam::Common::LABEL_PIN_AUTH_HDI
26
27 struct HdfPinAuthInterfaceHost {
28 struct IDeviceIoService ioService;
29 OHOS::sptr<OHOS::IRemoteObject> stub;
30 };
31
PinAuthInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int32_t PinAuthInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
33 struct HdfSBuf *reply)
34 {
35 IAM_LOGI("start");
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 auto *hdfPinAuthInterfaceHost = CONTAINER_OF(client->device->service,
42 struct HdfPinAuthInterfaceHost, ioService);
43 if (hdfPinAuthInterfaceHost == nullptr || hdfPinAuthInterfaceHost->stub == nullptr) {
44 IAM_LOGE("hdfPinAuthInterfaceHost 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
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 hdfPinAuthInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
62 }
63
HdfPinAuthInterfaceDriverInit(struct HdfDeviceObject * deviceObject)64 static int HdfPinAuthInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
65 {
66 IAM_LOGI("start");
67 if (deviceObject == nullptr) {
68 IAM_LOGE("deviceObject is nullptr");
69 return HDF_ERR_INVALID_PARAM;
70 }
71 std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi =
72 OHOS::UserIam::Common::MakeShared<OHOS::UserIam::PinAuth::PinAuth>();
73 constexpr uint32_t SUCCESS = 0;
74 if (pinHdi == nullptr || pinHdi->Init() != SUCCESS) {
75 IAM_LOGE("Pin hal init failed");
76 return HDF_FAILURE;
77 }
78 if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_USERAUTH)) {
79 IAM_LOGE("set pin auth hdf class failed");
80 return HDF_FAILURE;
81 }
82 return HDF_SUCCESS;
83 }
84
HdfPinAuthInterfaceDriverBind(struct HdfDeviceObject * deviceObject)85 static int HdfPinAuthInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
86 {
87 IAM_LOGI("start");
88 if (deviceObject == nullptr) {
89 IAM_LOGE("deviceObject is nullptr");
90 return HDF_ERR_INVALID_PARAM;
91 }
92 auto *hdfPinAuthInterfaceHost = new (std::nothrow) HdfPinAuthInterfaceHost;
93 if (hdfPinAuthInterfaceHost == nullptr) {
94 IAM_LOGE("failed to create create HdfPinAuthInterfaceHost object");
95 return HDF_FAILURE;
96 }
97
98 hdfPinAuthInterfaceHost->ioService.Dispatch = PinAuthInterfaceDriverDispatch;
99 hdfPinAuthInterfaceHost->ioService.Open = NULL;
100 hdfPinAuthInterfaceHost->ioService.Release = NULL;
101
102 auto serviceImpl = OHOS::HDI::PinAuth::V1_1::IPinAuthInterface::Get(true);
103 if (serviceImpl == nullptr) {
104 IAM_LOGE("failed to get of implement service");
105 delete hdfPinAuthInterfaceHost;
106 return HDF_FAILURE;
107 }
108
109 hdfPinAuthInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(
110 serviceImpl, OHOS::HDI::PinAuth::V1_1::IPinAuthInterface::GetDescriptor());
111 if (hdfPinAuthInterfaceHost->stub == nullptr) {
112 IAM_LOGE("failed to get stub object");
113 delete hdfPinAuthInterfaceHost;
114 return HDF_FAILURE;
115 }
116
117 deviceObject->service = &hdfPinAuthInterfaceHost->ioService;
118 IAM_LOGI("success");
119 return HDF_SUCCESS;
120 }
121
HdfPinAuthInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)122 static void HdfPinAuthInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
123 {
124 IAM_LOGI("start");
125 if (deviceObject == nullptr || deviceObject->service == nullptr) {
126 IAM_LOGE("deviceObject is invalid");
127 return;
128 }
129 auto *hdfPinAuthInterfaceHost = CONTAINER_OF(deviceObject->service,
130 struct HdfPinAuthInterfaceHost, ioService);
131 if (hdfPinAuthInterfaceHost == nullptr) {
132 IAM_LOGE("hdfPinAuthInterfaceHost is nullptr");
133 return;
134 }
135 delete hdfPinAuthInterfaceHost;
136 IAM_LOGI("success");
137 }
138
139 static struct HdfDriverEntry g_pinAuthInterfaceDriverEntry = {
140 .moduleVersion = 1,
141 .moduleName = "drivers_peripheral_pin_auth",
142 .Bind = HdfPinAuthInterfaceDriverBind,
143 .Init = HdfPinAuthInterfaceDriverInit,
144 .Release = HdfPinAuthInterfaceDriverRelease,
145 };
146
147 #ifdef __cplusplus
148 extern "C" {
149 #endif /* __cplusplus */
150 HDF_INIT(g_pinAuthInterfaceDriverEntry);
151 #ifdef __cplusplus
152 }
153 #endif /* __cplusplus */
154