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_S = 10;
43     int id = HiviewDFX::XCollie::GetInstance().SetTimer("BatteryManagerCallbackStub", DFX_DELAY_S, nullptr, nullptr,
44         HiviewDFX::XCOLLIE_FLAG_LOG);
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         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_CHARGING_STATUS):
57             return GetChargingStatusStub(reply);
58         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_HEALTH_STATUS):
59             return GetHealthStatusStub(reply);
60         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_PLUG_TYPE):
61             return GetPluggedTypeStub(reply);
62         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_VOLTAGE):
63             return GetVoltageStub(reply);
64         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_PRESENT):
65             return GetPresentStub(reply);
66         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_TEMPERATURE):
67             return GetBatteryTemperatureStub(reply);
68         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_LEVEL):
69             return GetBatteryCapacityLevelStub(reply);
70         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_REMAINING_CHARGE_TIME):
71             return GetRemainingChargeTimeStub(reply);
72         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_TECHNOLOGY):
73             return GetTechnologyStub(reply);
74         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_CURRENT_NOW):
75             return GetNowCurrentStub(reply);
76         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_REMAIN_ENERGY):
77             return GetRemainEnergyStub(reply);
78         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_TOTAL_ENERGY):
79             return GetTotalEnergyStub(reply);
80         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_CURRENT_AVERAGE):
81             return GetCurrentAverageStub(reply);
82         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::SET_BATTERY_CONFIG):
83             return SetChargeConfigStub(data, reply);
84         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::GET_BATTERY_CONFIG):
85             return GetChargeConfigStub(data, reply);
86         case static_cast<int>(PowerMgr::BatterySrvInterfaceCode::SUPPORT_BATTERY_CONFIG):
87             return SupportChargeConfigStub(data, reply);
88         default:
89             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
90     }
91 }
92 
GetCapacityStub(MessageParcel & reply)93 int32_t BatterySrvStub::GetCapacityStub(MessageParcel& reply)
94 {
95     int32_t ret = GetCapacity();
96     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
97     return ERR_OK;
98 }
99 
GetChargingStatusStub(MessageParcel & reply)100 int32_t BatterySrvStub::GetChargingStatusStub(MessageParcel& reply)
101 {
102     BatteryChargeState ret = GetChargingStatus();
103     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
104     return ERR_OK;
105 }
106 
GetHealthStatusStub(MessageParcel & reply)107 int32_t BatterySrvStub::GetHealthStatusStub(MessageParcel& reply)
108 {
109     BatteryHealthState ret = GetHealthStatus();
110     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
111     return ERR_OK;
112 }
113 
GetPluggedTypeStub(MessageParcel & reply)114 int32_t BatterySrvStub::GetPluggedTypeStub(MessageParcel& reply)
115 {
116     BatteryPluggedType ret = GetPluggedType();
117     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
118     return ERR_OK;
119 }
120 
GetVoltageStub(MessageParcel & reply)121 int32_t BatterySrvStub::GetVoltageStub(MessageParcel& reply)
122 {
123     int32_t ret = GetVoltage();
124     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
125     return ERR_OK;
126 }
127 
GetPresentStub(MessageParcel & reply)128 int32_t BatterySrvStub::GetPresentStub(MessageParcel& reply)
129 {
130     bool ret = GetPresent();
131     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
132     return ERR_OK;
133 }
134 
GetTechnologyStub(MessageParcel & reply)135 int32_t BatterySrvStub::GetTechnologyStub(MessageParcel& reply)
136 {
137     std::u16string ret = Str8ToStr16(GetTechnology());
138     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, String16, ret, E_WRITE_PARCEL_ERROR);
139     return ERR_OK;
140 }
141 
GetBatteryTemperatureStub(MessageParcel & reply)142 int32_t BatterySrvStub::GetBatteryTemperatureStub(MessageParcel& reply)
143 {
144     int32_t ret = GetBatteryTemperature();
145     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
146     return ERR_OK;
147 }
148 
GetBatteryCapacityLevelStub(MessageParcel & reply)149 int32_t BatterySrvStub::GetBatteryCapacityLevelStub(MessageParcel& reply)
150 {
151     BatteryCapacityLevel ret = GetCapacityLevel();
152     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Uint32, static_cast<uint32_t>(ret), E_WRITE_PARCEL_ERROR);
153     return ERR_OK;
154 }
155 
GetRemainingChargeTimeStub(MessageParcel & reply)156 int64_t BatterySrvStub::GetRemainingChargeTimeStub(MessageParcel& reply)
157 {
158     int64_t ret = GetRemainingChargeTime();
159     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int64, ret, E_WRITE_PARCEL_ERROR);
160     return ERR_OK;
161 }
GetNowCurrentStub(MessageParcel & reply)162 int32_t BatterySrvStub::GetNowCurrentStub(MessageParcel& reply)
163 {
164     int32_t ret = GetNowCurrent();
165     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
166     return ERR_OK;
167 }
GetRemainEnergyStub(MessageParcel & reply)168 int32_t BatterySrvStub::GetRemainEnergyStub(MessageParcel& reply)
169 {
170     int32_t ret = GetRemainEnergy();
171     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
172     return ERR_OK;
173 }
GetTotalEnergyStub(MessageParcel & reply)174 int32_t BatterySrvStub::GetTotalEnergyStub(MessageParcel& reply)
175 {
176     int32_t ret = GetTotalEnergy();
177     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
178     return ERR_OK;
179 }
GetCurrentAverageStub(MessageParcel & reply)180 int32_t BatterySrvStub::GetCurrentAverageStub(MessageParcel& reply)
181 {
182     int32_t ret = GetCurrentAverage();
183     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR);
184     return ERR_OK;
185 }
186 
SetChargeConfigStub(MessageParcel & data,MessageParcel & reply)187 int32_t BatterySrvStub::SetChargeConfigStub(MessageParcel& data, MessageParcel& reply)
188 {
189     std::u16string sceneName;
190     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String16, sceneName, E_READ_PARCEL_ERROR);
191     std::string tempSceneName = Str16ToStr8(sceneName);
192 
193     std::u16string value;
194     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String16, value, E_READ_PARCEL_ERROR);
195     std::string tempValue = Str16ToStr8(value);
196 
197     BatteryError error = SetBatteryConfig(tempSceneName, tempValue);
198     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
199     return ERR_OK;
200 }
201 
GetChargeConfigStub(MessageParcel & data,MessageParcel & reply)202 int32_t BatterySrvStub::GetChargeConfigStub(MessageParcel& data, MessageParcel& reply)
203 {
204     std::u16string sceneName;
205     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String16, sceneName, E_READ_PARCEL_ERROR);
206     std::string tempSceneName = Str16ToStr8(sceneName);
207 
208     std::string result;
209     BatteryError error = GetBatteryConfig(tempSceneName, result);
210     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
211     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, String16, Str8ToStr16(result), E_WRITE_PARCEL_ERROR);
212     return ERR_OK;
213 }
214 
SupportChargeConfigStub(MessageParcel & data,MessageParcel & reply)215 int32_t BatterySrvStub::SupportChargeConfigStub(MessageParcel& data, MessageParcel& reply)
216 {
217     std::u16string sceneName;
218     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String16, sceneName, E_READ_PARCEL_ERROR);
219     std::string tempSceneName = Str16ToStr8(sceneName);
220 
221     bool result = false;
222     BatteryError error = IsBatteryConfigSupported(tempSceneName, result);
223     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
224     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, result, E_WRITE_PARCEL_ERROR);
225     return ERR_OK;
226 }
227 } // namespace PowerMgr
228 } // namespace OHOS
229