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
Connect()34 ErrCode BatteryStatsClient::Connect()
35 {
36 std::lock_guard<std::mutex> lock(mutex_);
37 if (proxy_ != nullptr) {
38 return ERR_OK;
39 }
40 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41 if (sam == nullptr) {
42 STATS_HILOGE(COMP_FWK, "Fail to get registry");
43 return E_STATS_GET_SYSTEM_ABILITY_MANAGER_FAILED;
44 }
45 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(POWER_MANAGER_BATT_STATS_SERVICE_ID);
46 if (remoteObject_ == nullptr) {
47 STATS_HILOGE(COMP_FWK, "Get batterystats service failed");
48 return E_STATS_GET_SERVICE_FAILED;
49 }
50 proxy_ = iface_cast<IBatteryStats>(remoteObject_);
51 return ERR_OK;
52 }
53
ResetProxy(const wptr<IRemoteObject> & remote)54 void BatteryStatsClient::ResetProxy(const wptr<IRemoteObject>& remote)
55 {
56 std::lock_guard<std::mutex> lock(mutex_);
57 STATS_RETURN_IF(proxy_ == nullptr);
58 auto serviceRemote = proxy_->AsObject();
59 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
60 serviceRemote->RemoveDeathRecipient(deathRecipient_);
61 proxy_ = nullptr;
62 }
63 }
64
OnRemoteDied(const wptr<IRemoteObject> & remote)65 void BatteryStatsClient::BatteryStatsDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
66 {
67 if (remote == nullptr) {
68 STATS_HILOGE(COMP_FWK, "OnRemoteDied failed, remote is nullptr");
69 return;
70 }
71 BatteryStatsClient::GetInstance().ResetProxy(remote);
72 STATS_HILOGI(COMP_FWK, "Receive death notification");
73 }
74
GetBatteryStats()75 BatteryStatsInfoList BatteryStatsClient::GetBatteryStats()
76 {
77 STATS_HILOGD(COMP_FWK, "Call GetBatteryStats");
78 BatteryStatsInfoList entityList;
79 if (Connect() != ERR_OK) {
80 lastError_ = StatsError::ERR_CONNECTION_FAIL;
81 return entityList;
82 }
83 entityList = proxy_->GetBatteryStats();
84 return entityList;
85 }
86
GetAppStatsMah(const int32_t & uid)87 double BatteryStatsClient::GetAppStatsMah(const int32_t& uid)
88 {
89 STATS_HILOGD(COMP_FWK, "Call GetAppStatsMah");
90 double appStatsMah = StatsUtils::DEFAULT_VALUE;
91 if (Connect() != ERR_OK) {
92 lastError_ = StatsError::ERR_CONNECTION_FAIL;
93 return appStatsMah;
94 }
95 appStatsMah = proxy_->GetAppStatsMah(uid);
96 return appStatsMah;
97 }
98
SetOnBattery(bool isOnBattery)99 void BatteryStatsClient::SetOnBattery(bool isOnBattery)
100 {
101 STATS_HILOGD(COMP_FWK, "Call SetOnBattery");
102 STATS_RETURN_IF(Connect() != ERR_OK);
103 proxy_->SetOnBattery(isOnBattery);
104 }
105
GetAppStatsPercent(const int32_t & uid)106 double BatteryStatsClient::GetAppStatsPercent(const int32_t& uid)
107 {
108 STATS_HILOGD(COMP_FWK, "Call GetAppStatsPercent");
109 double appStatsPercent = StatsUtils::DEFAULT_VALUE;
110 if (Connect() != ERR_OK) {
111 lastError_ = StatsError::ERR_CONNECTION_FAIL;
112 return appStatsPercent;
113 }
114 appStatsPercent = proxy_->GetAppStatsPercent(uid);
115 return appStatsPercent;
116 }
117
GetPartStatsMah(const BatteryStatsInfo::ConsumptionType & type)118 double BatteryStatsClient::GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type)
119 {
120 STATS_HILOGD(COMP_FWK, "Call GetPartStatsMah");
121 double partStatsMah = StatsUtils::DEFAULT_VALUE;
122 if (Connect() != ERR_OK) {
123 lastError_ = StatsError::ERR_CONNECTION_FAIL;
124 return partStatsMah;
125 }
126 partStatsMah = proxy_->GetPartStatsMah(type);
127 return partStatsMah;
128 }
129
GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType & type)130 double BatteryStatsClient::GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type)
131 {
132 STATS_HILOGD(COMP_FWK, "Call GetPartStatsPercent");
133 double partStatsPercent = StatsUtils::DEFAULT_VALUE;
134 if (Connect() != ERR_OK) {
135 lastError_ = StatsError::ERR_CONNECTION_FAIL;
136 return partStatsPercent;
137 }
138 partStatsPercent = proxy_->GetPartStatsPercent(type);
139 return partStatsPercent;
140 }
141
Reset()142 void BatteryStatsClient::Reset()
143 {
144 STATS_HILOGD(COMP_FWK, "Call Reset");
145 STATS_RETURN_IF(Connect() != ERR_OK);
146 proxy_->Reset();
147 }
148
GetTotalTimeSecond(const StatsUtils::StatsType & statsType,const int32_t & uid)149 uint64_t BatteryStatsClient::GetTotalTimeSecond(const StatsUtils::StatsType& statsType, const int32_t& uid)
150 {
151 STATS_HILOGD(COMP_FWK, "Call GetTotalTimeSecond");
152 uint64_t time = StatsUtils::DEFAULT_VALUE;
153 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, time);
154 time = proxy_->GetTotalTimeSecond(statsType, uid);
155 return time;
156 }
157
GetTotalDataBytes(const StatsUtils::StatsType & statsType,const int32_t & uid)158 uint64_t BatteryStatsClient::GetTotalDataBytes(const StatsUtils::StatsType& statsType, const int32_t& uid)
159 {
160 STATS_HILOGD(COMP_FWK, "Call GetTotalDataBytes");
161 uint64_t count = StatsUtils::DEFAULT_VALUE;
162 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, count);
163 count = proxy_->GetTotalDataBytes(statsType, uid);
164 return count;
165 }
166
Dump(const std::vector<std::string> & args)167 std::string BatteryStatsClient::Dump(const std::vector<std::string>& args)
168 {
169 STATS_HILOGD(COMP_FWK, "Call Dump");
170 std::string error = "can't connect service";
171 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, error);
172 return proxy_->ShellDump(args, args.size());
173 }
174
GetLastError()175 StatsError BatteryStatsClient::GetLastError()
176 {
177 if (lastError_ != StatsError::ERR_OK) {
178 StatsError tmpError = lastError_;
179 lastError_ = StatsError::ERR_OK;
180 return tmpError;
181 }
182 STATS_RETURN_IF_WITH_RET(Connect() != ERR_OK, StatsError::ERR_CONNECTION_FAIL);
183 return proxy_->GetLastError();
184 }
185 } // namespace PowerMgr
186 } // namespace OHOS
187