1 /*
2  * Copyright (c) 2021-2023 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_client.h"
17 
18 #include "new"
19 #include "refbase.h"
20 #include "errors.h"
21 #include "iremote_broker.h"
22 #include "iservice_registry.h"
23 #include "if_system_ability_manager.h"
24 #include "system_ability_definition.h"
25 #include "battery_info.h"
26 #include "battery_log.h"
27 #include "power_mgr_errors.h"
28 #include "power_common.h"
29 
30 namespace OHOS {
31 namespace PowerMgr {
BatterySrvClient()32 BatterySrvClient::BatterySrvClient() {}
~BatterySrvClient()33 BatterySrvClient::~BatterySrvClient() {}
34 
Connect()35 sptr<IBatterySrv> BatterySrvClient::Connect()
36 {
37     std::lock_guard<std::mutex> lock(mutex_);
38     if (proxy_ != nullptr) {
39         return proxy_;
40     }
41     sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     if (sysMgr == nullptr) {
43         BATTERY_HILOGE(COMP_FWK, "Failed to get Registry");
44         return nullptr;
45     }
46     sptr<IRemoteObject> remoteObject_ = sysMgr->CheckSystemAbility(POWER_MANAGER_BATT_SERVICE_ID);
47     if (remoteObject_ == nullptr) {
48         BATTERY_HILOGE(COMP_FWK, "GetSystemAbility failed");
49         return nullptr;
50     }
51     sptr<IRemoteObject::DeathRecipient> drt = new(std::nothrow) BatterySrvDeathRecipient(*this);
52     if (drt == nullptr) {
53         BATTERY_HILOGE(COMP_FWK, "Failed to create BatterySrvDeathRecipient");
54         return nullptr;
55     }
56     if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(drt))) {
57         BATTERY_HILOGE(COMP_FWK, "Add death recipient to BatterySrv failed");
58         return nullptr;
59     }
60 
61     proxy_ = iface_cast<IBatterySrv>(remoteObject_);
62     deathRecipient_ = drt;
63     BATTERY_HILOGI(COMP_FWK, "Connecting PowerMgrService success, pid=%{public}d", getpid());
64     return proxy_;
65 }
66 
ResetProxy(const wptr<IRemoteObject> & remote)67 void BatterySrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
68 {
69     if (remote == nullptr) {
70         BATTERY_HILOGE(COMP_FWK, "OnRemoteDied failed, remote is nullptr");
71         return;
72     }
73 
74     std::lock_guard<std::mutex> lock(mutex_);
75     RETURN_IF(proxy_ == nullptr);
76     auto serviceRemote = proxy_->AsObject();
77     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
78         serviceRemote->RemoveDeathRecipient(deathRecipient_);
79         proxy_ = nullptr;
80     }
81 }
82 
OnRemoteDied(const wptr<IRemoteObject> & remote)83 void BatterySrvClient::BatterySrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
84 {
85     BATTERY_HILOGW(COMP_FWK, "Recv death notice, BateryService Died");
86     client_.ResetProxy(remote);
87 }
88 
GetCapacity()89 int32_t BatterySrvClient::GetCapacity()
90 {
91     auto proxy = Connect();
92     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_BATT_INT_VALUE);
93     return proxy->GetCapacity();
94 }
95 
GetChargingStatus()96 BatteryChargeState BatterySrvClient::GetChargingStatus()
97 {
98     auto proxy = Connect();
99     RETURN_IF_WITH_RET(proxy == nullptr, BatteryChargeState::CHARGE_STATE_BUTT);
100     return proxy->GetChargingStatus();
101 }
102 
GetHealthStatus()103 BatteryHealthState BatterySrvClient::GetHealthStatus()
104 {
105     auto proxy = Connect();
106     RETURN_IF_WITH_RET(proxy == nullptr, BatteryHealthState::HEALTH_STATE_BUTT);
107     return proxy->GetHealthStatus();
108 }
109 
GetPluggedType()110 BatteryPluggedType BatterySrvClient::GetPluggedType()
111 {
112     auto proxy = Connect();
113     RETURN_IF_WITH_RET(proxy == nullptr, BatteryPluggedType::PLUGGED_TYPE_BUTT);
114     return proxy->GetPluggedType();
115 }
116 
GetVoltage()117 int32_t BatterySrvClient::GetVoltage()
118 {
119     auto proxy = Connect();
120     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_BATT_INT_VALUE);
121     return proxy->GetVoltage();
122 }
123 
GetPresent()124 bool BatterySrvClient::GetPresent()
125 {
126     auto proxy = Connect();
127     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_BATT_BOOL_VALUE);
128     return proxy->GetPresent();
129 }
130 
GetTechnology()131 std::string BatterySrvClient::GetTechnology()
132 {
133     auto proxy = Connect();
134     RETURN_IF_WITH_RET(proxy == nullptr, "");
135     return proxy->GetTechnology();
136 }
137 
GetBatteryTemperature()138 int32_t BatterySrvClient::GetBatteryTemperature()
139 {
140     auto proxy = Connect();
141     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_BATT_TEMP_VALUE);
142     return proxy->GetBatteryTemperature();
143 }
144 
GetNowCurrent()145 int32_t BatterySrvClient::GetNowCurrent()
146 {
147     auto proxy = Connect();
148     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_BATT_INT_VALUE);
149     return proxy->GetNowCurrent();
150 }
151 
GetRemainEnergy()152 int32_t BatterySrvClient::GetRemainEnergy()
153 {
154     auto proxy = Connect();
155     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_BATT_INT_VALUE);
156     return proxy->GetRemainEnergy();
157 }
158 
GetTotalEnergy()159 int32_t BatterySrvClient::GetTotalEnergy()
160 {
161     auto proxy = Connect();
162     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_BATT_INT_VALUE);
163     return proxy->GetTotalEnergy();
164 }
165 
GetCapacityLevel()166 BatteryCapacityLevel BatterySrvClient::GetCapacityLevel()
167 {
168     auto proxy = Connect();
169     RETURN_IF_WITH_RET(proxy == nullptr, BatteryCapacityLevel::LEVEL_NONE);
170     return proxy->GetCapacityLevel();
171 }
172 
GetRemainingChargeTime()173 int64_t BatterySrvClient::GetRemainingChargeTime()
174 {
175     auto proxy = Connect();
176     RETURN_IF_WITH_RET(proxy == nullptr, INVALID_REMAINING_CHARGE_TIME_VALUE);
177     return proxy->GetRemainingChargeTime();
178 }
179 
SetBatteryConfig(const std::string & sceneName,const std::string & value)180 BatteryError BatterySrvClient::SetBatteryConfig(const std::string& sceneName, const std::string& value)
181 {
182     auto proxy = Connect();
183     RETURN_IF_WITH_RET(proxy == nullptr, BatteryError::ERR_CONNECTION_FAIL);
184     return proxy->SetBatteryConfig(sceneName, value);
185 }
186 
GetBatteryConfig(const std::string & sceneName,std::string & result)187 BatteryError BatterySrvClient::GetBatteryConfig(const std::string& sceneName, std::string& result)
188 {
189     auto proxy = Connect();
190     RETURN_IF_WITH_RET(proxy == nullptr, BatteryError::ERR_CONNECTION_FAIL);
191     return proxy->GetBatteryConfig(sceneName, result);
192 }
193 
IsBatteryConfigSupported(const std::string & sceneName,bool & result)194 BatteryError BatterySrvClient::IsBatteryConfigSupported(const std::string& sceneName, bool& result)
195 {
196     auto proxy = Connect();
197     RETURN_IF_WITH_RET(proxy == nullptr, BatteryError::ERR_CONNECTION_FAIL);
198     return proxy->IsBatteryConfigSupported(sceneName, result);
199 }
200 }  // namespace PowerMgr
201 }  // namespace OHOS
202