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_stats_client.h"
17
18 #include "errors.h"
19 #include "refbase.h"
20 #include "if_system_ability_manager.h"
21 #include "iremote_broker.h"
22 #include "iservice_registry.h"
23
24 #include "stats_common.h"
25 #include "stats_errors.h"
26 #include "stats_log.h"
27 #include "system_ability_definition.h"
28
29 namespace OHOS {
30 namespace PowerMgr {
BatteryStatsClient()31 BatteryStatsClient::BatteryStatsClient() {}
~BatteryStatsClient()32 BatteryStatsClient::~BatteryStatsClient() {}
33 constexpr int32_t INIT_VALUE = -1;
34 constexpr uint32_t PARAM_MAX_NUM = 10;
35
Connect()36 ErrCode BatteryStatsClient::Connect()
37 {
38 std::lock_guard<std::mutex> lock(mutex_);
39 if (proxy_ != nullptr) {
40 return ERR_OK;
41 }
42 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43 if (sam == nullptr) {
44 STATS_HILOGE(COMP_FWK, "Fail to get registry");
45 return E_STATS_GET_SYSTEM_ABILITY_MANAGER_FAILED;
46 }
47 sptr<IRemoteObject> remoteObject_ = sam->GetSystemAbility(POWER_MANAGER_BATT_STATS_SERVICE_ID);
48 if (remoteObject_ == nullptr) {
49 STATS_HILOGE(COMP_FWK, "Get batterystats service failed");
50 return E_STATS_GET_SERVICE_FAILED;
51 }
52 proxy_ = iface_cast<IBatteryStats>(remoteObject_);
53 return ERR_OK;
54 }
55
ResetProxy(const wptr<IRemoteObject> & remote)56 void BatteryStatsClient::ResetProxy(const wptr<IRemoteObject>& remote)
57 {
58 std::lock_guard<std::mutex> lock(mutex_);
59 STATS_RETURN_IF(proxy_ == nullptr);
60 auto serviceRemote = proxy_->AsObject();
61 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
62 serviceRemote->RemoveDeathRecipient(deathRecipient_);
63 proxy_ = nullptr;
64 }
65 }
66
OnRemoteDied(const wptr<IRemoteObject> & remote)67 void BatteryStatsClient::BatteryStatsDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
68 {
69 if (remote == nullptr) {
70 STATS_HILOGE(COMP_FWK, "OnRemoteDied failed, remote is nullptr");
71 return;
72 }
73 BatteryStatsClient::GetInstance().ResetProxy(remote);
74 STATS_HILOGI(COMP_FWK, "Receive death notification");
75 }
76
GetBatteryStats()77 BatteryStatsInfoList BatteryStatsClient::GetBatteryStats()
78 {
79 STATS_HILOGD(COMP_FWK, "Call GetBatteryStats");
80 BatteryStatsInfoList entityList;
81 if (Connect() != ERR_OK) {
82 lastError_ = StatsError::ERR_CONNECTION_FAIL;
83 return entityList;
84 }
85
86 ParcelableBatteryStatsList parcelableEntityList;
87 int32_t tempError = INIT_VALUE;
88 proxy_->GetBatteryStatsIpc(parcelableEntityList, tempError);
89 tempError_ = static_cast<StatsError>(tempError);
90 return parcelableEntityList.statsList_;
91 }
92
GetAppStatsMah(const int32_t & uid)93 double BatteryStatsClient::GetAppStatsMah(const int32_t& uid)
94 {
95 STATS_HILOGD(COMP_FWK, "Call GetAppStatsMah");
96 double appStatsMah = StatsUtils::DEFAULT_VALUE;
97 if (Connect() != ERR_OK) {
98 lastError_ = StatsError::ERR_CONNECTION_FAIL;
99 return appStatsMah;
100 }
101 int32_t tempError = INIT_VALUE;
102 proxy_->GetAppStatsMahIpc(uid, appStatsMah, tempError);
103 tempError_ = static_cast<StatsError>(tempError);
104 return appStatsMah;
105 }
106
SetOnBattery(bool isOnBattery)107 void BatteryStatsClient::SetOnBattery(bool isOnBattery)
108 {
109 STATS_HILOGD(COMP_FWK, "Call SetOnBattery");
110 STATS_RETURN_IF(Connect() != ERR_OK);
111 proxy_->SetOnBatteryIpc(isOnBattery);
112 }
113
GetAppStatsPercent(const int32_t & uid)114 double BatteryStatsClient::GetAppStatsPercent(const int32_t& uid)
115 {
116 STATS_HILOGD(COMP_FWK, "Call GetAppStatsPercent");
117 double appStatsPercent = StatsUtils::DEFAULT_VALUE;
118 if (Connect() != ERR_OK) {
119 lastError_ = StatsError::ERR_CONNECTION_FAIL;
120 return appStatsPercent;
121 }
122 int32_t tempError = INIT_VALUE;
123 proxy_->GetAppStatsPercentIpc(uid, appStatsPercent, tempError);
124 tempError_ = static_cast<StatsError>(tempError);
125 return appStatsPercent;
126 }
127
GetPartStatsMah(const BatteryStatsInfo::ConsumptionType & type)128 double BatteryStatsClient::GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type)
129 {
130 STATS_HILOGD(COMP_FWK, "Call GetPartStatsMah");
131 double partStatsMah = StatsUtils::DEFAULT_VALUE;
132 if (Connect() != ERR_OK) {
133 lastError_ = StatsError::ERR_CONNECTION_FAIL;
134 return partStatsMah;
135 }
136 int32_t tempError = INIT_VALUE;
137 proxy_->GetPartStatsMahIpc(static_cast<int32_t>(type), partStatsMah, tempError);
138 tempError_ = static_cast<StatsError>(tempError);
139 return partStatsMah;
140 }
141
GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType & type)142 double BatteryStatsClient::GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type)
143 {
144 STATS_HILOGD(COMP_FWK, "Call GetPartStatsPercent");
145 double partStatsPercent = StatsUtils::DEFAULT_VALUE;
146 if (Connect() != ERR_OK) {
147 lastError_ = StatsError::ERR_CONNECTION_FAIL;
148 return partStatsPercent;
149 }
150 int32_t tempError = INIT_VALUE;
151 proxy_->GetPartStatsPercentIpc(static_cast<int32_t>(type), partStatsPercent, tempError);
152 tempError_ = static_cast<StatsError>(tempError);
153 return partStatsPercent;
154 }
155
Reset()156 void BatteryStatsClient::Reset()
157 {
158 STATS_HILOGD(COMP_FWK, "Call Reset");
159 STATS_RETURN_IF(Connect() != ERR_OK);
160 proxy_->ResetIpc();
161 }
162
GetTotalTimeSecond(const StatsUtils::StatsType & statsType,const int32_t & uid)163 uint64_t BatteryStatsClient::GetTotalTimeSecond(const StatsUtils::StatsType& statsType, const int32_t& uid)
164 {
165 STATS_HILOGD(COMP_FWK, "Call GetTotalTimeSecond");
166 uint64_t time = StatsUtils::DEFAULT_VALUE;
167 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, time);
168 proxy_->GetTotalTimeSecondIpc(static_cast<int32_t>(statsType), uid, time);
169 return time;
170 }
171
GetTotalDataBytes(const StatsUtils::StatsType & statsType,const int32_t & uid)172 uint64_t BatteryStatsClient::GetTotalDataBytes(const StatsUtils::StatsType& statsType, const int32_t& uid)
173 {
174 STATS_HILOGD(COMP_FWK, "Call GetTotalDataBytes");
175 uint64_t count = StatsUtils::DEFAULT_VALUE;
176 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, count);
177 proxy_->GetTotalDataBytesIpc(static_cast<int32_t>(statsType), uid, count);
178 return count;
179 }
180
Dump(const std::vector<std::string> & args)181 std::string BatteryStatsClient::Dump(const std::vector<std::string>& args)
182 {
183 STATS_HILOGD(COMP_FWK, "Call Dump");
184 std::string error = "can't connect service";
185 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, error);
186 std::string dumpshell = "remote error";
187 uint32_t argc = args.size();
188 if (argc >= PARAM_MAX_NUM) {
189 STATS_HILOGE(COMP_FWK, "params exceed limit, argc=%{public}u", argc);
190 return dumpshell;
191 }
192 proxy_->ShellDumpIpc(args, argc, dumpshell);
193 return dumpshell;
194 }
195
GetLastError()196 StatsError BatteryStatsClient::GetLastError()
197 {
198 if (lastError_ != StatsError::ERR_OK) {
199 StatsError tmpError = lastError_;
200 lastError_ = StatsError::ERR_OK;
201 return tmpError;
202 }
203 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, StatsError::ERR_CONNECTION_FAIL);
204 return tempError_;
205 }
206 } // namespace PowerMgr
207 } // namespace OHOS
208