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_notify.h"
17 #include <regex>
18
19 #ifdef BATTERY_MANAGER_ENABLE_CHARGING_SOUND
20 #include "charging_sound.h"
21 #endif
22 #include "common_event_data.h"
23 #include "common_event_manager.h"
24 #include "common_event_publish_info.h"
25 #include "common_event_support.h"
26 #include "errors.h"
27 #include "hisysevent.h"
28 #include "if_system_ability_manager.h"
29 #include "iservice_registry.h"
30 #include "string_ex.h"
31 #include "system_ability_definition.h"
32
33 #include "battery_config.h"
34 #include "battery_log.h"
35 #include "battery_service.h"
36 #include "power_vibrator.h"
37 #include "power_mgr_client.h"
38 #include <dlfcn.h>
39
40 using namespace OHOS::AAFwk;
41 using namespace OHOS::EventFwk;
42 using namespace OHOS::HiviewDFX;
43
44 namespace OHOS {
45 namespace PowerMgr {
46 bool g_batteryLowOnce = false;
47 bool g_batteryOkOnce = false;
48 bool g_batteryConnectOnce = false;
49 bool g_batteryDisconnectOnce = false;
50 bool g_batteryChargingOnce = false;
51 bool g_batteryDischargingOnce = false;
52 bool g_commonEventInitSuccess = false;
53 OHOS::PowerMgr::BatteryCapacityLevel g_lastCapacityLevel = OHOS::PowerMgr::BatteryCapacityLevel::LEVEL_NONE;
54 const std::string POWER_SUPPLY = "SUBSYSTEM=power_supply";
55 const std::string SHUTDOWN = "shutdown";
56 const std::string REBOOT = "reboot";
57 const std::string SEND_COMMONEVENT = "sendcommonevent";
58 const std::string SEND_CUSTOMEVENT = "sendcustomevent";
59 const std::string BATTERY_CUSTOM_EVENT = "usual.event.battery.custom";
60 const std::string BATTERY_CUSTOM_EVENT_PREFIX = "usual.event.battery";
61 sptr<BatteryService> g_service = DelayedSpSingleton<BatteryService>::GetInstance();
62
BatteryNotify()63 BatteryNotify::BatteryNotify()
64 {
65 const int32_t DEFAULT_LOW_CAPACITY = 20;
66 lowCapacity_ = BatteryConfig::GetInstance().GetInt("soc.low", DEFAULT_LOW_CAPACITY);
67 BATTERY_HILOGI(COMP_SVC, "Low broadcast power=%{public}d", lowCapacity_);
68 }
69
PublishEvents(BatteryInfo & info)70 int32_t BatteryNotify::PublishEvents(BatteryInfo& info)
71 {
72 if (!g_commonEventInitSuccess) {
73 if (!IsCommonEventServiceAbilityExist()) {
74 return ERR_NO_INIT;
75 }
76 }
77 if (info.GetUevent() != POWER_SUPPLY && info.GetUevent() != "") {
78 HandleUevent(info);
79 return ERR_OK;
80 }
81
82 bool isAllSuccess = true;
83 bool ret = PublishChangedEvent(info);
84 isAllSuccess &= ret;
85 ret = PublishChangedEventInner(info);
86 isAllSuccess &= ret;
87 ret = PublishLowEvent(info);
88 isAllSuccess &= ret;
89 ret = PublishOkayEvent(info);
90 isAllSuccess &= ret;
91 ret = PublishPowerConnectedEvent(info);
92 isAllSuccess &= ret;
93 ret = PublishPowerDisconnectedEvent(info);
94 isAllSuccess &= ret;
95 ret = PublishChargingEvent(info);
96 isAllSuccess &= ret;
97 ret = PublishDischargingEvent(info);
98 isAllSuccess &= ret;
99 ret = PublishChargeTypeChangedEvent(info);
100 isAllSuccess &= ret;
101 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
102 lastPowerPluggedType_ = info.GetPluggedType();
103 #endif
104 return isAllSuccess ? ERR_OK : ERR_NO_INIT;
105 }
106
HandleUevent(BatteryInfo & info)107 void BatteryNotify::HandleUevent(BatteryInfo& info)
108 {
109 std::string uevent = info.GetUevent();
110 auto pos = uevent.rfind('$');
111 if (pos != std::string::npos) {
112 std::string ueventName = uevent.substr(0, pos);
113 std::string ueventAct = uevent.substr(++pos);
114 BATTERY_HILOGI(COMP_SVC, "%{public}s decision %{public}s",
115 ueventName.c_str(), ueventAct.c_str());
116 if (ueventAct == SHUTDOWN) {
117 const std::string reason = "POWEROFF_CHARGE_DISABLE";
118 PowerMgrClient::GetInstance().ShutDownDevice(reason);
119 } else if (ueventAct == REBOOT) {
120 PowerMgrClient::GetInstance().RebootDevice(ueventName);
121 } else if (ueventAct == SEND_COMMONEVENT) {
122 info.SetUevent(ueventName);
123 PublishChangedEvent(info);
124 } else if (ueventAct == SEND_CUSTOMEVENT) {
125 info.SetUevent(ueventName);
126 PublishCustomEvent(info, BATTERY_CUSTOM_EVENT);
127 } else if (ueventAct.compare(0, BATTERY_CUSTOM_EVENT_PREFIX.size(), BATTERY_CUSTOM_EVENT_PREFIX) == 0) {
128 info.SetUevent(ueventName);
129 PublishCustomEvent(info, ueventAct);
130 } else {
131 BATTERY_HILOGE(COMP_SVC, "undefine uevent act %{public}s", ueventAct.c_str());
132 }
133 }
134 BATTERY_HILOGI(COMP_SVC, "handle uevent info %{public}s", uevent.c_str());
135 }
136
PublishChargeTypeChangedEvent(const BatteryInfo & info)137 bool BatteryNotify::PublishChargeTypeChangedEvent(const BatteryInfo& info)
138 {
139 ChargeType chargeType = info.GetChargeType();
140 bool isSuccess = true;
141 if (batteryInfoChargeType_ == chargeType) {
142 BATTERY_HILOGD(COMP_SVC, "No need to send chargetype event");
143 return isSuccess;
144 }
145 batteryInfoChargeType_ = chargeType;
146 Want want;
147 want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED);
148 CommonEventData data;
149 data.SetWant(want);
150 CommonEventPublishInfo publishInfo;
151 publishInfo.SetOrdered(false);
152
153 data.SetCode(static_cast<int32_t>(chargeType));
154 BATTERY_HILOGD(COMP_SVC, "publisher chargeType=%{public}d", chargeType);
155 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
156 if (!isSuccess) {
157 BATTERY_HILOGD(COMP_SVC, "failed to publish battery charge type event");
158 }
159
160 return isSuccess;
161 }
162
IsCommonEventServiceAbilityExist() const163 bool BatteryNotify::IsCommonEventServiceAbilityExist() const
164 {
165 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
166 if (!sysMgr) {
167 BATTERY_HILOGE(COMP_SVC,
168 "IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
169 return false;
170 }
171 sptr<IRemoteObject> remote = sysMgr->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
172 if (!remote) {
173 BATTERY_HILOGE(COMP_SVC, "No CesServiceAbility");
174 return false;
175 }
176
177 if (!g_commonEventInitSuccess) {
178 BATTERY_HILOGI(COMP_SVC, "common event service ability init success");
179 g_commonEventInitSuccess = true;
180 }
181
182 return true;
183 }
184
PublishChangedEvent(const BatteryInfo & info)185 bool BatteryNotify::PublishChangedEvent(const BatteryInfo& info)
186 {
187 Want want;
188 int32_t capacity = info.GetCapacity();
189 int32_t pluggedType = static_cast<int32_t>(info.GetPluggedType());
190 int32_t temperature = info.GetTemperature();
191 int32_t healthState = static_cast<int32_t>(info.GetHealthState());
192 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY, capacity);
193 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_VOLTAGE, info.GetVoltage());
194 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TEMPERATURE, temperature);
195 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_HEALTH_STATE, healthState);
196 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE, pluggedType);
197 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE, static_cast<int32_t>(info.GetChargeState()));
198 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PRESENT, info.IsPresent());
199 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TECHNOLOGY, info.GetTechnology());
200 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_UEVENT, info.GetUevent());
201
202 auto capacityLevel = g_service->GetCapacityLevel();
203 if (capacityLevel != g_lastCapacityLevel) {
204 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY_LEVEL, static_cast<int32_t>(capacityLevel));
205 g_lastCapacityLevel = capacityLevel;
206 }
207
208 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
209 CommonEventData data;
210 data.SetWant(want);
211 CommonEventPublishInfo publishInfo;
212 publishInfo.SetOrdered(false);
213 if (capacity != lastCapacity_ || pluggedType != lastPluggedType_ ||
214 temperature != lastTemperature_ || healthState != lastHealthState_) {
215 HiSysEventWrite(HiSysEvent::Domain::BATTERY, "CHANGED", HiSysEvent::EventType::STATISTIC,
216 "LEVEL", capacity, "CHARGER", pluggedType, "VOLTAGE", info.GetVoltage(),
217 "TEMPERATURE", temperature, "HEALTH", healthState, "CURRENT", info.GetNowCurrent());
218 lastCapacity_ = capacity;
219 lastPluggedType_ = pluggedType;
220 lastTemperature_ = temperature;
221 lastHealthState_ = healthState;
222 }
223 bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
224 if (!isSuccess) {
225 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED event");
226 }
227 return isSuccess;
228 }
229
PublishChangedEventInner(const BatteryInfo & info) const230 bool BatteryNotify::PublishChangedEventInner(const BatteryInfo& info) const
231 {
232 Want want;
233 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_CURRENT, info.GetPluggedMaxCurrent());
234 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE, info.GetPluggedMaxVoltage());
235 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT, info.GetNowCurrent());
236 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_COUNTER, info.GetChargeCounter());
237
238 want.SetAction(BatteryInfo::COMMON_EVENT_BATTERY_CHANGED_INNER);
239 CommonEventData data;
240 data.SetWant(want);
241 CommonEventPublishInfo publishInfo;
242 publishInfo.SetOrdered(false);
243 const std::vector<std::string> permissionVec { "ohos.permission.POWER_OPTIMIZATION" };
244 publishInfo.SetSubscriberPermissions(permissionVec);
245
246 bool isSuccess = true;
247 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
248 if (!isSuccess) {
249 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED_INNER event");
250 }
251 return isSuccess;
252 }
253
PublishLowEvent(const BatteryInfo & info) const254 bool BatteryNotify::PublishLowEvent(const BatteryInfo& info) const
255 {
256 bool isSuccess = true;
257
258 if (info.GetCapacity() > lowCapacity_) {
259 g_batteryLowOnce = false;
260 return isSuccess;
261 }
262
263 if (g_batteryLowOnce) {
264 return isSuccess;
265 }
266
267 Want want;
268 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
269 CommonEventData data;
270 data.SetWant(want);
271 CommonEventPublishInfo publishInfo;
272 publishInfo.SetOrdered(false);
273 data.SetCode(info.GetCapacity());
274 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
275 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
276 if (!isSuccess) {
277 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish battery_low event");
278 }
279 g_batteryLowOnce = true;
280 return isSuccess;
281 }
282
PublishOkayEvent(const BatteryInfo & info) const283 bool BatteryNotify::PublishOkayEvent(const BatteryInfo& info) const
284 {
285 bool isSuccess = true;
286
287 if (info.GetCapacity() <= lowCapacity_) {
288 g_batteryOkOnce = false;
289 return isSuccess;
290 }
291
292 if (g_batteryOkOnce) {
293 return isSuccess;
294 }
295
296 Want want;
297 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
298 CommonEventData data;
299 data.SetWant(want);
300 CommonEventPublishInfo publishInfo;
301 publishInfo.SetOrdered(false);
302 data.SetCode(info.GetCapacity());
303 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
304 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
305 if (!isSuccess) {
306 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery_okay event");
307 }
308 g_batteryOkOnce = true;
309 return isSuccess;
310 }
311
PublishPowerConnectedEvent(const BatteryInfo & info) const312 bool BatteryNotify::PublishPowerConnectedEvent(const BatteryInfo& info) const
313 {
314 bool isSuccess = true;
315
316 WirelessPluggedConnected(info);
317 if ((info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_NONE) ||
318 (info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
319 g_batteryConnectOnce = false;
320 return isSuccess;
321 }
322
323 if (g_batteryConnectOnce) {
324 return isSuccess;
325 }
326 StartVibrator();
327 #ifdef BATTERY_MANAGER_ENABLE_CHARGING_SOUND
328 if (g_service && g_service->IsBootCompleted()) {
329 ChargingSound::GetInstance().Start();
330 }
331 #endif
332 Want want;
333 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
334 CommonEventData data;
335 data.SetWant(want);
336 CommonEventPublishInfo publishInfo;
337 publishInfo.SetOrdered(false);
338 data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
339 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
340 static_cast<uint32_t>(info.GetPluggedType()));
341 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
342 if (!isSuccess) {
343 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish power_connected event");
344 }
345
346 g_batteryConnectOnce = true;
347 return isSuccess;
348 }
349
StartVibrator() const350 void BatteryNotify::StartVibrator() const
351 {
352 std::shared_ptr<PowerVibrator> vibrator = PowerVibrator::GetInstance();
353 std::string scene = "start_charge";
354 vibrator->StartVibrator(scene);
355 }
356
PublishPowerDisconnectedEvent(const BatteryInfo & info) const357 bool BatteryNotify::PublishPowerDisconnectedEvent(const BatteryInfo& info) const
358 {
359 bool isSuccess = true;
360
361 WirelessPluggedDisconnected(info);
362 if ((info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_NONE) &&
363 (info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
364 g_batteryDisconnectOnce = false;
365 return isSuccess;
366 }
367
368 if (g_batteryDisconnectOnce) {
369 return isSuccess;
370 }
371 #ifdef BATTERY_MANAGER_ENABLE_CHARGING_SOUND
372 if (g_service && g_service->IsBootCompleted()) {
373 ChargingSound::GetInstance().Stop();
374 }
375 #endif
376 Want want;
377 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
378 CommonEventData data;
379 data.SetWant(want);
380 CommonEventPublishInfo publishInfo;
381 publishInfo.SetOrdered(false);
382 data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
383 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
384 static_cast<uint32_t>(info.GetPluggedType()));
385 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
386 if (!isSuccess) {
387 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish power_disconnected event");
388 }
389
390 g_batteryDisconnectOnce = true;
391 return isSuccess;
392 }
393
WirelessPluggedConnected(const BatteryInfo & info) const394 void BatteryNotify::WirelessPluggedConnected(const BatteryInfo& info) const
395 {
396 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
397 bool wirelessChargerEnable = BatteryConfig::GetInstance().GetWirelessChargerConf();
398 if (wirelessChargerEnable) {
399 if ((lastPowerPluggedType_ != BatteryPluggedType::PLUGGED_TYPE_WIRELESS) &&
400 (info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_WIRELESS)) {
401 RotationMotionSubscriber();
402 }
403 }
404 return;
405 #endif
406 }
407
WirelessPluggedDisconnected(const BatteryInfo & info) const408 void BatteryNotify::WirelessPluggedDisconnected(const BatteryInfo& info) const
409 {
410 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
411 bool wirelessChargerEnable = BatteryConfig::GetInstance().GetWirelessChargerConf();
412 if (wirelessChargerEnable) {
413 if ((lastPowerPluggedType_ == BatteryPluggedType::PLUGGED_TYPE_WIRELESS) &&
414 (info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_WIRELESS)) {
415 RotationMotionUnsubscriber();
416 }
417 }
418 return;
419 #endif
420 }
421
422 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
423 static const char* ROTATION_SUBSCRIBER_CONFIG = "RotationMotionSubscriber";
424 static const char* ROTATION_UNSUBSCRIBER_CONFIG = "RotationMotionUnsubscriber";
425 static const char* POWER_CHARGE_EXTENSION_PATH = "libpower_charge_ext.z.so";
426 typedef void(*FuncRotationSubscriber)();
427 typedef void(*FuncRotationUnsubscriber)();
428 #endif
429
RotationMotionSubscriber() const430 void BatteryNotify::RotationMotionSubscriber() const
431 {
432 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
433 BATTERY_HILOGI(FEATURE_BATT_INFO, "Start to RotationMotionSubscriber");
434 void *subscriberHandler = dlopen(POWER_CHARGE_EXTENSION_PATH, RTLD_LAZY | RTLD_NODELETE);
435 if (subscriberHandler == nullptr) {
436 BATTERY_HILOGE(FEATURE_BATT_INFO, "Dlopen RotationMotionSubscriber failed, reason : %{public}s", dlerror());
437 return;
438 }
439 FuncRotationSubscriber rotationSubscriberFlag =
440 reinterpret_cast<FuncRotationSubscriber>(dlsym(subscriberHandler, ROTATION_SUBSCRIBER_CONFIG));
441 if (rotationSubscriberFlag == nullptr) {
442 BATTERY_HILOGE(FEATURE_BATT_INFO, "RotationMotionSubscriber is null, reason : %{public}s", dlerror());
443 dlclose(subscriberHandler);
444 subscriberHandler = nullptr;
445 return;
446 }
447 rotationSubscriberFlag();
448 BATTERY_HILOGI(FEATURE_BATT_INFO, "RotationMotionSubscriber Success");
449 dlclose(subscriberHandler);
450 subscriberHandler = nullptr;
451 return;
452 #endif
453 }
454
RotationMotionUnsubscriber() const455 void BatteryNotify::RotationMotionUnsubscriber() const
456 {
457 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
458 BATTERY_HILOGI(FEATURE_BATT_INFO, "Start to RotationMotionUnsubscriber");
459 void *unsubscriberHandler = dlopen(POWER_CHARGE_EXTENSION_PATH, RTLD_LAZY | RTLD_NODELETE);
460 if (unsubscriberHandler == nullptr) {
461 BATTERY_HILOGE(FEATURE_BATT_INFO, "Dlopen RotationMotionUnsubscriber failed, reason : %{public}s", dlerror());
462 return;
463 }
464 FuncRotationUnsubscriber rotationUnsubscriberFlag =
465 reinterpret_cast<FuncRotationUnsubscriber>(dlsym(unsubscriberHandler, ROTATION_UNSUBSCRIBER_CONFIG));
466 if (rotationUnsubscriberFlag == nullptr) {
467 BATTERY_HILOGE(FEATURE_BATT_INFO, "RotationMotionUnsubscriber is null, reason : %{public}s", dlerror());
468 dlclose(unsubscriberHandler);
469 unsubscriberHandler = nullptr;
470 return;
471 }
472 rotationUnsubscriberFlag();
473 BATTERY_HILOGI(FEATURE_BATT_INFO, "RotationMotionUnsubscriber Success");
474 dlclose(unsubscriberHandler);
475 unsubscriberHandler = nullptr;
476 return;
477 #endif
478 }
479
PublishChargingEvent(const BatteryInfo & info) const480 bool BatteryNotify::PublishChargingEvent(const BatteryInfo& info) const
481 {
482 bool isSuccess = true;
483
484 if ((info.GetChargeState() != BatteryChargeState::CHARGE_STATE_ENABLE) &&
485 (info.GetChargeState() != BatteryChargeState::CHARGE_STATE_FULL)) {
486 g_batteryChargingOnce = false;
487 return isSuccess;
488 }
489
490 if (g_batteryChargingOnce) {
491 return isSuccess;
492 }
493
494 Want want;
495 want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGING);
496 CommonEventData data;
497 data.SetWant(want);
498 CommonEventPublishInfo publishInfo;
499 publishInfo.SetOrdered(false);
500 data.SetCode(static_cast<int32_t>(info.GetChargeState()));
501 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
502 static_cast<uint32_t>(info.GetChargeState()));
503 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
504 if (!isSuccess) {
505 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
506 }
507
508 g_batteryChargingOnce = true;
509 return isSuccess;
510 }
511
PublishDischargingEvent(const BatteryInfo & info) const512 bool BatteryNotify::PublishDischargingEvent(const BatteryInfo& info) const
513 {
514 bool isSuccess = true;
515
516 if ((info.GetChargeState() == BatteryChargeState::CHARGE_STATE_ENABLE) ||
517 (info.GetChargeState() == BatteryChargeState::CHARGE_STATE_FULL)) {
518 g_batteryDischargingOnce = false;
519 return isSuccess;
520 }
521
522 if (g_batteryDischargingOnce) {
523 return isSuccess;
524 }
525
526 Want want;
527 want.SetAction(CommonEventSupport::COMMON_EVENT_DISCHARGING);
528 CommonEventData data;
529 data.SetWant(want);
530 CommonEventPublishInfo publishInfo;
531 publishInfo.SetOrdered(false);
532 data.SetCode(static_cast<int32_t>(info.GetChargeState()));
533 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
534 static_cast<uint32_t>(info.GetChargeState()));
535 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
536 if (!isSuccess) {
537 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
538 }
539
540 g_batteryDischargingOnce = true;
541 return isSuccess;
542 }
543
PublishCustomEvent(const BatteryInfo & info,const std::string & commonEventName) const544 bool BatteryNotify::PublishCustomEvent(const BatteryInfo& info, const std::string& commonEventName) const
545 {
546 Want want;
547 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_UEVENT, info.GetUevent());
548 want.SetAction(commonEventName);
549 CommonEventData data;
550 data.SetWant(want);
551 CommonEventPublishInfo publishInfo;
552 publishInfo.SetOrdered(false);
553 const std::vector<std::string> permissionVec { "ohos.permission.POWER_OPTIMIZATION" };
554 publishInfo.SetSubscriberPermissions(permissionVec);
555
556 bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
557 if (!isSuccess) {
558 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery custom event");
559 }
560 return isSuccess;
561 }
562 } // namespace PowerMgr
563 } // namespace OHOS
564