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