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