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