1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <hdf_base.h>
17094332d3Sopenharmony_ci#include <hdf_device_desc.h>
18094332d3Sopenharmony_ci#include <hdf_log.h>
19094332d3Sopenharmony_ci#include <hdf_sbuf_ipc.h>
20094332d3Sopenharmony_ci#include "v1_0/secure_element_interface_stub.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ci#ifdef SE_DRIVER_USE_CA
23094332d3Sopenharmony_ci#include "secure_element_ca_proxy.h"
24094332d3Sopenharmony_ci#endif
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_ci#define HDF_LOG_TAG hdf_se
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_ci#ifdef LOG_DOMAIN
29094332d3Sopenharmony_ci#undef LOG_DOMAIN
30094332d3Sopenharmony_ci#endif
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_ci#define LOG_DOMAIN 0xD000305
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_ciusing OHOS::HDI::SecureElement::V1_0::ISecureElementInterface;
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_cistruct HdfSeInterfaceHost {
37094332d3Sopenharmony_ci    struct IDeviceIoService ioservice;
38094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
39094332d3Sopenharmony_ci};
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_cistatic int32_t SeInterfaceDriverDispatch(struct HdfDeviceIoClient* client, int cmdId, struct HdfSBuf* data,
42094332d3Sopenharmony_ci    struct HdfSBuf* reply)
43094332d3Sopenharmony_ci{
44094332d3Sopenharmony_ci    auto* hdfSeInterfaceHost =
45094332d3Sopenharmony_ci        CONTAINER_OF(client->device->service, struct HdfSeInterfaceHost, ioservice);
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_ci    OHOS::MessageParcel* dataParcel = nullptr;
48094332d3Sopenharmony_ci    OHOS::MessageParcel* replyParcel = nullptr;
49094332d3Sopenharmony_ci    OHOS::MessageOption option;
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
52094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
53094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
54094332d3Sopenharmony_ci    }
55094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
56094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
57094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
58094332d3Sopenharmony_ci    }
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_ci    return hdfSeInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
61094332d3Sopenharmony_ci}
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_cistatic int HdfSeInterfaceDriverInit(struct HdfDeviceObject* deviceObject)
64094332d3Sopenharmony_ci{
65094332d3Sopenharmony_ci    HDF_LOGE("%{public}s: Enter", __func__);
66094332d3Sopenharmony_ci#ifdef SE_DRIVER_USE_CA
67094332d3Sopenharmony_ci    int ret = OHOS::HDI::SecureElement::SecureElementCaProxy::GetInstance().VendorSecureElementCaOnStart();
68094332d3Sopenharmony_ci    if (ret != SECURE_ELEMENT_CA_RET_OK) {
69094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Failed", __func__);
70094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
71094332d3Sopenharmony_ci    }
72094332d3Sopenharmony_ci#endif
73094332d3Sopenharmony_ci    return HDF_SUCCESS;
74094332d3Sopenharmony_ci}
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_cistatic int HdfSeInterfaceDriverBind(struct HdfDeviceObject* deviceObject)
77094332d3Sopenharmony_ci{
78094332d3Sopenharmony_ci    auto* hdfSeInterfaceHost = new (std::nothrow) HdfSeInterfaceHost;
79094332d3Sopenharmony_ci    if (hdfSeInterfaceHost == nullptr) {
80094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to create HdfSeInterfaceDriverBind Object!", __func__);
81094332d3Sopenharmony_ci        return HDF_FAILURE;
82094332d3Sopenharmony_ci    }
83094332d3Sopenharmony_ci
84094332d3Sopenharmony_ci    hdfSeInterfaceHost->ioservice.Dispatch = SeInterfaceDriverDispatch;
85094332d3Sopenharmony_ci    hdfSeInterfaceHost->ioservice.Open = nullptr;
86094332d3Sopenharmony_ci    hdfSeInterfaceHost->ioservice.Release = nullptr;
87094332d3Sopenharmony_ci
88094332d3Sopenharmony_ci    auto serviceImpl = ISecureElementInterface::Get(true);
89094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
90094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
91094332d3Sopenharmony_ci        delete hdfSeInterfaceHost;
92094332d3Sopenharmony_ci        return HDF_FAILURE;
93094332d3Sopenharmony_ci    }
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ci    hdfSeInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
96094332d3Sopenharmony_ci        ISecureElementInterface::GetDescriptor());
97094332d3Sopenharmony_ci    if (hdfSeInterfaceHost->stub == nullptr) {
98094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get stub object", __func__);
99094332d3Sopenharmony_ci        delete hdfSeInterfaceHost;
100094332d3Sopenharmony_ci        return HDF_FAILURE;
101094332d3Sopenharmony_ci    }
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_ci    deviceObject->service = &hdfSeInterfaceHost->ioservice;
104094332d3Sopenharmony_ci    return HDF_SUCCESS;
105094332d3Sopenharmony_ci}
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_cistatic void HdfSeInterfaceDriverRelease(struct HdfDeviceObject* deviceObject)
108094332d3Sopenharmony_ci{
109094332d3Sopenharmony_ci    if (deviceObject->service == nullptr) {
110094332d3Sopenharmony_ci        HDF_LOGE("HdfSeInterfaceDriverRelease not initted");
111094332d3Sopenharmony_ci        return;
112094332d3Sopenharmony_ci    }
113094332d3Sopenharmony_ci
114094332d3Sopenharmony_ci    auto* hdfSeInterfaceHost =
115094332d3Sopenharmony_ci        CONTAINER_OF(deviceObject->service, struct HdfSeInterfaceHost, ioservice);
116094332d3Sopenharmony_ci    delete hdfSeInterfaceHost;
117094332d3Sopenharmony_ci}
118094332d3Sopenharmony_ci
119094332d3Sopenharmony_cistatic struct HdfDriverEntry g_seInterfaceDriverEntry = {
120094332d3Sopenharmony_ci    .moduleVersion = 1,
121094332d3Sopenharmony_ci    .moduleName = "secure_element_service",
122094332d3Sopenharmony_ci    .Bind = HdfSeInterfaceDriverBind,
123094332d3Sopenharmony_ci    .Init = HdfSeInterfaceDriverInit,
124094332d3Sopenharmony_ci    .Release = HdfSeInterfaceDriverRelease,
125094332d3Sopenharmony_ci};
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_ci#ifdef __cplusplus
128094332d3Sopenharmony_ciextern "C" {
129094332d3Sopenharmony_ci#endif /* __cplusplus */
130094332d3Sopenharmony_ciHDF_INIT(g_seInterfaceDriverEntry);
131094332d3Sopenharmony_ci#ifdef __cplusplus
132094332d3Sopenharmony_ci}
133094332d3Sopenharmony_ci#endif /* __cplusplus */
134