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