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 "base_types.h"
19 #include "refbase.h"
20 #include "errors.h"
21 #include "string_ex.h"
22 #include "iservice_registry.h"
23 #include "if_system_ability_manager.h"
24 #include "system_ability_definition.h"
25 #include "iremote_object.h"
26 #include "matching_skills.h"
27 #include "hisysevent.h"
28 #include "common_event_publish_info.h"
29 #include "hilog/log_cpp.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
81 return isAllSuccess ? ERR_OK : ERR_NO_INIT;
82 }
83
IsCommonEventServiceAbilityExist() const84 bool BatteryNotify::IsCommonEventServiceAbilityExist() const
85 {
86 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87 if (!sysMgr) {
88 BATTERY_HILOGE(COMP_SVC,
89 "IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
90 return false;
91 }
92 sptr<IRemoteObject> remote = sysMgr->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
93 if (!remote) {
94 BATTERY_HILOGE(COMP_SVC, "No CesServiceAbility");
95 return false;
96 }
97
98 g_commonEventInitSuccess = true;
99 return true;
100 }
101
PublishChangedEvent(const BatteryInfo & info) const102 bool BatteryNotify::PublishChangedEvent(const BatteryInfo& info) const
103 {
104 Want want;
105 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY, info.GetCapacity());
106 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_VOLTAGE, info.GetVoltage());
107 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TEMPERATURE, info.GetTemperature());
108 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_HEALTH_STATE, static_cast<int32_t>(info.GetHealthState()));
109 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE, static_cast<int32_t>(info.GetPluggedType()));
110 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE, static_cast<int32_t>(info.GetChargeState()));
111 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PRESENT, info.IsPresent());
112 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TECHNOLOGY, info.GetTechnology());
113
114 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_CURRENT, info.GetPluggedMaxCurrent());
115 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE, info.GetPluggedMaxVoltage());
116 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT, info.GetNowCurrent());
117 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_COUNTER, info.GetChargeCounter());
118
119 sptr<BatteryService> batterySrv = DelayedSpSingleton<BatteryService>::GetInstance();
120 auto capacityLevel = batterySrv->GetCapacityLevel();
121 if (capacityLevel != g_lastCapacityLevel) {
122 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY_LEVEL, static_cast<int32_t>(capacityLevel));
123 // The old way of broadcasting
124 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_CAPACITY_LEVEL), static_cast<int32_t>(capacityLevel));
125 g_lastCapacityLevel = capacityLevel;
126 }
127 ChangedEventDeprecated(want, info);
128
129 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
130 CommonEventData data;
131 data.SetWant(want);
132 CommonEventPublishInfo publishInfo;
133 publishInfo.SetOrdered(false);
134 bool isSuccess = true;
135
136 HiSysEvent::Write("BATTERY", "BATTERY_CHANGED", HiSysEvent::EventType::STATISTIC,
137 "LEVEL", info.GetCapacity(), "CHARGER", static_cast<int32_t>(info.GetPluggedType()),
138 "VOLTAGE", info.GetVoltage(), "TEMPERATURE", info.GetTemperature(),
139 "HEALTH", static_cast<int32_t>(info.GetHealthState()), "CURRENT", info.GetNowCurrent());
140 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
141 if (!isSuccess) {
142 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED event");
143 }
144 return isSuccess;
145 }
146
ChangedEventDeprecated(Want & want,const BatteryInfo & info) const147 void BatteryNotify::ChangedEventDeprecated(Want& want, const BatteryInfo& info) const
148 {
149 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_CAPACITY), info.GetCapacity());
150 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_VOLTAGE), info.GetVoltage());
151 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_TEMPERATURE), info.GetTemperature());
152 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_HEALTH_STATE), static_cast<int32_t>(info.GetHealthState()));
153 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_TYPE), static_cast<int32_t>(info.GetPluggedType()));
154 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_CURRENT), info.GetPluggedMaxCurrent());
155 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_VOLTAGE), info.GetPluggedMaxVoltage());
156 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_CHARGE_STATE), static_cast<int32_t>(info.GetChargeState()));
157 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_CHARGE_COUNTER), info.GetChargeCounter());
158 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PRESENT), info.IsPresent());
159 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_TECHNOLOGY), info.GetTechnology());
160 want.SetParam(ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_NOW_CURRENT), info.GetNowCurrent());
161 }
162
PublishLowEvent(const BatteryInfo & info) const163 bool BatteryNotify::PublishLowEvent(const BatteryInfo& info) const
164 {
165 Want want;
166 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
167 CommonEventData data;
168 data.SetWant(want);
169 CommonEventPublishInfo publishInfo;
170 publishInfo.SetOrdered(false);
171 bool isSuccess = true;
172
173 if (info.GetCapacity() > lowCapacity_) {
174 g_batteryLowOnce = false;
175 return isSuccess;
176 }
177
178 if (g_batteryLowOnce) {
179 return isSuccess;
180 }
181
182 data.SetCode(info.GetCapacity());
183 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
184 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
185 if (!isSuccess) {
186 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish battery_low event");
187 }
188 g_batteryLowOnce = true;
189 return isSuccess;
190 }
191
PublishOkayEvent(const BatteryInfo & info) const192 bool BatteryNotify::PublishOkayEvent(const BatteryInfo& info) const
193 {
194 Want want;
195 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
196 CommonEventData data;
197 data.SetWant(want);
198 CommonEventPublishInfo publishInfo;
199 publishInfo.SetOrdered(false);
200 bool isSuccess = true;
201
202 if (info.GetCapacity() <= lowCapacity_) {
203 g_batteryOkOnce = false;
204 return isSuccess;
205 }
206
207 if (g_batteryOkOnce) {
208 return isSuccess;
209 }
210
211 data.SetCode(info.GetCapacity());
212 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
213 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
214 if (!isSuccess) {
215 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery_okay event");
216 }
217 g_batteryOkOnce = true;
218 return isSuccess;
219 }
220
PublishPowerConnectedEvent(const BatteryInfo & info) const221 bool BatteryNotify::PublishPowerConnectedEvent(const BatteryInfo& info) const
222 {
223 Want want;
224 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
225 CommonEventData data;
226 data.SetWant(want);
227 CommonEventPublishInfo publishInfo;
228 publishInfo.SetOrdered(false);
229 bool isSuccess = true;
230
231 if ((info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_NONE) ||
232 (info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
233 g_batteryConnectOnce = false;
234 return isSuccess;
235 }
236
237 if (g_batteryConnectOnce) {
238 return isSuccess;
239 }
240
241 data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
242 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
243 static_cast<uint32_t>(info.GetPluggedType()));
244 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
245 if (!isSuccess) {
246 BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish power_connected event");
247 }
248
249 g_batteryConnectOnce = true;
250 return isSuccess;
251 }
252
PublishPowerDisconnectedEvent(const BatteryInfo & info) const253 bool BatteryNotify::PublishPowerDisconnectedEvent(const BatteryInfo& info) const
254 {
255 Want want;
256 want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
257 CommonEventData data;
258 data.SetWant(want);
259 CommonEventPublishInfo publishInfo;
260 publishInfo.SetOrdered(false);
261 bool isSuccess = true;
262
263 if ((info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_NONE) &&
264 (info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
265 g_batteryDisconnectOnce = false;
266 return isSuccess;
267 }
268
269 if (g_batteryDisconnectOnce) {
270 return isSuccess;
271 }
272
273 data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
274 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
275 static_cast<uint32_t>(info.GetPluggedType()));
276 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
277 if (!isSuccess) {
278 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish power_disconnected event");
279 }
280
281 g_batteryDisconnectOnce = true;
282 return isSuccess;
283 }
284
PublishChargingEvent(const BatteryInfo & info) const285 bool BatteryNotify::PublishChargingEvent(const BatteryInfo& info) const
286 {
287 Want want;
288 want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGING);
289 CommonEventData data;
290 data.SetWant(want);
291 CommonEventPublishInfo publishInfo;
292 publishInfo.SetOrdered(false);
293 bool isSuccess = true;
294
295 if (info.GetChargeState() != BatteryChargeState::CHARGE_STATE_ENABLE) {
296 g_batteryChargingOnce = false;
297 return isSuccess;
298 }
299
300 if (g_batteryChargingOnce) {
301 return isSuccess;
302 }
303
304 data.SetCode(static_cast<int32_t>(info.GetChargeState()));
305 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
306 static_cast<uint32_t>(info.GetChargeState()));
307 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
308 if (!isSuccess) {
309 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
310 }
311
312 g_batteryChargingOnce = true;
313 return isSuccess;
314 }
315
PublishDischargingEvent(const BatteryInfo & info) const316 bool BatteryNotify::PublishDischargingEvent(const BatteryInfo& info) const
317 {
318 Want want;
319 want.SetAction(CommonEventSupport::COMMON_EVENT_DISCHARGING);
320 CommonEventData data;
321 data.SetWant(want);
322 CommonEventPublishInfo publishInfo;
323 publishInfo.SetOrdered(false);
324 bool isSuccess = true;
325
326 if (info.GetChargeState() == BatteryChargeState::CHARGE_STATE_ENABLE) {
327 g_batteryDischargingOnce = false;
328 return isSuccess;
329 }
330
331 if (g_batteryDischargingOnce) {
332 return isSuccess;
333 }
334
335 data.SetCode(static_cast<int32_t>(info.GetChargeState()));
336 BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
337 static_cast<uint32_t>(info.GetChargeState()));
338 isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
339 if (!isSuccess) {
340 BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
341 }
342
343 g_batteryDischargingOnce = true;
344 return isSuccess;
345 }
346 } // namespace PowerMgr
347 } // namespace OHOS
348