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