• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "battery_srv_stub.h"
17 
18 #include "errors.h"
19 #include "xcollie/xcollie.h"
20 #include "xcollie/xcollie_define.h"
21 #include "ipc_object_stub.h"
22 #include "message_parcel.h"
23 #include "battery_info.h"
24 #include "battery_log.h"
25 #include "battery_manager_ipc_interface_code.h"
26 #include "power_mgr_errors.h"
27 #include "power_common.h"
28 #include "string_ex.h"
29 
30 namespace OHOS {
31 namespace PowerMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int BatterySrvStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
33 {
34     BATTERY_HILOGD(FEATURE_BATT_INFO, "cmd = %{public}d, flags = %{public}d", code, option.GetFlags());
35     std::u16string descriptor = BatterySrvStub::GetDescriptor();
36     std::u16string remoteDescriptor = data.ReadInterfaceToken();
37     if (descriptor != remoteDescriptor) {
38         BATTERY_HILOGE(FEATURE_BATT_INFO, "Descriptor is not matched");
39         return E_GET_POWER_SERVICE_FAILED;
40     }
41 
42     const int DFX_DELAY_MS = 10000;
43     int id = HiviewDFX::XCollie::GetInstance().SetTimer("BatteryManagerCallbackStub", DFX_DELAY_MS, nullptr, nullptr,
44         HiviewDFX::XCOLLIE_FLAG_NOOP);
45     int32_t ret = CheckRequestCode(code, data, reply, option);
46     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
47     return ret;
48 }
49 
CheckRequestCode(const uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int32_t BatterySrvStub::CheckRequestCode(const uint32_t code, MessageParcel& data, MessageParcel& reply,
51     MessageOption& option)
52 {
53     switch (code) {
54         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_CAPACITY): {
55             return GetCapacityStub(reply);
56         }
57         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_CHARGING_STATUS): {
58             return GetChargingStatusStub(reply);
59         }
60         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_HEALTH_STATUS): {
61             return GetHealthStatusStub(reply);
62         }
63         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_PLUG_TYPE): {
64             return GetPluggedTypeStub(reply);
65         }
66         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_VOLTAGE): {
67             return GetVoltageStub(reply);
68         }
69         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_PRESENT): {
70             return GetPresentStub(reply);
71         }
72         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_TEMPERATURE): {
73             return GetBatteryTemperatureStub(reply);
74         }
75         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_LEVEL): {
76             return GetBatteryCapacityLevelStub(reply);
77         }
78         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_REMAINING_CHARGE_TIME): {
79             return GetRemainingChargeTimeStub(reply);
80         }
81         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_TECHNOLOGY): {
82             return GetTechnologyStub(reply);
83         }
84         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_CURRENT_NOW): {
85             return GetNowCurrentStub(reply);
86         }
87         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_REMAIN_ENERGY): {
88             return GetRemainEnergyStub(reply);
89         }
90         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_TOTAL_ENERGY): {
91             return GetTotalEnergyStub(reply);
92         }
93         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_CURRENT_AVERAGE): {
94             return GetCurrentAverageStub(reply);
95         }
96         default: {
97             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
98         }
99     }
100 }
101 
GetCapacityStub(MessageParcel & reply)102 int32_t BatterySrvStub::GetCapacityStub(MessageParcel& reply)
103 {
104     int32_t ret = GetCapacity();
105     WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
106     return ERR_OK;
107 }
108 
GetChargingStatusStub(MessageParcel & reply)109 int32_t BatterySrvStub::GetChargingStatusStub(MessageParcel& reply)
110 {
111     BatteryChargeState ret = GetChargingStatus();
112     WRITE_PARCEL_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
113     return ERR_OK;
114 }
115 
GetHealthStatusStub(MessageParcel & reply)116 int32_t BatterySrvStub::GetHealthStatusStub(MessageParcel& reply)
117 {
118     BatteryHealthState ret = GetHealthStatus();
119     WRITE_PARCEL_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
120     return ERR_OK;
121 }
122 
GetPluggedTypeStub(MessageParcel & reply)123 int32_t BatterySrvStub::GetPluggedTypeStub(MessageParcel& reply)
124 {
125     BatteryPluggedType ret = GetPluggedType();
126     WRITE_PARCEL_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
127     return ERR_OK;
128 }
129 
GetVoltageStub(MessageParcel & reply)130 int32_t BatterySrvStub::GetVoltageStub(MessageParcel& reply)
131 {
132     int32_t ret = GetVoltage();
133     WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
134     return ERR_OK;
135 }
136 
GetPresentStub(MessageParcel & reply)137 int32_t BatterySrvStub::GetPresentStub(MessageParcel& reply)
138 {
139     bool ret = GetPresent();
140     WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
141     return ERR_OK;
142 }
143 
GetTechnologyStub(MessageParcel & reply)144 int32_t BatterySrvStub::GetTechnologyStub(MessageParcel& reply)
145 {
146     std::u16string ret = Str8ToStr16(GetTechnology());
147     WRITE_PARCEL_WITH_RET(reply, String16, ret, E_WRITE_PARCEL_ERROR);
148     return ERR_OK;
149 }
150 
GetBatteryTemperatureStub(MessageParcel & reply)151 int32_t BatterySrvStub::GetBatteryTemperatureStub(MessageParcel& reply)
152 {
153     int32_t ret = GetBatteryTemperature();
154     WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
155     return ERR_OK;
156 }
157 
GetBatteryCapacityLevelStub(MessageParcel & reply)158 int32_t BatterySrvStub::GetBatteryCapacityLevelStub(MessageParcel& reply)
159 {
160     BatteryCapacityLevel ret = GetCapacityLevel();
161     WRITE_PARCEL_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
162     return ERR_OK;
163 }
164 
GetRemainingChargeTimeStub(MessageParcel & reply)165 int64_t BatterySrvStub::GetRemainingChargeTimeStub(MessageParcel& reply)
166 {
167     int64_t ret = GetRemainingChargeTime();
168     WRITE_PARCEL_WITH_RET(reply, Int64, ret, E_WRITE_PARCEL_ERROR);
169     return ERR_OK;
170 }
GetNowCurrentStub(MessageParcel & reply)171 int32_t BatterySrvStub::GetNowCurrentStub(MessageParcel& reply)
172 {
173     int32_t ret = GetNowCurrent();
174     WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
175     return ERR_OK;
176 }
GetRemainEnergyStub(MessageParcel & reply)177 int32_t BatterySrvStub::GetRemainEnergyStub(MessageParcel& reply)
178 {
179     int32_t ret = GetRemainEnergy();
180     WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
181     return ERR_OK;
182 }
GetTotalEnergyStub(MessageParcel & reply)183 int32_t BatterySrvStub::GetTotalEnergyStub(MessageParcel& reply)
184 {
185     int32_t ret = GetTotalEnergy();
186     WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
187     return ERR_OK;
188 }
GetCurrentAverageStub(MessageParcel & reply)189 int32_t BatterySrvStub::GetCurrentAverageStub(MessageParcel& reply)
190 {
191     int32_t ret = GetCurrentAverage();
192     WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
193     return ERR_OK;
194 }
195 } // namespace PowerMgr
196 } // namespace OHOS
197