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 #include "battery_log.h"
20 #include "v2_0/battery_interface_stub.h"
21
22 using namespace OHOS::HDI::Battery::V2_0;
23 using namespace OHOS::HDI::Battery;
24
25 namespace {
26 struct HdfBatteryInterfaceHost {
27 struct IDeviceIoService ioService;
28 OHOS::sptr<OHOS::IRemoteObject> stub;
29 };
30 }
31
BatteryInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int32_t BatteryInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
33 struct HdfSBuf *reply)
34 {
35 auto *hdfBatteryInterfaceHost = CONTAINER_OF(client->device->service, struct HdfBatteryInterfaceHost, 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 BATTERY_HILOGE(COMP_HDI, "invalid data sbuf object to dispatch");
43 return HDF_ERR_INVALID_PARAM;
44 }
45 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
46 BATTERY_HILOGE(COMP_HDI, "invalid reply sbuf object to dispatch");
47 return HDF_ERR_INVALID_PARAM;
48 }
49
50 return hdfBatteryInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
51 }
52
53 static int32_t HdfBatteryInterfaceDriverInit([[maybe_unused]] struct HdfDeviceObject *deviceObject)
54 {
55 return HDF_SUCCESS;
56 }
57
HdfBatteryInterfaceDriverBind(struct HdfDeviceObject * deviceObject)58 static int32_t HdfBatteryInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
59 {
60 auto *hdfBatteryInterfaceHost = new (std::nothrow) HdfBatteryInterfaceHost;
61 if (hdfBatteryInterfaceHost == nullptr) {
62 BATTERY_HILOGE(COMP_HDI, "%{public}s: failed to create HdfBatteryInterfaceHost object", __func__);
63 return HDF_FAILURE;
64 }
65
66 hdfBatteryInterfaceHost->ioService.Dispatch = BatteryInterfaceDriverDispatch;
67 hdfBatteryInterfaceHost->ioService.Open = nullptr;
68 hdfBatteryInterfaceHost->ioService.Release = nullptr;
69
70 auto serviceImpl = IBatteryInterface::Get(true);
71 if (serviceImpl == nullptr) {
72 BATTERY_HILOGE(COMP_HDI, "%{public}s: failed to get of implement service", __func__);
73 delete hdfBatteryInterfaceHost;
74 return HDF_FAILURE;
75 }
76
77 hdfBatteryInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
78 IBatteryInterface::GetDescriptor());
79 if (hdfBatteryInterfaceHost->stub == nullptr) {
80 BATTERY_HILOGE(COMP_HDI, "%{public}s: failed to get stub object", __func__);
81 delete hdfBatteryInterfaceHost;
82 return HDF_FAILURE;
83 }
84
85 deviceObject->service = &hdfBatteryInterfaceHost->ioService;
86 return HDF_SUCCESS;
87 }
88
HdfBatteryInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)89 static void HdfBatteryInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
90 {
91 if (deviceObject->service == nullptr) {
92 BATTERY_HILOGE(COMP_HDI, "HdfBatteryInterfaceDriverRelease not initted");
93 return;
94 }
95
96 auto *hdfBatteryInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfBatteryInterfaceHost, ioService);
97 delete hdfBatteryInterfaceHost;
98 }
99
100 static struct HdfDriverEntry g_batteryInterfaceDriverEntry = {
101 .moduleVersion = 1,
102 .moduleName = "battery_interface_service",
103 .Bind = HdfBatteryInterfaceDriverBind,
104 .Init = HdfBatteryInterfaceDriverInit,
105 .Release = HdfBatteryInterfaceDriverRelease,
106 };
107
108 #ifdef __cplusplus
109 extern "C" {
110 #endif /* __cplusplus */
111 HDF_INIT(g_batteryInterfaceDriverEntry);
112 #ifdef __cplusplus
113 }
114 #endif /* __cplusplus */
115