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_device_desc.h"
17094332d3Sopenharmony_ci#include "hdf_sbuf_ipc.h"
18094332d3Sopenharmony_ci#include "battery_log.h"
19094332d3Sopenharmony_ci#include "v2_0/battery_interface_stub.h"
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ciusing namespace OHOS::HDI::Battery::V2_0;
22094332d3Sopenharmony_ciusing namespace OHOS::HDI::Battery;
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_cinamespace {
25094332d3Sopenharmony_cistruct HdfBatteryInterfaceHost {
26094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
27094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
28094332d3Sopenharmony_ci};
29094332d3Sopenharmony_ci}
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cistatic int32_t BatteryInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
32094332d3Sopenharmony_ci    struct HdfSBuf *reply)
33094332d3Sopenharmony_ci{
34094332d3Sopenharmony_ci    auto *hdfBatteryInterfaceHost = CONTAINER_OF(client->device->service, struct HdfBatteryInterfaceHost, ioService);
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_ci    OHOS::MessageParcel *dataParcel = nullptr;
37094332d3Sopenharmony_ci    OHOS::MessageParcel *replyParcel = nullptr;
38094332d3Sopenharmony_ci    OHOS::MessageOption option;
39094332d3Sopenharmony_ci
40094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
41094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "invalid data sbuf object to dispatch");
42094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
43094332d3Sopenharmony_ci    }
44094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
45094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "invalid reply sbuf object to dispatch");
46094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
47094332d3Sopenharmony_ci    }
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_ci    return hdfBatteryInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
50094332d3Sopenharmony_ci}
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_cistatic int32_t HdfBatteryInterfaceDriverInit([[maybe_unused]] struct HdfDeviceObject *deviceObject)
53094332d3Sopenharmony_ci{
54094332d3Sopenharmony_ci    return HDF_SUCCESS;
55094332d3Sopenharmony_ci}
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_cistatic int32_t HdfBatteryInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
58094332d3Sopenharmony_ci{
59094332d3Sopenharmony_ci    auto *hdfBatteryInterfaceHost = new (std::nothrow) HdfBatteryInterfaceHost;
60094332d3Sopenharmony_ci    if (hdfBatteryInterfaceHost == nullptr) {
61094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "%{public}s: failed to create HdfBatteryInterfaceHost object", __func__);
62094332d3Sopenharmony_ci        return HDF_FAILURE;
63094332d3Sopenharmony_ci    }
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    hdfBatteryInterfaceHost->ioService.Dispatch = BatteryInterfaceDriverDispatch;
66094332d3Sopenharmony_ci    hdfBatteryInterfaceHost->ioService.Open = nullptr;
67094332d3Sopenharmony_ci    hdfBatteryInterfaceHost->ioService.Release = nullptr;
68094332d3Sopenharmony_ci
69094332d3Sopenharmony_ci    auto serviceImpl = IBatteryInterface::Get(true);
70094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
71094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "%{public}s: failed to get of implement service", __func__);
72094332d3Sopenharmony_ci        delete hdfBatteryInterfaceHost;
73094332d3Sopenharmony_ci        return HDF_FAILURE;
74094332d3Sopenharmony_ci    }
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_ci    hdfBatteryInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
77094332d3Sopenharmony_ci        IBatteryInterface::GetDescriptor());
78094332d3Sopenharmony_ci    if (hdfBatteryInterfaceHost->stub == nullptr) {
79094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "%{public}s: failed to get stub object", __func__);
80094332d3Sopenharmony_ci        delete hdfBatteryInterfaceHost;
81094332d3Sopenharmony_ci        return HDF_FAILURE;
82094332d3Sopenharmony_ci    }
83094332d3Sopenharmony_ci
84094332d3Sopenharmony_ci    deviceObject->service = &hdfBatteryInterfaceHost->ioService;
85094332d3Sopenharmony_ci    return HDF_SUCCESS;
86094332d3Sopenharmony_ci}
87094332d3Sopenharmony_ci
88094332d3Sopenharmony_cistatic void HdfBatteryInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
89094332d3Sopenharmony_ci{
90094332d3Sopenharmony_ci    if (deviceObject->service == nullptr) {
91094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "HdfBatteryInterfaceDriverRelease not initted");
92094332d3Sopenharmony_ci        return;
93094332d3Sopenharmony_ci    }
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ci    auto *hdfBatteryInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfBatteryInterfaceHost, ioService);
96094332d3Sopenharmony_ci    delete hdfBatteryInterfaceHost;
97094332d3Sopenharmony_ci}
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_cistatic struct HdfDriverEntry g_batteryInterfaceDriverEntry = {
100094332d3Sopenharmony_ci    .moduleVersion = 1,
101094332d3Sopenharmony_ci    .moduleName = "battery_interface_service",
102094332d3Sopenharmony_ci    .Bind = HdfBatteryInterfaceDriverBind,
103094332d3Sopenharmony_ci    .Init = HdfBatteryInterfaceDriverInit,
104094332d3Sopenharmony_ci    .Release = HdfBatteryInterfaceDriverRelease,
105094332d3Sopenharmony_ci};
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_ci#ifdef __cplusplus
108094332d3Sopenharmony_ciextern "C" {
109094332d3Sopenharmony_ci#endif /* __cplusplus */
110094332d3Sopenharmony_ciHDF_INIT(g_batteryInterfaceDriverEntry);
111094332d3Sopenharmony_ci#ifdef __cplusplus
112094332d3Sopenharmony_ci}
113094332d3Sopenharmony_ci#endif /* __cplusplus */
114