• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 ErrCode BatterySrvClient::Connect()
36 {
37     std::lock_guard<std::mutex> lock(mutex_);
38     if (proxy_ != nullptr) {
39         return ERR_OK;
40     }
41     sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     if (sysMgr == nullptr) {
43         BATTERY_HILOGW(COMP_FWK, "Failed to get Registry");
44         return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
45     }
46     sptr<IRemoteObject> remoteObject_ = sysMgr->CheckSystemAbility(POWER_MANAGER_BATT_SERVICE_ID);
47     if (remoteObject_ == nullptr) {
48         BATTERY_HILOGW(COMP_FWK, "GetSystemAbility failed");
49         return E_GET_POWER_SERVICE_FAILED;
50     }
51 
52     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new BatterySrvDeathRecipient());
53     if (deathRecipient_ == nullptr) {
54         BATTERY_HILOGW(COMP_FWK, "Failed to create BatterySrvDeathRecipient");
55         return ERR_NO_MEMORY;
56     }
57     if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
58         BATTERY_HILOGW(COMP_FWK, "Add death recipient to BatterySrv failed");
59         return E_ADD_DEATH_RECIPIENT_FAILED;
60     }
61 
62     proxy_ = iface_cast<IBatterySrv>(remoteObject_);
63     return ERR_OK;
64 }
65 
ResetProxy(const wptr<IRemoteObject> & remote)66 void BatterySrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
67 {
68     std::lock_guard<std::mutex> lock(mutex_);
69     RETURN_IF(proxy_ == nullptr);
70     auto serviceRemote = proxy_->AsObject();
71     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
72         serviceRemote->RemoveDeathRecipient(deathRecipient_);
73         proxy_ = nullptr;
74     }
75 }
76 
OnRemoteDied(const wptr<IRemoteObject> & remote)77 void BatterySrvClient::BatterySrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
78 {
79     BATTERY_HILOGW(COMP_FWK, "BateryService Died");
80     if (remote == nullptr) {
81         BATTERY_HILOGE(COMP_FWK, "remote is nullptr");
82         return;
83     }
84     BatterySrvClient::GetInstance().ResetProxy(remote);
85 }
86 
GetCapacity()87 int32_t BatterySrvClient::GetCapacity()
88 {
89     int32_t capacity = INVALID_BATT_INT_VALUE;
90     RETURN_IF_WITH_RET(Connect() != ERR_OK, capacity);
91     capacity = proxy_->GetCapacity();
92     return capacity;
93 }
94 
GetChargingStatus()95 BatteryChargeState BatterySrvClient::GetChargingStatus()
96 {
97     BatteryChargeState chargingState = BatteryChargeState::CHARGE_STATE_BUTT;
98     RETURN_IF_WITH_RET(Connect() != ERR_OK, chargingState);
99     chargingState = proxy_->GetChargingStatus();
100     return chargingState;
101 }
102 
GetHealthStatus()103 BatteryHealthState BatterySrvClient::GetHealthStatus()
104 {
105     BatteryHealthState healthStatus = BatteryHealthState::HEALTH_STATE_BUTT;
106     RETURN_IF_WITH_RET(Connect() != ERR_OK, healthStatus);
107     healthStatus = proxy_->GetHealthStatus();
108     return healthStatus;
109 }
110 
GetPluggedType()111 BatteryPluggedType BatterySrvClient::GetPluggedType()
112 {
113     BatteryPluggedType pluggedType = BatteryPluggedType::PLUGGED_TYPE_BUTT;
114     RETURN_IF_WITH_RET(Connect() != ERR_OK, pluggedType);
115     pluggedType = proxy_->GetPluggedType();
116     return pluggedType;
117 }
118 
GetVoltage()119 int32_t BatterySrvClient::GetVoltage()
120 {
121     int32_t voltage = INVALID_BATT_INT_VALUE;
122     RETURN_IF_WITH_RET(Connect() != ERR_OK, voltage);
123     voltage = proxy_->GetVoltage();
124     return voltage;
125 }
126 
GetPresent()127 bool BatterySrvClient::GetPresent()
128 {
129     bool present = INVALID_BATT_BOOL_VALUE;
130     RETURN_IF_WITH_RET(Connect() != ERR_OK, present);
131     present = proxy_->GetPresent();
132     return present;
133 }
134 
GetTechnology()135 std::string BatterySrvClient::GetTechnology()
136 {
137     std::string technology;
138     RETURN_IF_WITH_RET(Connect() != ERR_OK, technology);
139     technology = proxy_->GetTechnology();
140     return technology;
141 }
142 
GetBatteryTemperature()143 int32_t BatterySrvClient::GetBatteryTemperature()
144 {
145     int32_t temperature = INVALID_BATT_TEMP_VALUE;
146     RETURN_IF_WITH_RET(Connect() != ERR_OK, temperature);
147     temperature = proxy_->GetBatteryTemperature();
148     return temperature;
149 }
150 
GetNowCurrent()151 int32_t BatterySrvClient::GetNowCurrent()
152 {
153     int32_t nowCurrent = INVALID_BATT_INT_VALUE;
154     RETURN_IF_WITH_RET(Connect() != ERR_OK, nowCurrent);
155     nowCurrent = proxy_->GetNowCurrent();
156     return nowCurrent;
157 }
158 
GetRemainEnergy()159 int32_t BatterySrvClient::GetRemainEnergy()
160 {
161     int32_t remainEnergy = INVALID_BATT_INT_VALUE;
162     RETURN_IF_WITH_RET(Connect() != ERR_OK, remainEnergy);
163     remainEnergy = proxy_->GetRemainEnergy();
164     return remainEnergy;
165 }
166 
GetTotalEnergy()167 int32_t BatterySrvClient::GetTotalEnergy()
168 {
169     int32_t totalEnergy = INVALID_BATT_INT_VALUE;
170     RETURN_IF_WITH_RET(Connect() != ERR_OK, totalEnergy);
171     totalEnergy = proxy_->GetTotalEnergy();
172     return totalEnergy;
173 }
174 
GetCapacityLevel()175 BatteryCapacityLevel BatterySrvClient::GetCapacityLevel()
176 {
177     BatteryCapacityLevel level = BatteryCapacityLevel::LEVEL_NONE;
178     RETURN_IF_WITH_RET(Connect() != ERR_OK, level);
179     level = proxy_->GetCapacityLevel();
180     return level;
181 }
182 
GetRemainingChargeTime()183 int64_t BatterySrvClient::GetRemainingChargeTime()
184 {
185     int64_t time = INVALID_REMAINING_CHARGE_TIME_VALUE;
186     RETURN_IF_WITH_RET(Connect() != ERR_OK, time);
187     time = proxy_->GetRemainingChargeTime();
188     return time;
189 }
190 }  // namespace PowerMgr
191 }  // namespace OHOS
192