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