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_stats_listener.h"
17
18 #include <string>
19 #include <strstream>
20
21 #include "bluetooth_def.h"
22 #include "call_manager_inner_type.h"
23 #include "display_power_info.h"
24 #include "wifi_msg.h"
25
26 #include "battery_stats_service.h"
27 #include "stats_hisysevent.h"
28 #include "stats_log.h"
29 #include "stats_types.h"
30
31 namespace OHOS {
32 namespace PowerMgr {
33 namespace {
34 constexpr int32_t THERMAL_RATIO_BEGIN = 0;
35 constexpr int32_t THERMAL_RATIO_LENGTH = 4;
36 }
OnEvent(std::shared_ptr<HiviewDFX::HiSysEventRecord> sysEvent)37 void BatteryStatsListener::OnEvent(std::shared_ptr<HiviewDFX::HiSysEventRecord> sysEvent)
38 {
39 if (sysEvent == nullptr) {
40 return;
41 }
42 std::string eventName = sysEvent->GetEventName();
43 std::string eventDetail = sysEvent->AsJson();
44 STATS_HILOGD(COMP_SVC, "EventDetail: %{public}s", eventDetail.c_str());
45 if (!StatsHiSysEvent::CheckHiSysEvent(eventName)) {
46 return;
47 }
48 Json::Value root;
49 Json::CharReaderBuilder reader;
50 std::string errors;
51 std::istrstream is(eventDetail.c_str());
52 if (parseFromStream(reader, is, &root, &errors)) {
53 ProcessHiSysEvent(eventName, root);
54 } else {
55 STATS_HILOGW(COMP_SVC, "Parse hisysevent data failed");
56 }
57 }
58
ProcessHiSysEvent(const std::string & eventName,const Json::Value & root)59 void BatteryStatsListener::ProcessHiSysEvent(const std::string& eventName, const Json::Value& root)
60 {
61 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
62 auto detector = statsService->GetBatteryStatsDetector();
63 StatsUtils::StatsData data;
64 data.eventDebugInfo.clear();
65 if (eventName == StatsHiSysEvent::POWER_RUNNINGLOCK) {
66 ProcessWakelockEvent(data, root);
67 } else if (eventName == StatsHiSysEvent::SCREEN_STATE || eventName == StatsHiSysEvent::BRIGHTNESS_NIT ||
68 eventName == StatsHiSysEvent::BACKLIGHT_DISCOUNT || eventName == StatsHiSysEvent::AMBIENT_LIGHT) {
69 ProcessDispalyEvent(data, root, eventName);
70 } else if (eventName == StatsHiSysEvent::BATTERY_CHANGED) {
71 ProcessBatteryEvent(data, root);
72 } else if (eventName == StatsHiSysEvent::POWER_TEMPERATURE ||
73 eventName == StatsHiSysEvent::THERMAL_LEVEL_CHANGED ||
74 eventName == StatsHiSysEvent::THERMAL_ACTION_TRIGGERED) {
75 ProcessThermalEvent(data, root);
76 } else if (eventName == StatsHiSysEvent::POWER_WORKSCHEDULER || eventName == StatsHiSysEvent::WORK_ADD ||
77 eventName == StatsHiSysEvent::WORK_REMOVE || eventName == StatsHiSysEvent::WORK_START ||
78 eventName == StatsHiSysEvent::WORK_STOP) {
79 ProcessWorkschedulerEvent(data, root);
80 } else if (eventName == StatsHiSysEvent::CALL_STATE || eventName == StatsHiSysEvent::DATA_CONNECTION_STATE) {
81 ProcessPhoneEvent(data, root, eventName);
82 } else if (eventName == StatsHiSysEvent::TORCH_STATE) {
83 ProcessFlashlightEvent(data, root);
84 } else if (eventName == StatsHiSysEvent::CAMERA_CONNECT || eventName == StatsHiSysEvent::CAMERA_DISCONNECT ||
85 eventName == StatsHiSysEvent::FLASHLIGHT_ON || eventName == StatsHiSysEvent::FLASHLIGHT_OFF) {
86 ProcessCameraEvent(data, root, eventName);
87 } else if (eventName == StatsHiSysEvent::STREAM_CHANGE) {
88 ProcessAudioEvent(data, root);
89 } else if (eventName == StatsHiSysEvent::POWER_SENSOR_GRAVITY ||
90 eventName == StatsHiSysEvent::POWER_SENSOR_PROXIMITY) {
91 ProcessSensorEvent(data, root, eventName);
92 } else if (eventName == StatsHiSysEvent::GNSS_STATE) {
93 ProcessGnssEvent(data, root);
94 } else if (eventName == StatsHiSysEvent::BR_SWITCH_STATE || eventName == StatsHiSysEvent::DISCOVERY_STATE ||
95 eventName == StatsHiSysEvent::BLE_SWITCH_STATE || eventName == StatsHiSysEvent::BLE_SCAN_START ||
96 eventName == StatsHiSysEvent::BLE_SCAN_STOP) {
97 ProcessBluetoothEvent(data, root, eventName);
98 } else if (eventName == StatsHiSysEvent::WIFI_CONNECTION || eventName == StatsHiSysEvent::WIFI_SCAN) {
99 ProcessWifiEvent(data, root, eventName);
100 } else if (eventName == StatsHiSysEvent::START_REMOTE_ABILITY) {
101 ProcessDistributedSchedulerEvent(data, root);
102 } else if (eventName == StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT) {
103 ProcessAlarmEvent(data, root);
104 }
105 detector->HandleStatsChangedEvent(data);
106 }
107
ProcessCameraEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)108 void BatteryStatsListener::ProcessCameraEvent(StatsUtils::StatsData& data, const Json::Value& root,
109 const std::string& eventName)
110 {
111 if (eventName == StatsHiSysEvent::CAMERA_CONNECT || eventName == StatsHiSysEvent::CAMERA_DISCONNECT) {
112 data.type = StatsUtils::STATS_TYPE_CAMERA_ON;
113 if (!root["UID"].asString().empty()) {
114 data.uid = stoi(root["UID"].asString());
115 }
116 if (!root["PID"].asString().empty()) {
117 data.pid = stoi(root["PID"].asString());
118 }
119 if (!root["ID"].asString().empty()) {
120 data.deviceId = root["ID"].asString();
121 }
122 if (eventName == StatsHiSysEvent::CAMERA_CONNECT) {
123 data.state = StatsUtils::STATS_STATE_ACTIVATED;
124 } else {
125 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
126 }
127 } else if (eventName == StatsHiSysEvent::FLASHLIGHT_ON || eventName == StatsHiSysEvent::FLASHLIGHT_OFF) {
128 data.type = StatsUtils::STATS_TYPE_CAMERA_FLASHLIGHT_ON;
129 if (eventName == StatsHiSysEvent::FLASHLIGHT_ON) {
130 data.state = StatsUtils::STATS_STATE_ACTIVATED;
131 } else {
132 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
133 }
134 }
135 }
136
ProcessAudioEvent(StatsUtils::StatsData & data,const Json::Value & root)137 void BatteryStatsListener::ProcessAudioEvent(StatsUtils::StatsData& data, const Json::Value& root)
138 {
139 data.type = StatsUtils::STATS_TYPE_AUDIO_ON;
140 if (!root["UID"].asString().empty()) {
141 data.uid = stoi(root["UID"].asString());
142 }
143 if (!root["PID"].asString().empty()) {
144 data.pid = stoi(root["PID"].asString());
145 }
146 if (!root["STATE"].asString().empty()) {
147 AudioState audioState = AudioState(stoi(root["STATE"].asString()));
148 switch (audioState) {
149 case AudioState::AUDIO_STATE_RUNNING:
150 data.state = StatsUtils::STATS_STATE_ACTIVATED;
151 break;
152 case AudioState::AUDIO_STATE_STOPPED:
153 case AudioState::AUDIO_STATE_RELEASED:
154 case AudioState::AUDIO_STATE_PAUSED:
155 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
156 break;
157 default:
158 break;
159 }
160 }
161 }
162
ProcessSensorEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)163 void BatteryStatsListener::ProcessSensorEvent(StatsUtils::StatsData& data, const Json::Value& root,
164 const std::string& eventName)
165 {
166 if (eventName == StatsHiSysEvent::POWER_SENSOR_GRAVITY) {
167 data.type = StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON;
168 } else if (eventName == StatsHiSysEvent::POWER_SENSOR_PROXIMITY) {
169 data.type = StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON;
170 }
171
172 if (!root["UID"].asString().empty()) {
173 data.uid = stoi(root["UID"].asString());
174 }
175 if (!root["PID"].asString().empty()) {
176 data.pid = stoi(root["PID"].asString());
177 }
178 if (!root["STATE"].asString().empty()) {
179 if (root["STATE"].asString() == "1") {
180 data.state = StatsUtils::STATS_STATE_ACTIVATED;
181 } else if (root["STATE"].asString() == "0") {
182 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
183 }
184 }
185 }
186
ProcessGnssEvent(StatsUtils::StatsData & data,const Json::Value & root)187 void BatteryStatsListener::ProcessGnssEvent(StatsUtils::StatsData& data, const Json::Value& root)
188 {
189 data.type = StatsUtils::STATS_TYPE_GNSS_ON;
190 if (!root["UID"].asString().empty()) {
191 data.uid = stoi(root["UID"].asString());
192 }
193 if (!root["PID"].asString().empty()) {
194 data.pid = stoi(root["PID"].asString());
195 }
196 if (!root["STATE"].asString().empty()) {
197 if (root["STATE"].asString() == "start") {
198 data.state = StatsUtils::STATS_STATE_ACTIVATED;
199 } else if (root["STATE"].asString() == "stop") {
200 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
201 }
202 }
203 }
204
ProcessBluetoothBrEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)205 void BatteryStatsListener::ProcessBluetoothBrEvent(StatsUtils::StatsData& data, const Json::Value& root,
206 const std::string& eventName)
207 {
208 if (eventName == StatsHiSysEvent::BR_SWITCH_STATE) {
209 data.type = StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON;
210 if (!root["STATE"].asString().empty()) {
211 if (stoi(root["STATE"].asString()) == Bluetooth::BTStateID::STATE_TURN_ON) {
212 data.state = StatsUtils::STATS_STATE_ACTIVATED;
213 } else if (stoi(root["STATE"].asString()) == Bluetooth::BTStateID::STATE_TURN_OFF) {
214 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
215 }
216 }
217 } else if (eventName == StatsHiSysEvent::DISCOVERY_STATE) {
218 data.type = StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN;
219 if (!root["STATE"].asString().empty()) {
220 if (stoi(root["STATE"].asString()) == Bluetooth::DISCOVERY_STARTED) {
221 data.state = StatsUtils::STATS_STATE_ACTIVATED;
222 } else if (stoi(root["STATE"].asString()) == Bluetooth::DISCOVERY_STOPED) {
223 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
224 }
225 }
226 if (!root["UID"].asString().empty()) {
227 data.uid = stoi(root["UID"].asString());
228 }
229 if (!root["PID"].asString().empty()) {
230 data.pid = stoi(root["PID"].asString());
231 }
232 }
233 }
234
ProcessBluetoothBleEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)235 void BatteryStatsListener::ProcessBluetoothBleEvent(StatsUtils::StatsData& data, const Json::Value& root,
236 const std::string& eventName)
237 {
238 if (eventName == StatsHiSysEvent::BLE_SWITCH_STATE) {
239 data.type = StatsUtils::STATS_TYPE_BLUETOOTH_BLE_ON;
240 if (!root["STATE"].asString().empty()) {
241 if (stoi(root["STATE"].asString()) == Bluetooth::BTStateID::STATE_TURN_ON) {
242 data.state = StatsUtils::STATS_STATE_ACTIVATED;
243 } else if (stoi(root["STATE"].asString()) == Bluetooth::BTStateID::STATE_TURN_OFF) {
244 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
245 }
246 }
247 } else if (eventName == StatsHiSysEvent::BLE_SCAN_START || eventName == StatsHiSysEvent::BLE_SCAN_STOP) {
248 data.type = StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN;
249 if (eventName == StatsHiSysEvent::BLE_SCAN_START) {
250 data.state = StatsUtils::STATS_STATE_ACTIVATED;
251 } else if (eventName == StatsHiSysEvent::BLE_SCAN_STOP) {
252 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
253 }
254 if (!root["UID"].asString().empty()) {
255 data.uid = stoi(root["UID"].asString());
256 }
257 if (!root["PID"].asString().empty()) {
258 data.pid = stoi(root["PID"].asString());
259 }
260 }
261 }
262
ProcessBluetoothEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)263 void BatteryStatsListener::ProcessBluetoothEvent(StatsUtils::StatsData& data, const Json::Value& root,
264 const std::string& eventName)
265 {
266 if (eventName == StatsHiSysEvent::BR_SWITCH_STATE || eventName == StatsHiSysEvent::DISCOVERY_STATE) {
267 ProcessBluetoothBrEvent(data, root, eventName);
268 } else if (eventName == StatsHiSysEvent::BLE_SWITCH_STATE ||eventName == StatsHiSysEvent::BLE_SCAN_START ||
269 eventName == StatsHiSysEvent::BLE_SCAN_STOP) {
270 ProcessBluetoothBleEvent(data, root, eventName);
271 }
272 }
273
ProcessWifiEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)274 void BatteryStatsListener::ProcessWifiEvent(StatsUtils::StatsData& data, const Json::Value& root,
275 const std::string& eventName)
276 {
277 if (eventName == StatsHiSysEvent::WIFI_CONNECTION) {
278 data.type = StatsUtils::STATS_TYPE_WIFI_ON;
279 if (!root["TYPE"].asString().empty()) {
280 Wifi::ConnState connectionType = Wifi::ConnState(stoi(root["TYPE"].asString()));
281 switch (connectionType) {
282 case Wifi::ConnState::CONNECTED:
283 data.state = StatsUtils::STATS_STATE_ACTIVATED;
284 break;
285 case Wifi::ConnState::DISCONNECTED:
286 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
287 break;
288 default:
289 break;
290 }
291 }
292 } else if (eventName == StatsHiSysEvent::WIFI_SCAN) {
293 data.type = StatsUtils::STATS_TYPE_WIFI_SCAN;
294 data.traffic = 1;
295 }
296 }
297
ProcessPhoneDebugInfo(StatsUtils::StatsData & data,const Json::Value & root)298 void BatteryStatsListener::ProcessPhoneDebugInfo(StatsUtils::StatsData& data, const Json::Value& root)
299 {
300 if (!root["name_"].asString().empty()) {
301 data.eventDebugInfo.append("Event name = ").append(root["name_"].asString());
302 }
303 if (!root["STATE"].asString().empty()) {
304 data.eventDebugInfo.append(" State = ").append(root["STATE"].asString());
305 }
306 if (!root["SLOT_ID"].asString().empty()) {
307 data.eventDebugInfo.append(" Slot ID = ").append(root["SLOT_ID"].asString());
308 }
309 if (!root["INDEX_ID"].asString().empty()) {
310 data.eventDebugInfo.append(" Index ID = ").append(root["INDEX_ID"].asString());
311 }
312 }
313
ProcessPhoneEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)314 void BatteryStatsListener::ProcessPhoneEvent(StatsUtils::StatsData& data, const Json::Value& root,
315 const std::string& eventName)
316 {
317 if (eventName == StatsHiSysEvent::CALL_STATE) {
318 data.type = StatsUtils::STATS_TYPE_PHONE_ACTIVE;
319 if (!root["STATE"].asString().empty()) {
320 Telephony::TelCallState callState = Telephony::TelCallState(stoi(root["STATE"].asString()));
321 switch (callState) {
322 case Telephony::TelCallState::CALL_STATUS_ACTIVE:
323 data.state = StatsUtils::STATS_STATE_ACTIVATED;
324 break;
325 case Telephony::TelCallState::CALL_STATUS_DISCONNECTED:
326 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
327 break;
328 default:
329 break;
330 }
331 }
332 } else if (eventName == StatsHiSysEvent::DATA_CONNECTION_STATE) {
333 data.type = StatsUtils::STATS_TYPE_PHONE_DATA;
334 if (!root["STATE"].asString().empty()) {
335 if (root["STATE"].asString() == "1") {
336 data.state = StatsUtils::STATS_STATE_ACTIVATED;
337 } else if (root["STATE"].asString() == "0") {
338 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
339 }
340 }
341 }
342
343 /**
344 * The average power consumption of phone call and phone data is divided by level
345 * However, the Telephony event has no input level information, so use level 0
346 */
347 data.level = 0;
348 ProcessPhoneDebugInfo(data, root);
349 }
350
ProcessFlashlightEvent(StatsUtils::StatsData & data,const Json::Value & root)351 void BatteryStatsListener::ProcessFlashlightEvent(StatsUtils::StatsData& data, const Json::Value& root)
352 {
353 data.type = StatsUtils::STATS_TYPE_FLASHLIGHT_ON;
354 if (!root["UID"].asString().empty()) {
355 data.uid = stoi(root["UID"].asString());
356 }
357 if (!root["PID"].asString().empty()) {
358 data.pid = stoi(root["PID"].asString());
359 }
360 if (!root["STATE"].asString().empty()) {
361 if (root["STATE"].asString() == "1") {
362 data.state = StatsUtils::STATS_STATE_ACTIVATED;
363 } else if (root["STATE"].asString() == "0") {
364 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
365 }
366 }
367 }
368
ProcessWakelockEvent(StatsUtils::StatsData & data,const Json::Value & root)369 void BatteryStatsListener::ProcessWakelockEvent(StatsUtils::StatsData& data, const Json::Value& root)
370 {
371 data.type = StatsUtils::STATS_TYPE_WAKELOCK_HOLD;
372 if (!root["UID"].asString().empty()) {
373 data.uid = stoi(root["UID"].asString());
374 }
375 if (!root["PID"].asString().empty()) {
376 data.pid = stoi(root["PID"].asString());
377 }
378 if (!root["STATE"].asString().empty()) {
379 RunningLockState lockState = RunningLockState(stoi(root["STATE"].asString()));
380 std::string stateLabel = "";
381 switch (lockState) {
382 case RunningLockState::RUNNINGLOCK_STATE_DISABLE: {
383 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
384 stateLabel = "Disable";
385 break;
386 }
387 case RunningLockState::RUNNINGLOCK_STATE_ENABLE: {
388 data.state = StatsUtils::STATS_STATE_ACTIVATED;
389 stateLabel = "Enable";
390 break;
391 }
392 case RunningLockState::RUNNINGLOCK_STATE_PROXIED:
393 case RunningLockState::RUNNINGLOCK_STATE_UNPROXIED_RESTORE: {
394 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
395 stateLabel = "Proxied";
396 break;
397 }
398 default:
399 break;
400 }
401 data.eventDebugInfo.append(" STATE = ").append(stateLabel);
402 }
403 if (!root["TYPE"].asString().empty()) {
404 data.eventDataType = stoi(root["TYPE"].asString());
405 }
406 if (!root["NAME"].asString().empty()) {
407 data.eventDataName = root["NAME"].asString();
408 }
409 if (!root["LOG_LEVEL"].asString().empty()) {
410 data.eventDebugInfo.append(" LOG_LEVEL = ").append(root["LOG_LEVEL"].asString());
411 }
412 if (!root["TAG"].asString().empty()) {
413 data.eventDebugInfo.append(" TAG = ").append(root["TAG"].asString());
414 }
415 if (!root["MESSAGE"].asString().empty()) {
416 data.eventDebugInfo.append(" MESSAGE = ").append(root["MESSAGE"].asString());
417 }
418 }
419
ProcessDispalyDebugInfo(StatsUtils::StatsData & data,const Json::Value & root)420 void BatteryStatsListener::ProcessDispalyDebugInfo(StatsUtils::StatsData& data, const Json::Value& root)
421 {
422 if (!root["name_"].asString().empty()) {
423 data.eventDebugInfo.append("Event name = ").append(root["name_"].asString());
424 }
425 if (!root["STATE"].asString().empty()) {
426 data.eventDebugInfo.append(" Screen state = ").append(root["STATE"].asString());
427 }
428 if (!root["BRIGHTNESS"].asString().empty()) {
429 data.eventDebugInfo.append(" Screen brightness = ").append(root["BRIGHTNESS"].asString());
430 }
431 if (!root["REASON"].asString().empty()) {
432 data.eventDebugInfo.append(" Brightness reason = ").append(root["REASON"].asString());
433 }
434 if (!root["NIT"].asString().empty()) {
435 data.eventDebugInfo.append(" Brightness nit = ").append(root["NIT"].asString());
436 }
437 if (!root["RATIO"].asString().empty()) {
438 data.eventDebugInfo.append(" Ratio = ").append(root["RATIO"].asString());
439 }
440 if (!root["TYPE"].asString().empty()) {
441 data.eventDebugInfo.append(" Ambient type = ").append(root["TYPE"].asString());
442 }
443 if (!root["LEVEL"].asString().empty()) {
444 data.eventDebugInfo.append(" Ambient brightness = ").append(root["LEVEL"].asString());
445 }
446 }
447
ProcessDispalyEvent(StatsUtils::StatsData & data,const Json::Value & root,const std::string & eventName)448 void BatteryStatsListener::ProcessDispalyEvent(StatsUtils::StatsData& data, const Json::Value& root,
449 const std::string& eventName)
450 {
451 data.type = StatsUtils::STATS_TYPE_DISPLAY;
452 if (eventName == StatsHiSysEvent::SCREEN_STATE) {
453 data.type = StatsUtils::STATS_TYPE_SCREEN_ON;
454 if (!root["STATE"].asString().empty()) {
455 DisplayPowerMgr::DisplayState displayState = DisplayPowerMgr::DisplayState(stoi(root["STATE"].asString()));
456 switch (displayState) {
457 case DisplayPowerMgr::DisplayState::DISPLAY_OFF:
458 data.state = StatsUtils::STATS_STATE_DEACTIVATED;
459 break;
460 case DisplayPowerMgr::DisplayState::DISPLAY_ON:
461 data.state = StatsUtils::STATS_STATE_ACTIVATED;
462 break;
463 default:
464 break;
465 }
466 }
467 } else if (eventName == StatsHiSysEvent::BRIGHTNESS_NIT) {
468 data.type = StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS;
469 if (!root["BRIGHTNESS"].asString().empty()) {
470 data.level = stoi(root["BRIGHTNESS"].asString());
471 }
472 }
473 ProcessDispalyDebugInfo(data, root);
474 }
475
ProcessBatteryEvent(StatsUtils::StatsData & data,const Json::Value & root)476 void BatteryStatsListener::ProcessBatteryEvent(StatsUtils::StatsData& data, const Json::Value& root)
477 {
478 data.type = StatsUtils::STATS_TYPE_BATTERY;
479 if (!root["LEVEL"].asString().empty()) {
480 data.level = stoi(root["LEVEL"].asString());
481 }
482 if (!root["CHARGER"].asString().empty()) {
483 data.eventDataExtra = stoi(root["CHARGER"].asString());
484 }
485 if (!root["VOLTAGE"].asString().empty()) {
486 data.eventDebugInfo.append(" Voltage = ").append(root["VOLTAGE"].asString());
487 }
488 if (!root["HEALTH"].asString().empty()) {
489 data.eventDebugInfo.append(" Health = ").append(root["HEALTH"].asString());
490 }
491 if (!root["TEMPERATURE"].asString().empty()) {
492 data.eventDebugInfo.append(" Temperature = ").append(root["TEMPERATURE"].asString());
493 }
494 }
495
ProcessThermalEvent(StatsUtils::StatsData & data,const Json::Value & root)496 void BatteryStatsListener::ProcessThermalEvent(StatsUtils::StatsData& data, const Json::Value& root)
497 {
498 data.type = StatsUtils::STATS_TYPE_THERMAL;
499 if (!root["name_"].asString().empty()) {
500 data.eventDebugInfo.append("Event name = ").append(root["name_"].asString());
501 }
502 if (!root["NAME"].asString().empty()) {
503 data.eventDebugInfo.append(" Name = ").append(root["NAME"].asString());
504 }
505 if (!root["TEMPERATURE"].asString().empty()) {
506 data.eventDebugInfo.append(" Temperature = ").append(root["TEMPERATURE"].asString());
507 }
508 if (!root["LEVEL"].asString().empty()) {
509 data.eventDebugInfo.append(" Temperature level = ").append(root["LEVEL"].asString());
510 }
511 if (!root["ACTION"].asString().empty()) {
512 data.eventDebugInfo.append(" Action name = ").append(root["ACTION"].asString());
513 }
514 if (!root["VALUE"].asString().empty()) {
515 data.eventDebugInfo.append(" Value = ").append(root["VALUE"].asString());
516 }
517 if (!root["RATIO"].asString().empty()) {
518 std::string ratio = std::to_string(root["RATIO"].asFloat()).substr(THERMAL_RATIO_BEGIN, THERMAL_RATIO_LENGTH);
519 data.eventDebugInfo.append(" Ratio = ").append(ratio);
520 }
521 }
522
ProcessWorkschedulerEvent(StatsUtils::StatsData & data,const Json::Value & root)523 void BatteryStatsListener::ProcessWorkschedulerEvent(StatsUtils::StatsData& data, const Json::Value& root)
524 {
525 data.type = StatsUtils::STATS_TYPE_WORKSCHEDULER;
526 if (root["name_"].asString() == StatsHiSysEvent::POWER_WORKSCHEDULER) {
527 if (!root["UID"].asString().empty()) {
528 data.uid = stoi(root["UID"].asString());
529 }
530 if (!root["PID"].asString().empty()) {
531 data.pid = stoi(root["PID"].asString());
532 }
533 if (!root["STATE"].asString().empty()) {
534 data.state = StatsUtils::StatsState(stoi(root["STATE"].asString()));
535 }
536 if (!root["TYPE"].asString().empty()) {
537 data.eventDataType = stoi(root["TYPE"].asString());
538 }
539 if (!root["INTERVAL"].asString().empty()) {
540 data.eventDataExtra = stoi(root["INTERVAL"].asString());
541 }
542 } else {
543 if (!root["name_"].asString().empty()) {
544 data.eventDebugInfo.append(root["name_"].asString()).append(":");
545 }
546 if (!root["UID"].asString().empty()) {
547 data.uid = stoi(root["UID"].asString());
548 }
549 if (!root["PID"].asString().empty()) {
550 data.pid = stoi(root["PID"].asString());
551 }
552 if (!root["NAME"].asString().empty()) {
553 data.eventDebugInfo.append(" Bundle name = ").append(root["NAME"].asString());
554 }
555 if (!root["WORKID"].asString().empty()) {
556 data.eventDebugInfo.append(" Work ID = ").append(root["WORKID"].asString());
557 }
558 if (!root["TRIGGER"].asString().empty()) {
559 data.eventDebugInfo.append(" Trigger conditions = ").append(root["TRIGGER"].asString());
560 }
561 if (!root["TYPE"].asString().empty()) {
562 data.eventDebugInfo.append(" Work type = ").append(root["TYPE"].asString());
563 }
564 if (!root["INTERVAL"].asString().empty()) {
565 data.eventDebugInfo.append(" Interval = ").append(root["INTERVAL"].asString());
566 }
567 }
568 }
569
ProcessDistributedSchedulerEvent(StatsUtils::StatsData & data,const Json::Value & root)570 void BatteryStatsListener::ProcessDistributedSchedulerEvent(StatsUtils::StatsData& data, const Json::Value& root)
571 {
572 data.type = StatsUtils::STATS_TYPE_DISTRIBUTEDSCHEDULER;
573 if (!root["name_"].asString().empty()) {
574 data.eventDebugInfo.append("Event name = ").append(root["name_"].asString());
575 }
576 if (!root["CALLING_TYPE"].asString().empty()) {
577 data.eventDebugInfo.append(" Calling Type = ").append(root["CALLING_TYPE"].asString());
578 }
579 if (!root["CALLING_UID"].asString().empty()) {
580 data.eventDebugInfo.append(" Calling Uid = ").append(root["CALLING_UID"].asString());
581 }
582 if (!root["CALLING_PID"].asString().empty()) {
583 data.eventDebugInfo.append(" Calling Pid = ").append(root["CALLING_PID"].asString());
584 }
585 if (!root["TARGET_BUNDLE"].asString().empty()) {
586 data.eventDebugInfo.append(" Target Bundle Name = ").append(root["TARGET_BUNDLE"].asString());
587 }
588 if (!root["TARGET_ABILITY"].asString().empty()) {
589 data.eventDebugInfo.append(" Target Ability Name = ").append(root["TARGET_ABILITY"].asString());
590 }
591 if (!root["CALLING_APP_UID"].asString().empty()) {
592 data.eventDebugInfo.append(" Calling App Uid = ").append(root["CALLING_APP_UID"].asString());
593 }
594 if (!root["RESULT"].asString().empty()) {
595 data.eventDebugInfo.append(" RESULT = ").append(root["RESULT"].asString());
596 }
597 }
598
ProcessAlarmEvent(StatsUtils::StatsData & data,const Json::Value & root)599 void BatteryStatsListener::ProcessAlarmEvent(StatsUtils::StatsData& data, const Json::Value& root)
600 {
601 data.type = StatsUtils::STATS_TYPE_ALARM;
602 data.traffic = 1;
603 if (!root["CALLER_UID"].asString().empty()) {
604 data.uid = stoi(root["CALLER_UID"].asString());
605 }
606 if (!root["CALLER_PID"].asString().empty()) {
607 data.pid = stoi(root["CALLER_PID"].asString());
608 }
609 }
610
OnServiceDied()611 void BatteryStatsListener::OnServiceDied()
612 {
613 STATS_HILOGE(COMP_SVC, "Service disconnected");
614 }
615 } // namespace PowerMgr
616 } // namespace OHOS
617