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