• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "common_event_data.h"
19 #include "common_event_manager.h"
20 #include "common_event_publish_info.h"
21 #include "common_event_support.h"
22 #include "errors.h"
23 #include "hisysevent.h"
24 #include "if_system_ability_manager.h"
25 #include "iservice_registry.h"
26 #include "string_ex.h"
27 #include "system_ability_definition.h"
28 
29 #include "battery_config.h"
30 #include "battery_log.h"
31 #include "battery_service.h"
32 
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::EventFwk;
35 using namespace OHOS::HiviewDFX;
36 
37 namespace OHOS {
38 namespace PowerMgr {
39 bool g_batteryLowOnce = false;
40 bool g_batteryOkOnce = false;
41 bool g_batteryConnectOnce = false;
42 bool g_batteryDisconnectOnce = false;
43 bool g_batteryChargingOnce = false;
44 bool g_batteryDischargingOnce = false;
45 bool g_commonEventInitSuccess = false;
46 OHOS::PowerMgr::BatteryCapacityLevel g_lastCapacityLevel = OHOS::PowerMgr::BatteryCapacityLevel::LEVEL_NONE;
47 
BatteryNotify()48 BatteryNotify::BatteryNotify()
49 {
50     const int32_t DEFAULT_LOW_CAPACITY = 20;
51     lowCapacity_ = BatteryConfig::GetInstance().GetInt("soc.low", DEFAULT_LOW_CAPACITY);
52     BATTERY_HILOGI(COMP_SVC, "Low broadcast power=%{public}d", lowCapacity_);
53 }
54 
PublishEvents(const BatteryInfo & info)55 int32_t BatteryNotify::PublishEvents(const BatteryInfo& info)
56 {
57     if (g_commonEventInitSuccess) {
58         BATTERY_HILOGI(COMP_SVC, "common event service ability init success");
59     } else {
60         if (!IsCommonEventServiceAbilityExist()) {
61             return ERR_NO_INIT;
62         }
63     }
64 
65     bool isAllSuccess = true;
66     bool ret = PublishChangedEvent(info);
67     isAllSuccess &= ret;
68     ret = PublishLowEvent(info);
69     isAllSuccess &= ret;
70     ret = PublishOkayEvent(info);
71     isAllSuccess &= ret;
72     ret = PublishPowerConnectedEvent(info);
73     isAllSuccess &= ret;
74     ret = PublishPowerDisconnectedEvent(info);
75     isAllSuccess &= ret;
76     ret = PublishChargingEvent(info);
77     isAllSuccess &= ret;
78     ret = PublishDischargingEvent(info);
79     isAllSuccess &= ret;
80     ret = PublishChargeTypeChangedEvent(info);
81     isAllSuccess &= ret;
82 
83     return isAllSuccess ? ERR_OK : ERR_NO_INIT;
84 }
85 
PublishChargeTypeChangedEvent(const BatteryInfo & info)86 bool BatteryNotify::PublishChargeTypeChangedEvent(const BatteryInfo& info)
87 {
88     ChargeType chargeType = info.GetChargeType();
89     bool isSuccess = true;
90     if (batteryInfoChargeType_ == chargeType) {
91         BATTERY_HILOGD(COMP_SVC, "No need to send chargetype event");
92         return isSuccess;
93     }
94     batteryInfoChargeType_ = chargeType;
95     Want want;
96     want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED);
97     CommonEventData data;
98     data.SetWant(want);
99     CommonEventPublishInfo publishInfo;
100     publishInfo.SetOrdered(false);
101 
102     data.SetCode(static_cast<int32_t>(chargeType));
103     BATTERY_HILOGD(COMP_SVC, "publisher chargeType=%{public}d", chargeType);
104     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
105     if (!isSuccess) {
106         BATTERY_HILOGD(COMP_SVC, "failed to publish battery charge type event");
107     }
108 
109     return isSuccess;
110 }
111 
IsCommonEventServiceAbilityExist() const112 bool BatteryNotify::IsCommonEventServiceAbilityExist() const
113 {
114     sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
115     if (!sysMgr) {
116         BATTERY_HILOGE(COMP_SVC,
117             "IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
118         return false;
119     }
120     sptr<IRemoteObject> remote = sysMgr->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
121     if (!remote) {
122         BATTERY_HILOGE(COMP_SVC, "No CesServiceAbility");
123         return false;
124     }
125 
126     g_commonEventInitSuccess = true;
127     return true;
128 }
129 
PublishChangedEvent(const BatteryInfo & info) const130 bool BatteryNotify::PublishChangedEvent(const BatteryInfo& info) const
131 {
132     Want want;
133     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY, info.GetCapacity());
134     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_VOLTAGE, info.GetVoltage());
135     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TEMPERATURE, info.GetTemperature());
136     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_HEALTH_STATE, static_cast<int32_t>(info.GetHealthState()));
137     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE, static_cast<int32_t>(info.GetPluggedType()));
138     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE, static_cast<int32_t>(info.GetChargeState()));
139     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PRESENT, info.IsPresent());
140     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TECHNOLOGY, info.GetTechnology());
141 
142     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_CURRENT, info.GetPluggedMaxCurrent());
143     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE, info.GetPluggedMaxVoltage());
144     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT, info.GetNowCurrent());
145     want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_COUNTER, info.GetChargeCounter());
146 
147     sptr<BatteryService> batterySrv = DelayedSpSingleton<BatteryService>::GetInstance();
148     auto capacityLevel = batterySrv->GetCapacityLevel();
149     if (capacityLevel != g_lastCapacityLevel) {
150         want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY_LEVEL, static_cast<int32_t>(capacityLevel));
151         g_lastCapacityLevel = capacityLevel;
152     };
153 
154     want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
155     CommonEventData data;
156     data.SetWant(want);
157     CommonEventPublishInfo publishInfo;
158     publishInfo.SetOrdered(false);
159     bool isSuccess = true;
160 
161     HiSysEventWrite(HiSysEvent::Domain::BATTERY, "CHANGED", HiSysEvent::EventType::STATISTIC,
162         "LEVEL", info.GetCapacity(), "CHARGER", static_cast<int32_t>(info.GetPluggedType()),
163         "VOLTAGE", info.GetVoltage(), "TEMPERATURE", info.GetTemperature(),
164         "HEALTH", static_cast<int32_t>(info.GetHealthState()), "CURRENT", info.GetNowCurrent());
165     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
166     if (!isSuccess) {
167         BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED event");
168     }
169     return isSuccess;
170 }
171 
PublishLowEvent(const BatteryInfo & info) const172 bool BatteryNotify::PublishLowEvent(const BatteryInfo& info) const
173 {
174     bool isSuccess = true;
175 
176     if (info.GetCapacity() > lowCapacity_) {
177         g_batteryLowOnce = false;
178         return isSuccess;
179     }
180 
181     if (g_batteryLowOnce) {
182         return isSuccess;
183     }
184 
185     Want want;
186     want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
187     CommonEventData data;
188     data.SetWant(want);
189     CommonEventPublishInfo publishInfo;
190     publishInfo.SetOrdered(false);
191     data.SetCode(info.GetCapacity());
192     BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
193     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
194     if (!isSuccess) {
195         BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish battery_low event");
196     }
197     g_batteryLowOnce = true;
198     return isSuccess;
199 }
200 
PublishOkayEvent(const BatteryInfo & info) const201 bool BatteryNotify::PublishOkayEvent(const BatteryInfo& info) const
202 {
203     bool isSuccess = true;
204 
205     if (info.GetCapacity() <= lowCapacity_) {
206         g_batteryOkOnce = false;
207         return isSuccess;
208     }
209 
210     if (g_batteryOkOnce) {
211         return isSuccess;
212     }
213 
214     Want want;
215     want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
216     CommonEventData data;
217     data.SetWant(want);
218     CommonEventPublishInfo publishInfo;
219     publishInfo.SetOrdered(false);
220     data.SetCode(info.GetCapacity());
221     BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
222     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
223     if (!isSuccess) {
224         BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery_okay event");
225     }
226     g_batteryOkOnce = true;
227     return isSuccess;
228 }
229 
PublishPowerConnectedEvent(const BatteryInfo & info) const230 bool BatteryNotify::PublishPowerConnectedEvent(const BatteryInfo& info) const
231 {
232     bool isSuccess = true;
233 
234     if ((info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_NONE) ||
235         (info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
236         g_batteryConnectOnce = false;
237         return isSuccess;
238     }
239 
240     if (g_batteryConnectOnce) {
241         return isSuccess;
242     }
243 
244     Want want;
245     want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
246     CommonEventData data;
247     data.SetWant(want);
248     CommonEventPublishInfo publishInfo;
249     publishInfo.SetOrdered(false);
250     data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
251     BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
252         static_cast<uint32_t>(info.GetPluggedType()));
253     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
254     if (!isSuccess) {
255         BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish power_connected event");
256     }
257 
258     g_batteryConnectOnce = true;
259     return isSuccess;
260 }
261 
PublishPowerDisconnectedEvent(const BatteryInfo & info) const262 bool BatteryNotify::PublishPowerDisconnectedEvent(const BatteryInfo& info) const
263 {
264     bool isSuccess = true;
265 
266     if ((info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_NONE) &&
267         (info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
268         g_batteryDisconnectOnce = false;
269         return isSuccess;
270     }
271 
272     if (g_batteryDisconnectOnce) {
273         return isSuccess;
274     }
275 
276     Want want;
277     want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
278     CommonEventData data;
279     data.SetWant(want);
280     CommonEventPublishInfo publishInfo;
281     publishInfo.SetOrdered(false);
282     data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
283     BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
284         static_cast<uint32_t>(info.GetPluggedType()));
285     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
286     if (!isSuccess) {
287         BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish power_disconnected event");
288     }
289 
290     g_batteryDisconnectOnce = true;
291     return isSuccess;
292 }
293 
PublishChargingEvent(const BatteryInfo & info) const294 bool BatteryNotify::PublishChargingEvent(const BatteryInfo& info) const
295 {
296     bool isSuccess = true;
297 
298     if (info.GetChargeState() != BatteryChargeState::CHARGE_STATE_ENABLE) {
299         g_batteryChargingOnce = false;
300         return isSuccess;
301     }
302 
303     if (g_batteryChargingOnce) {
304         return isSuccess;
305     }
306 
307     Want want;
308     want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGING);
309     CommonEventData data;
310     data.SetWant(want);
311     CommonEventPublishInfo publishInfo;
312     publishInfo.SetOrdered(false);
313     data.SetCode(static_cast<int32_t>(info.GetChargeState()));
314     BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
315         static_cast<uint32_t>(info.GetChargeState()));
316     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
317     if (!isSuccess) {
318         BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
319     }
320 
321     g_batteryChargingOnce = true;
322     return isSuccess;
323 }
324 
PublishDischargingEvent(const BatteryInfo & info) const325 bool BatteryNotify::PublishDischargingEvent(const BatteryInfo& info) const
326 {
327     bool isSuccess = true;
328 
329     if (info.GetChargeState() == BatteryChargeState::CHARGE_STATE_ENABLE) {
330         g_batteryDischargingOnce = false;
331         return isSuccess;
332     }
333 
334     if (g_batteryDischargingOnce) {
335         return isSuccess;
336     }
337 
338     Want want;
339     want.SetAction(CommonEventSupport::COMMON_EVENT_DISCHARGING);
340     CommonEventData data;
341     data.SetWant(want);
342     CommonEventPublishInfo publishInfo;
343     publishInfo.SetOrdered(false);
344     data.SetCode(static_cast<int32_t>(info.GetChargeState()));
345     BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
346         static_cast<uint32_t>(info.GetChargeState()));
347     isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
348     if (!isSuccess) {
349         BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
350     }
351 
352     g_batteryDischargingOnce = true;
353     return isSuccess;
354 }
355 } // namespace PowerMgr
356 } // namespace OHOS
357