• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_log.h>
19 #include <hdf_sbuf_ipc.h>
20 #include "v1_0/secure_element_interface_stub.h"
21 
22 #define HDF_LOG_TAG hdf_se
23 
24 using OHOS::HDI::SecureElement::V1_0::ISecureElementInterface;
25 
26 struct HdfSeInterfaceHost {
27     struct IDeviceIoService ioservice;
28     OHOS::sptr<OHOS::IRemoteObject> stub;
29 };
30 
SeInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)31 static int32_t SeInterfaceDriverDispatch(struct HdfDeviceIoClient* client, int cmdId, struct HdfSBuf* data,
32     struct HdfSBuf* reply)
33 {
34     auto* hdfSeInterfaceHost =
35         CONTAINER_OF(client->device->service, struct HdfSeInterfaceHost, ioservice);
36 
37     OHOS::MessageParcel* dataParcel = nullptr;
38     OHOS::MessageParcel* replyParcel = nullptr;
39     OHOS::MessageOption option;
40 
41     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
42         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
43         return HDF_ERR_INVALID_PARAM;
44     }
45     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
46         HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
47         return HDF_ERR_INVALID_PARAM;
48     }
49 
50     return hdfSeInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
51 }
52 
HdfSeInterfaceDriverInit(struct HdfDeviceObject * deviceObject)53 static int HdfSeInterfaceDriverInit(struct HdfDeviceObject* deviceObject)
54 {
55     return HDF_SUCCESS;
56 }
57 
HdfSeInterfaceDriverBind(struct HdfDeviceObject * deviceObject)58 static int HdfSeInterfaceDriverBind(struct HdfDeviceObject* deviceObject)
59 {
60     auto* hdfSeInterfaceHost = new (std::nothrow) HdfSeInterfaceHost;
61     if (hdfSeInterfaceHost == nullptr) {
62         HDF_LOGE("%{public}s: failed to create HdfSeInterfaceDriverBind Object!", __func__);
63         return HDF_FAILURE;
64     }
65 
66     hdfSeInterfaceHost->ioservice.Dispatch = SeInterfaceDriverDispatch;
67     hdfSeInterfaceHost->ioservice.Open = nullptr;
68     hdfSeInterfaceHost->ioservice.Release = nullptr;
69 
70     auto serviceImpl = ISecureElementInterface::Get(true);
71     if (serviceImpl == nullptr) {
72         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
73         delete hdfSeInterfaceHost;
74         return HDF_FAILURE;
75     }
76 
77     hdfSeInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
78         ISecureElementInterface::GetDescriptor());
79     if (hdfSeInterfaceHost->stub == nullptr) {
80         HDF_LOGE("%{public}s: failed to get stub object", __func__);
81         delete hdfSeInterfaceHost;
82         return HDF_FAILURE;
83     }
84 
85     deviceObject->service = &hdfSeInterfaceHost->ioservice;
86     return HDF_SUCCESS;
87 }
88 
HdfSeInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)89 static void HdfSeInterfaceDriverRelease(struct HdfDeviceObject* deviceObject)
90 {
91     if (deviceObject->service == nullptr) {
92         HDF_LOGE("HdfSeInterfaceDriverRelease not initted");
93         return;
94     }
95 
96     auto* hdfSeInterfaceHost =
97         CONTAINER_OF(deviceObject->service, struct HdfSeInterfaceHost, ioservice);
98     delete hdfSeInterfaceHost;
99 }
100 
101 static struct HdfDriverEntry g_seInterfaceDriverEntry = {
102     .moduleVersion = 1,
103     .moduleName = "secure_element_service",
104     .Bind = HdfSeInterfaceDriverBind,
105     .Init = HdfSeInterfaceDriverInit,
106     .Release = HdfSeInterfaceDriverRelease,
107 };
108 
109 #ifdef __cplusplus
110 extern "C" {
111 #endif /* __cplusplus */
112 HDF_INIT(g_seInterfaceDriverEntry);
113 #ifdef __cplusplus
114 }
115 #endif /* __cplusplus */
116