1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 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_2/power_interface_stub.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ci#define HDF_LOG_TAG PowerInterfaceDriver
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ciusing namespace OHOS::HDI::Power::V1_2;
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cinamespace {
27094332d3Sopenharmony_cistruct HdfPowerInterfaceHost {
28094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
29094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
30094332d3Sopenharmony_ci};
31094332d3Sopenharmony_ci}
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_cistatic int32_t PowerInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
34094332d3Sopenharmony_ci    struct HdfSBuf *reply)
35094332d3Sopenharmony_ci{
36094332d3Sopenharmony_ci    auto *hdfPowerInterfaceHost = CONTAINER_OF(client->device->service, struct HdfPowerInterfaceHost, ioService);
37094332d3Sopenharmony_ci
38094332d3Sopenharmony_ci    OHOS::MessageParcel *dataParcel = nullptr;
39094332d3Sopenharmony_ci    OHOS::MessageParcel *replyParcel = nullptr;
40094332d3Sopenharmony_ci    OHOS::MessageOption option;
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
43094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
44094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
45094332d3Sopenharmony_ci    }
46094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
47094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
48094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
49094332d3Sopenharmony_ci    }
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    return hdfPowerInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
52094332d3Sopenharmony_ci}
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_cistatic int HdfPowerInterfaceDriverInit([[maybe_unused]] struct HdfDeviceObject *deviceObject)
55094332d3Sopenharmony_ci{
56094332d3Sopenharmony_ci    HDF_LOGI("HdfPowerInterfaceDriverInit enter");
57094332d3Sopenharmony_ci    return HDF_SUCCESS;
58094332d3Sopenharmony_ci}
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_cistatic int HdfPowerInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
61094332d3Sopenharmony_ci{
62094332d3Sopenharmony_ci    HDF_LOGI("HdfPowerInterfaceDriverBind enter");
63094332d3Sopenharmony_ci
64094332d3Sopenharmony_ci    auto *hdfPowerInterfaceHost = new (std::nothrow) HdfPowerInterfaceHost;
65094332d3Sopenharmony_ci    if (hdfPowerInterfaceHost == nullptr) {
66094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to create HdfPowerInterfaceHost object", __func__);
67094332d3Sopenharmony_ci        return HDF_FAILURE;
68094332d3Sopenharmony_ci    }
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci    hdfPowerInterfaceHost->ioService.Dispatch = PowerInterfaceDriverDispatch;
71094332d3Sopenharmony_ci    hdfPowerInterfaceHost->ioService.Open = NULL;
72094332d3Sopenharmony_ci    hdfPowerInterfaceHost->ioService.Release = NULL;
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ci    auto serviceImpl = IPowerInterface::Get(true);
75094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
76094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
77094332d3Sopenharmony_ci        delete hdfPowerInterfaceHost;
78094332d3Sopenharmony_ci        return HDF_FAILURE;
79094332d3Sopenharmony_ci    }
80094332d3Sopenharmony_ci
81094332d3Sopenharmony_ci    hdfPowerInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
82094332d3Sopenharmony_ci        IPowerInterface::GetDescriptor());
83094332d3Sopenharmony_ci    if (hdfPowerInterfaceHost->stub == nullptr) {
84094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get stub object", __func__);
85094332d3Sopenharmony_ci        delete hdfPowerInterfaceHost;
86094332d3Sopenharmony_ci        return HDF_FAILURE;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    deviceObject->service = &hdfPowerInterfaceHost->ioService;
90094332d3Sopenharmony_ci    return HDF_SUCCESS;
91094332d3Sopenharmony_ci}
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_cistatic void HdfPowerInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
94094332d3Sopenharmony_ci{
95094332d3Sopenharmony_ci    HDF_LOGI("HdfPowerInterfaceDriverRelease enter");
96094332d3Sopenharmony_ci    if (deviceObject->service == nullptr) {
97094332d3Sopenharmony_ci        HDF_LOGE("HdfPowerInterfaceDriverRelease not initted");
98094332d3Sopenharmony_ci        return;
99094332d3Sopenharmony_ci    }
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_ci    auto *hdfPowerInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfPowerInterfaceHost, ioService);
102094332d3Sopenharmony_ci    delete hdfPowerInterfaceHost;
103094332d3Sopenharmony_ci}
104094332d3Sopenharmony_ci
105094332d3Sopenharmony_cistatic struct HdfDriverEntry g_powerinterfaceDriverEntry = {
106094332d3Sopenharmony_ci    .moduleVersion = 1,
107094332d3Sopenharmony_ci    .moduleName = "power_interface_service",
108094332d3Sopenharmony_ci    .Bind = HdfPowerInterfaceDriverBind,
109094332d3Sopenharmony_ci    .Init = HdfPowerInterfaceDriverInit,
110094332d3Sopenharmony_ci    .Release = HdfPowerInterfaceDriverRelease,
111094332d3Sopenharmony_ci};
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_ci#ifdef __cplusplus
114094332d3Sopenharmony_ciextern "C" {
115094332d3Sopenharmony_ci#endif /* __cplusplus */
116094332d3Sopenharmony_ciHDF_INIT(g_powerinterfaceDriverEntry);
117094332d3Sopenharmony_ci#ifdef __cplusplus
118094332d3Sopenharmony_ci}
119094332d3Sopenharmony_ci#endif /* __cplusplus */
120