1 /*
2 * Copyright (c) 2021 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_service_subscriber.h"
17
18 #include <typeinfo>
19 #include "ohos/aafwk/content/want.h"
20 #include "battery_log.h"
21 #include "string_ex.h"
22 #include "batteryd_api.h"
23 #include "iservice_registry.h"
24 #include "if_system_ability_manager.h"
25 #include "system_ability_definition.h"
26
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::EventFwk;
29
30 namespace OHOS {
31 namespace PowerMgr {
32 bool g_firstPublish = true;
33 bool g_batteryLowOnce = false;
34 bool g_batteryOkOnce = false;
35 bool g_batteryConnectOnce = false;
36 bool g_batteryDisconnectOnce = false;
37 bool g_batteryChargingOnce = false;
38 bool g_batteryDischargingOnce = false;
39 bool g_commonEventInitSuccess = false;
40 BatterydInfo g_batteryInfo;
41 const int BATTERY_LOW_CAPACITY = 20;
42
Update(const BatteryInfo & info)43 int32_t BatteryServiceSubscriber::Update(const BatteryInfo& info)
44 {
45 if (g_commonEventInitSuccess) {
46 BATTERY_HILOGE(COMP_SVC, "common event service ability init success");
47 } else {
48 if (!IsCommonEventServiceAbilityExist()) {
49 return ERR_NO_INIT;
50 }
51 }
52
53 bool isAllSuccess = true;
54 bool ret = HandleBatteryChangedEvent(info);
55 isAllSuccess &= ret;
56 ret = HandleBatteryLowEvent(info);
57 isAllSuccess &= ret;
58 ret = HandleBatteryOkayEvent(info);
59 isAllSuccess &= ret;
60 ret = HandleBatteryPowerConnectedEvent(info);
61 isAllSuccess &= ret;
62 ret = HandleBatteryPowerDisconnectedEvent(info);
63 isAllSuccess &= ret;
64 ret = HandleBatteryChargingEvent(info);
65 isAllSuccess &= ret;
66 ret = HandleBatteryDischargingEvent(info);
67 isAllSuccess &= ret;
68 g_firstPublish = false;
69
70 return isAllSuccess ? ERR_OK : ERR_NO_INIT;
71 }
72
IsCommonEventServiceAbilityExist()73 bool BatteryServiceSubscriber::IsCommonEventServiceAbilityExist()
74 {
75 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76 if (!sysMgr) {
77 BATTERY_HILOGE(COMP_SVC,
78 "IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
79 return false;
80 }
81 sptr<IRemoteObject> remote = sysMgr->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
82 if (!remote) {
83 BATTERY_HILOGE(COMP_SVC, "No CesServiceAbility");
84 return false;
85 }
86
87 g_commonEventInitSuccess = true;
88 return true;
89 }
90
HandleBatteryChangedEvent(const BatteryInfo & info)91 bool BatteryServiceSubscriber::HandleBatteryChangedEvent(const BatteryInfo& info)
92 {
93 Want want;
94 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_CAPACITY), info.GetCapacity());
95 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_VOLTAGE), info.GetVoltage());
96 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_TEMPERATURE), info.GetTemperature());
97 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_HEALTH_STATE), static_cast<int>(info.GetHealthState()));
98 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_TYPE), static_cast<int>(info.GetPluggedType()));
99 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_CURRENT), info.GetPluggedMaxCurrent());
100 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_VOLTAGE), info.GetPluggedMaxVoltage());
101 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_CHARGE_STATE), static_cast<int>(info.GetChargeState()));
102 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_CHARGE_COUNTER), info.GetChargeCounter());
103 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PRESENT), info.IsPresent());
104 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_TECHNOLOGY), info.GetTechnology());
105
106 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
107 CommonEventData data;
108 data.SetWant(want);
109 CommonEventPublishInfo publishInfo;
110 publishInfo.SetOrdered(false);
111 bool isSuccess = true;
112
113 if ((g_firstPublish == true) || (CmpBatteryInfo(info) == false)) {
114 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
115 SwaptBatteryInfo(info);
116 }
117
118 if (!isSuccess) {
119 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED event");
120 }
121 return isSuccess;
122 }
123
CmpBatteryInfo(const BatteryInfo & info)124 bool BatteryServiceSubscriber::CmpBatteryInfo(const BatteryInfo& info)
125 {
126 return ((g_batteryInfo.capacity_ == info.GetCapacity()) &&
127 (g_batteryInfo.voltage_ == info.GetVoltage()) &&
128 (g_batteryInfo.temperature_ == info.GetTemperature()) &&
129 (g_batteryInfo.healthState_ == static_cast<int32_t>(info.GetHealthState())) &&
130 (g_batteryInfo.pluggedType_ == static_cast<int32_t>(info.GetPluggedType())) &&
131 (g_batteryInfo.pluggedMaxCurrent_ == info.GetPluggedMaxCurrent()) &&
132 (g_batteryInfo.pluggedMaxVoltage_ == info.GetPluggedMaxVoltage()) &&
133 (g_batteryInfo.chargeState_ == static_cast<int32_t>(info.GetChargeState())) &&
134 (g_batteryInfo.chargeCounter_ == info.GetChargeCounter()) &&
135 (g_batteryInfo.present_ == info.IsPresent()));
136 }
137
SwaptBatteryInfo(const BatteryInfo & info)138 void BatteryServiceSubscriber::SwaptBatteryInfo(const BatteryInfo& info)
139 {
140 g_batteryInfo.capacity_ = info.GetCapacity();
141 g_batteryInfo.voltage_ = info.GetVoltage();
142 g_batteryInfo.temperature_ = info.GetTemperature();
143 g_batteryInfo.healthState_ = static_cast<uint32_t>(info.GetHealthState());
144 g_batteryInfo.pluggedType_ = static_cast<uint32_t>(info.GetPluggedType());
145 g_batteryInfo.pluggedMaxCurrent_ = info.GetPluggedMaxCurrent();
146 g_batteryInfo.pluggedMaxVoltage_ = info.GetPluggedMaxVoltage();
147 g_batteryInfo.chargeState_ = static_cast<int32_t>(info.GetChargeState());
148 g_batteryInfo.chargeCounter_ = info.GetChargeCounter();
149 g_batteryInfo.present_ = info.IsPresent();
150 }
151
HandleBatteryLowEvent(const BatteryInfo & info)152 bool BatteryServiceSubscriber::HandleBatteryLowEvent(const BatteryInfo& info)
153 {
154 Want want;
155 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
156 CommonEventData data;
157 data.SetWant(want);
158 CommonEventPublishInfo publishInfo;
159 publishInfo.SetOrdered(false);
160 bool isSuccess = true;
161
162 if (info.GetCapacity() > BATTERY_LOW_CAPACITY) {
163 g_batteryLowOnce = false;
164 return isSuccess;
165 }
166
167 if (g_batteryLowOnce) {
168 return isSuccess;
169 }
170
171 data.SetCode(BatteryInfo::COMMON_EVENT_CODE_CAPACITY);
172 data.SetData(ToString(info.GetCapacity()));
173 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
174 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
175 if (!isSuccess) {
176 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish battery_low event");
177 }
178 g_batteryLowOnce = true;
179 return isSuccess;
180 }
181
HandleBatteryOkayEvent(const BatteryInfo & info)182 bool BatteryServiceSubscriber::HandleBatteryOkayEvent(const BatteryInfo& info)
183 {
184 Want want;
185 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
186 CommonEventData data;
187 data.SetWant(want);
188 CommonEventPublishInfo publishInfo;
189 publishInfo.SetOrdered(false);
190 bool isSuccess = true;
191
192 if (info.GetCapacity() <= BATTERY_LOW_CAPACITY) {
193 g_batteryOkOnce = false;
194 return isSuccess;
195 }
196
197 if (g_batteryOkOnce) {
198 return isSuccess;
199 }
200
201 data.SetCode(BatteryInfo::COMMON_EVENT_CODE_CAPACITY);
202 data.SetData(ToString(info.GetCapacity()));
203 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
204 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
205 if (!isSuccess) {
206 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery_okay event");
207 }
208 g_batteryOkOnce = true;
209 return isSuccess;
210 }
211
HandleBatteryPowerConnectedEvent(const BatteryInfo & info)212 bool BatteryServiceSubscriber::HandleBatteryPowerConnectedEvent(const BatteryInfo& info)
213 {
214 Want want;
215 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
216 CommonEventData data;
217 data.SetWant(want);
218 CommonEventPublishInfo publishInfo;
219 publishInfo.SetOrdered(false);
220 bool isSuccess = true;
221
222 if ((static_cast<uint32_t>(info.GetPluggedType()) == PLUGGED_TYPE_NONE) ||
223 (static_cast<uint32_t>(info.GetPluggedType()) == PLUGGED_TYPE_BUTT)) {
224 g_batteryConnectOnce = false;
225 return isSuccess;
226 }
227
228 if (g_batteryConnectOnce) {
229 return isSuccess;
230 }
231
232 data.SetCode(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_TYPE);
233 data.SetData(ToString(static_cast<uint32_t>(info.GetPluggedType())));
234 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}d",
235 static_cast<uint32_t>(info.GetPluggedType()));
236 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
237 if (!isSuccess) {
238 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish power_connected event");
239 }
240
241 g_batteryConnectOnce = true;
242 return isSuccess;
243 }
244
HandleBatteryPowerDisconnectedEvent(const BatteryInfo & info)245 bool BatteryServiceSubscriber::HandleBatteryPowerDisconnectedEvent(const BatteryInfo& info)
246 {
247 Want want;
248 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
249 CommonEventData data;
250 data.SetWant(want);
251 CommonEventPublishInfo publishInfo;
252 publishInfo.SetOrdered(false);
253 bool isSuccess = true;
254
255 if ((static_cast<uint32_t>(info.GetPluggedType()) != PLUGGED_TYPE_NONE) &&
256 (static_cast<uint32_t>(info.GetPluggedType()) != PLUGGED_TYPE_BUTT)) {
257 g_batteryDisconnectOnce = false;
258 return isSuccess;
259 }
260
261 if (g_batteryDisconnectOnce) {
262 return isSuccess;
263 }
264
265 data.SetCode(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_TYPE);
266 data.SetData(ToString(static_cast<uint32_t>(info.GetPluggedType())));
267 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}d",
268 static_cast<uint32_t>(info.GetPluggedType()));
269 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
270 if (!isSuccess) {
271 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish power_disconnected event");
272 }
273
274 g_batteryDisconnectOnce = true;
275 return isSuccess;
276 }
277
HandleBatteryChargingEvent(const BatteryInfo & info)278 bool BatteryServiceSubscriber::HandleBatteryChargingEvent(const BatteryInfo& info)
279 {
280 Want want;
281 want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGING);
282 CommonEventData data;
283 data.SetWant(want);
284 CommonEventPublishInfo publishInfo;
285 publishInfo.SetOrdered(false);
286 bool isSuccess = true;
287
288 if (static_cast<uint32_t>(info.GetChargeState()) !=
289 static_cast<uint32_t>(BatteryChargeState::CHARGE_STATE_ENABLE)) {
290 g_batteryChargingOnce = false;
291 return isSuccess;
292 }
293
294 if (g_batteryChargingOnce) {
295 return isSuccess;
296 }
297
298 data.SetCode(BatteryInfo::COMMON_EVENT_CODE_CHARGE_STATE);
299 data.SetData(ToString(static_cast<uint32_t>(info.GetChargeState())));
300 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}d",
301 static_cast<uint32_t>(info.GetChargeState()));
302 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
303 if (!isSuccess) {
304 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
305 }
306
307 g_batteryChargingOnce = true;
308 return isSuccess;
309 }
310
HandleBatteryDischargingEvent(const BatteryInfo & info)311 bool BatteryServiceSubscriber::HandleBatteryDischargingEvent(const BatteryInfo& info)
312 {
313 Want want;
314 want.SetAction(CommonEventSupport::COMMON_EVENT_DISCHARGING);
315 CommonEventData data;
316 data.SetWant(want);
317 CommonEventPublishInfo publishInfo;
318 publishInfo.SetOrdered(false);
319 bool isSuccess = true;
320
321 if (static_cast<uint32_t>(info.GetChargeState()) != static_cast<uint32_t>(BatteryChargeState::CHARGE_STATE_NONE)) {
322 g_batteryDischargingOnce = false;
323 return isSuccess;
324 }
325
326 if (g_batteryDischargingOnce) {
327 return isSuccess;
328 }
329
330 data.SetCode(BatteryInfo::COMMON_EVENT_CODE_CHARGE_STATE);
331 data.SetData(ToString(static_cast<uint32_t>(info.GetChargeState())));
332 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}d",
333 static_cast<uint32_t>(info.GetChargeState()));
334 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
335 if (!isSuccess) {
336 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
337 }
338
339 g_batteryDischargingOnce = true;
340 return isSuccess;
341 }
342 } // namespace PowerMgr
343 } // namespace OHOS
344