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 SEND_POPUP = "sendpopup";
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.compare(0, BATTERY_CUSTOM_EVENT_PREFIX.size(), BATTERY_CUSTOM_EVENT_PREFIX) == 0) {
125 info.SetUevent(ueventName);
126 PublishCustomEvent(info, ueventAct);
127 } else if (ueventAct == SEND_POPUP) {
128 info.SetUevent(ueventName);
129 PublishChangedEvent(info);
130 HandleNotification(ueventName);
131 } else {
132 BATTERY_HILOGE(COMP_SVC, "undefine uevent act %{public}s", ueventAct.c_str());
133 }
134 }
135 BATTERY_HILOGI(COMP_SVC, "handle uevent info %{public}s", uevent.c_str());
136 }
137
PublishChargeTypeChangedEvent(const BatteryInfo & info)138 bool BatteryNotify::PublishChargeTypeChangedEvent(const BatteryInfo& info)
139 {
140 ChargeType chargeType = info.GetChargeType();
141 bool isSuccess = true;
142 if (batteryInfoChargeType_ == chargeType) {
143 BATTERY_HILOGD(COMP_SVC, "No need to send chargetype event");
144 return isSuccess;
145 }
146 batteryInfoChargeType_ = chargeType;
147 Want want;
148 want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED);
149 CommonEventData data;
150 data.SetWant(want);
151 CommonEventPublishInfo publishInfo;
152 publishInfo.SetOrdered(false);
153
154 data.SetCode(static_cast<int32_t>(chargeType));
155 BATTERY_HILOGD(COMP_SVC, "publisher chargeType=%{public}d", chargeType);
156 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
157 if (!isSuccess) {
158 BATTERY_HILOGD(COMP_SVC, "failed to publish battery charge type event");
159 }
160
161 return isSuccess;
162 }
163
IsCommonEventServiceAbilityExist() const164 bool BatteryNotify::IsCommonEventServiceAbilityExist() const
165 {
166 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
167 if (!sysMgr) {
168 BATTERY_HILOGE(COMP_SVC,
169 "IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
170 return false;
171 }
172 sptr<IRemoteObject> remote = sysMgr->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
173 if (!remote) {
174 BATTERY_HILOGE(COMP_SVC, "No CesServiceAbility");
175 return false;
176 }
177
178 if (!g_commonEventInitSuccess) {
179 BATTERY_HILOGI(COMP_SVC, "common event service ability init success");
180 g_commonEventInitSuccess = true;
181 }
182
183 return true;
184 }
185
PublishChangedEvent(const BatteryInfo & info)186 bool BatteryNotify::PublishChangedEvent(const BatteryInfo& info)
187 {
188 Want want;
189 int32_t capacity = info.GetCapacity();
190 int32_t pluggedType = static_cast<int32_t>(info.GetPluggedType());
191 int32_t temperature = info.GetTemperature();
192 int32_t healthState = static_cast<int32_t>(info.GetHealthState());
193 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY, capacity);
194 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_VOLTAGE, info.GetVoltage());
195 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TEMPERATURE, temperature);
196 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_HEALTH_STATE, healthState);
197 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE, pluggedType);
198 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE, static_cast<int32_t>(info.GetChargeState()));
199 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PRESENT, info.IsPresent());
200 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TECHNOLOGY, info.GetTechnology());
201 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_UEVENT, info.GetUevent());
202
203 auto capacityLevel = g_service->GetCapacityLevel();
204 if (capacityLevel != g_lastCapacityLevel) {
205 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY_LEVEL, static_cast<int32_t>(capacityLevel));
206 g_lastCapacityLevel = capacityLevel;
207 }
208
209 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
210 CommonEventData data;
211 data.SetWant(want);
212 CommonEventPublishInfo publishInfo;
213 publishInfo.SetOrdered(false);
214 if (capacity != lastCapacity_ || pluggedType != lastPluggedType_ ||
215 temperature != lastTemperature_ || healthState != lastHealthState_) {
216 HiSysEventWrite(HiSysEvent::Domain::BATTERY, "CHANGED", HiSysEvent::EventType::STATISTIC,
217 "LEVEL", capacity, "CHARGER", pluggedType, "VOLTAGE", info.GetVoltage(),
218 "TEMPERATURE", temperature, "HEALTH", healthState, "CURRENT", info.GetNowCurrent(),
219 "CHARGE_MODE", static_cast<int32_t>(info.GetChargeType()));
220 lastCapacity_ = capacity;
221 lastPluggedType_ = pluggedType;
222 lastTemperature_ = temperature;
223 lastHealthState_ = healthState;
224 }
225 bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
226 if (!isSuccess) {
227 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED event");
228 }
229 return isSuccess;
230 }
231
PublishChangedEventInner(const BatteryInfo & info) const232 bool BatteryNotify::PublishChangedEventInner(const BatteryInfo& info) const
233 {
234 Want want;
235 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_CURRENT, info.GetPluggedMaxCurrent());
236 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE, info.GetPluggedMaxVoltage());
237 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT, info.GetNowCurrent());
238 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_COUNTER, info.GetChargeCounter());
239
240 want.SetAction(BatteryInfo::COMMON_EVENT_BATTERY_CHANGED_INNER);
241 CommonEventData data;
242 data.SetWant(want);
243 CommonEventPublishInfo publishInfo;
244 publishInfo.SetOrdered(false);
245 const std::vector<std::string> permissionVec { "ohos.permission.POWER_OPTIMIZATION" };
246 publishInfo.SetSubscriberPermissions(permissionVec);
247
248 bool isSuccess = true;
249 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
250 if (!isSuccess) {
251 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED_INNER event");
252 }
253 return isSuccess;
254 }
255
PublishLowEvent(const BatteryInfo & info) const256 bool BatteryNotify::PublishLowEvent(const BatteryInfo& info) const
257 {
258 bool isSuccess = true;
259
260 if (info.GetCapacity() > lowCapacity_) {
261 g_batteryLowOnce = false;
262 return isSuccess;
263 }
264
265 if (g_batteryLowOnce) {
266 return isSuccess;
267 }
268
269 Want want;
270 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
271 CommonEventData data;
272 data.SetWant(want);
273 CommonEventPublishInfo publishInfo;
274 publishInfo.SetOrdered(false);
275 data.SetCode(info.GetCapacity());
276 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
277 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
278 if (!isSuccess) {
279 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish battery_low event");
280 }
281 g_batteryLowOnce = true;
282 return isSuccess;
283 }
284
PublishOkayEvent(const BatteryInfo & info) const285 bool BatteryNotify::PublishOkayEvent(const BatteryInfo& info) const
286 {
287 bool isSuccess = true;
288
289 if (info.GetCapacity() <= lowCapacity_) {
290 g_batteryOkOnce = false;
291 return isSuccess;
292 }
293
294 if (g_batteryOkOnce) {
295 return isSuccess;
296 }
297
298 Want want;
299 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
300 CommonEventData data;
301 data.SetWant(want);
302 CommonEventPublishInfo publishInfo;
303 publishInfo.SetOrdered(false);
304 data.SetCode(info.GetCapacity());
305 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
306 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
307 if (!isSuccess) {
308 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery_okay event");
309 }
310 g_batteryOkOnce = true;
311 return isSuccess;
312 }
313
PublishPowerConnectedEvent(const BatteryInfo & info) const314 bool BatteryNotify::PublishPowerConnectedEvent(const BatteryInfo& info) const
315 {
316 bool isSuccess = true;
317
318 WirelessPluggedConnected(info);
319 if ((info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_NONE) ||
320 (info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
321 g_batteryConnectOnce = false;
322 return isSuccess;
323 }
324
325 if (g_batteryConnectOnce) {
326 return isSuccess;
327 }
328 StartVibrator();
329 #ifdef BATTERY_MANAGER_ENABLE_CHARGING_SOUND
330 if (g_service && g_service->IsBootCompleted()) {
331 ChargingSound::GetInstance().Start();
332 }
333 #endif
334 Want want;
335 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
336 CommonEventData data;
337 data.SetWant(want);
338 CommonEventPublishInfo publishInfo;
339 publishInfo.SetOrdered(false);
340 data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
341 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
342 static_cast<uint32_t>(info.GetPluggedType()));
343 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
344 if (!isSuccess) {
345 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish power_connected event");
346 }
347
348 g_batteryConnectOnce = true;
349 return isSuccess;
350 }
351
StartVibrator() const352 void BatteryNotify::StartVibrator() const
353 {
354 std::shared_ptr<PowerVibrator> vibrator = PowerVibrator::GetInstance();
355 std::string scene = "start_charge";
356 vibrator->StartVibrator(scene);
357 }
358
PublishPowerDisconnectedEvent(const BatteryInfo & info) const359 bool BatteryNotify::PublishPowerDisconnectedEvent(const BatteryInfo& info) const
360 {
361 bool isSuccess = true;
362
363 WirelessPluggedDisconnected(info);
364 if ((info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_NONE) &&
365 (info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
366 g_batteryDisconnectOnce = false;
367 return isSuccess;
368 }
369
370 if (g_batteryDisconnectOnce) {
371 return isSuccess;
372 }
373 #ifdef BATTERY_MANAGER_ENABLE_CHARGING_SOUND
374 if (g_service && g_service->IsBootCompleted()) {
375 ChargingSound::GetInstance().Stop();
376 }
377 #endif
378 Want want;
379 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
380 CommonEventData data;
381 data.SetWant(want);
382 CommonEventPublishInfo publishInfo;
383 publishInfo.SetOrdered(false);
384 data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
385 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
386 static_cast<uint32_t>(info.GetPluggedType()));
387 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
388 if (!isSuccess) {
389 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish power_disconnected event");
390 }
391
392 g_batteryDisconnectOnce = true;
393 return isSuccess;
394 }
395
WirelessPluggedConnected(const BatteryInfo & info) const396 void BatteryNotify::WirelessPluggedConnected(const BatteryInfo& info) const
397 {
398 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
399 bool wirelessChargerEnable = BatteryConfig::GetInstance().GetWirelessChargerConf();
400 if (wirelessChargerEnable) {
401 if ((lastPowerPluggedType_ != BatteryPluggedType::PLUGGED_TYPE_WIRELESS) &&
402 (info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_WIRELESS)) {
403 RotationMotionSubscriber();
404 }
405 }
406 return;
407 #endif
408 }
409
WirelessPluggedDisconnected(const BatteryInfo & info) const410 void BatteryNotify::WirelessPluggedDisconnected(const BatteryInfo& info) const
411 {
412 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
413 bool wirelessChargerEnable = BatteryConfig::GetInstance().GetWirelessChargerConf();
414 if (wirelessChargerEnable) {
415 if ((lastPowerPluggedType_ == BatteryPluggedType::PLUGGED_TYPE_WIRELESS) &&
416 (info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_WIRELESS)) {
417 RotationMotionUnsubscriber();
418 }
419 }
420 return;
421 #endif
422 }
423
424 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
425 static const char* ROTATION_SUBSCRIBER_CONFIG = "RotationMotionSubscriber";
426 static const char* ROTATION_UNSUBSCRIBER_CONFIG = "RotationMotionUnsubscriber";
427 static const char* POWER_CHARGE_EXTENSION_PATH = "libpower_charge_ext.z.so";
428 typedef void(*FuncRotationSubscriber)();
429 typedef void(*FuncRotationUnsubscriber)();
430 #endif
431
RotationMotionSubscriber() const432 void BatteryNotify::RotationMotionSubscriber() const
433 {
434 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
435 BATTERY_HILOGI(FEATURE_BATT_INFO, "Start to RotationMotionSubscriber");
436 void *subscriberHandler = dlopen(POWER_CHARGE_EXTENSION_PATH, RTLD_LAZY | RTLD_NODELETE);
437 if (subscriberHandler == nullptr) {
438 BATTERY_HILOGE(FEATURE_BATT_INFO, "Dlopen RotationMotionSubscriber failed, reason : %{public}s", dlerror());
439 return;
440 }
441 FuncRotationSubscriber rotationSubscriberFlag =
442 reinterpret_cast<FuncRotationSubscriber>(dlsym(subscriberHandler, ROTATION_SUBSCRIBER_CONFIG));
443 if (rotationSubscriberFlag == nullptr) {
444 BATTERY_HILOGE(FEATURE_BATT_INFO, "RotationMotionSubscriber is null, reason : %{public}s", dlerror());
445 dlclose(subscriberHandler);
446 subscriberHandler = nullptr;
447 return;
448 }
449 rotationSubscriberFlag();
450 BATTERY_HILOGI(FEATURE_BATT_INFO, "RotationMotionSubscriber Success");
451 dlclose(subscriberHandler);
452 subscriberHandler = nullptr;
453 return;
454 #endif
455 }
456
RotationMotionUnsubscriber() const457 void BatteryNotify::RotationMotionUnsubscriber() const
458 {
459 #ifdef BATTERY_MANAGER_ENABLE_WIRELESS_CHARGE
460 BATTERY_HILOGI(FEATURE_BATT_INFO, "Start to RotationMotionUnsubscriber");
461 void *unsubscriberHandler = dlopen(POWER_CHARGE_EXTENSION_PATH, RTLD_LAZY | RTLD_NODELETE);
462 if (unsubscriberHandler == nullptr) {
463 BATTERY_HILOGE(FEATURE_BATT_INFO, "Dlopen RotationMotionUnsubscriber failed, reason : %{public}s", dlerror());
464 return;
465 }
466 FuncRotationUnsubscriber rotationUnsubscriberFlag =
467 reinterpret_cast<FuncRotationUnsubscriber>(dlsym(unsubscriberHandler, ROTATION_UNSUBSCRIBER_CONFIG));
468 if (rotationUnsubscriberFlag == nullptr) {
469 BATTERY_HILOGE(FEATURE_BATT_INFO, "RotationMotionUnsubscriber is null, reason : %{public}s", dlerror());
470 dlclose(unsubscriberHandler);
471 unsubscriberHandler = nullptr;
472 return;
473 }
474 rotationUnsubscriberFlag();
475 BATTERY_HILOGI(FEATURE_BATT_INFO, "RotationMotionUnsubscriber Success");
476 dlclose(unsubscriberHandler);
477 unsubscriberHandler = nullptr;
478 return;
479 #endif
480 }
481
PublishChargingEvent(const BatteryInfo & info) const482 bool BatteryNotify::PublishChargingEvent(const BatteryInfo& info) const
483 {
484 bool isSuccess = true;
485
486 if ((info.GetChargeState() != BatteryChargeState::CHARGE_STATE_ENABLE) &&
487 (info.GetChargeState() != BatteryChargeState::CHARGE_STATE_FULL)) {
488 g_batteryChargingOnce = false;
489 return isSuccess;
490 }
491
492 if (g_batteryChargingOnce) {
493 return isSuccess;
494 }
495
496 Want want;
497 want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGING);
498 CommonEventData data;
499 data.SetWant(want);
500 CommonEventPublishInfo publishInfo;
501 publishInfo.SetOrdered(false);
502 data.SetCode(static_cast<int32_t>(info.GetChargeState()));
503 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
504 static_cast<uint32_t>(info.GetChargeState()));
505 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
506 if (!isSuccess) {
507 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
508 }
509
510 g_batteryChargingOnce = true;
511 return isSuccess;
512 }
513
PublishDischargingEvent(const BatteryInfo & info) const514 bool BatteryNotify::PublishDischargingEvent(const BatteryInfo& info) const
515 {
516 bool isSuccess = true;
517
518 if ((info.GetChargeState() == BatteryChargeState::CHARGE_STATE_ENABLE) ||
519 (info.GetChargeState() == BatteryChargeState::CHARGE_STATE_FULL)) {
520 g_batteryDischargingOnce = false;
521 return isSuccess;
522 }
523
524 if (g_batteryDischargingOnce) {
525 return isSuccess;
526 }
527
528 Want want;
529 want.SetAction(CommonEventSupport::COMMON_EVENT_DISCHARGING);
530 CommonEventData data;
531 data.SetWant(want);
532 CommonEventPublishInfo publishInfo;
533 publishInfo.SetOrdered(false);
534 data.SetCode(static_cast<int32_t>(info.GetChargeState()));
535 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
536 static_cast<uint32_t>(info.GetChargeState()));
537 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
538 if (!isSuccess) {
539 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
540 }
541
542 g_batteryDischargingOnce = true;
543 return isSuccess;
544 }
545
PublishCustomEvent(const BatteryInfo & info,const std::string & commonEventName) const546 bool BatteryNotify::PublishCustomEvent(const BatteryInfo& info, const std::string& commonEventName) const
547 {
548 Want want;
549 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_UEVENT, info.GetUevent());
550 want.SetAction(commonEventName);
551 CommonEventData data;
552 data.SetWant(want);
553 CommonEventPublishInfo publishInfo;
554 publishInfo.SetOrdered(false);
555 const std::vector<std::string> permissionVec { "ohos.permission.POWER_OPTIMIZATION" };
556 publishInfo.SetSubscriberPermissions(permissionVec);
557
558 bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
559 if (!isSuccess) {
560 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery custom event");
561 }
562 return isSuccess;
563 }
564
HandleNotification(const std::string & ueventName) const565 bool BatteryNotify::HandleNotification(const std::string& ueventName) const
566 {
567 #ifdef BATTERY_SUPPORT_NOTIFICATION
568 std::unordered_map<std::string, std::vector<BatteryConfig::PopupConf>> popupCfg =
569 BatteryConfig::GetInstance().GetPopupConf();
570 auto iter = popupCfg.find(ueventName);
571 if (iter == popupCfg.end()) {
572 BATTERY_HILOGW(COMP_SVC, "HandleNotification not found conf: %{public}s", ueventName.c_str());
573 return false;
574 }
575 typedef void(*HandleNotificationFunc)(const std::string&, int32_t,
576 const std::unordered_map<std::string, BatteryConfig::NotificationConf>&);
577 void* handler = dlopen("libbattery_notification.z.so", RTLD_LAZY | RTLD_NODELETE);
578 if (handler == nullptr) {
579 BATTERY_HILOGE(FEATURE_BATT_INFO, "dlopen HandleNotificationFunc failed, reason : %{public}s", dlerror());
580 return false;
581 }
582 HandleNotificationFunc HandleNotification =
583 reinterpret_cast<HandleNotificationFunc>(dlsym(handler, "HandleNotification"));
584 if (HandleNotification == nullptr) {
585 BATTERY_HILOGE(FEATURE_BATT_INFO, "HandleNotificationFunc is null, reason : %{public}s", dlerror());
586 dlclose(handler);
587 handler = nullptr;
588 return false;
589 }
590 auto nConfMap = BatteryConfig::GetInstance().GetNotificationConf();
591 for (auto& item : iter->second) {
592 HandleNotification(item.name, item.action, nConfMap);
593 BATTERY_HILOGI(COMP_SVC, "popupName=%{public}s, popupAction=%{public}d", item.name.c_str(), item.action);
594 }
595 dlclose(handler);
596 handler = nullptr;
597 #endif
598 return true;
599 }
600
601 } // namespace PowerMgr
602 } // namespace OHOS
603