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 #ifndef BATTERY_STATS_CORE_H 17 #define BATTERY_STATS_CORE_H 18 19 #include <memory> 20 #include <string> 21 #include <cstdint> 22 #include <iosfwd> 23 #include "json/value.h" 24 25 #include "battery_stats_info.h" 26 #include "entities/battery_stats_entity.h" 27 #include "stats_log.h" 28 #include "stats_utils.h" 29 30 namespace OHOS { 31 namespace PowerMgr { 32 class BatteryStatsCore { 33 public: BatteryStatsCore()34 explicit BatteryStatsCore() 35 { 36 STATS_HILOGI(COMP_SVC, "BatteryStatsCore instance is created"); 37 } 38 ~BatteryStatsCore() = default; 39 void ComputePower(); 40 BatteryStatsInfoList GetBatteryStats(); 41 double GetAppStatsMah(const int32_t& uid); 42 double GetAppStatsPercent(const int32_t& uid); 43 double GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type); 44 double GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type); 45 int64_t GetTotalTimeMs(StatsUtils::StatsType statsType, int16_t level = StatsUtils::INVALID_VALUE); 46 int64_t GetTotalTimeMs(int32_t uid, StatsUtils::StatsType statsType, 47 int16_t level = StatsUtils::INVALID_VALUE); 48 int64_t GetTotalDataCount(StatsUtils::StatsType statsType, int32_t uid = StatsUtils::INVALID_VALUE); 49 int64_t GetTotalConsumptionCount(StatsUtils::StatsType statsType, int32_t uid = StatsUtils::INVALID_VALUE); 50 void UpdateStats(StatsUtils::StatsType statsType, StatsUtils::StatsState state, 51 int16_t level = StatsUtils::INVALID_VALUE, int32_t uid = StatsUtils::INVALID_VALUE, 52 const std::string& deviceId = ""); 53 void UpdateStats(StatsUtils::StatsType statsType, int64_t time, int64_t data, 54 int32_t uid = StatsUtils::INVALID_VALUE); 55 std::shared_ptr<BatteryStatsEntity> GetEntity(const BatteryStatsInfo::ConsumptionType& type); 56 bool SaveBatteryStatsData(); 57 bool LoadBatteryStatsData(); 58 void DumpInfo(std::string& result); 59 void UpdateDebugInfo(const std::string& info); 60 void GetDebugInfo(std::string& result); 61 void Reset(); 62 bool Init(); 63 private: 64 std::shared_ptr<BatteryStatsEntity> audioEntity_; 65 std::shared_ptr<BatteryStatsEntity> bluetoothEntity_; 66 std::shared_ptr<BatteryStatsEntity> cameraEntity_; 67 std::shared_ptr<BatteryStatsEntity> cpuEntity_; 68 std::shared_ptr<BatteryStatsEntity> flashlightEntity_; 69 std::shared_ptr<BatteryStatsEntity> gnssEntity_; 70 std::shared_ptr<BatteryStatsEntity> idleEntity_; 71 std::shared_ptr<BatteryStatsEntity> phoneEntity_; 72 std::shared_ptr<BatteryStatsEntity> screenEntity_; 73 std::shared_ptr<BatteryStatsEntity> sensorEntity_; 74 std::shared_ptr<BatteryStatsEntity> uidEntity_; 75 std::shared_ptr<BatteryStatsEntity> userEntity_; 76 std::shared_ptr<BatteryStatsEntity> wifiEntity_; 77 std::shared_ptr<BatteryStatsEntity> wakelockEntity_; 78 std::shared_ptr<BatteryStatsEntity> alarmEntity_; 79 bool isCameraOn_ = false; 80 bool isScreenOn_ = false; 81 int32_t lastBrightnessLevel_ = StatsUtils::INVALID_VALUE; 82 int32_t lastCameraUid_ = StatsUtils::INVALID_VALUE; 83 std::mutex mutex_; 84 std::string debugInfo_; 85 void UpdateTimer(std::shared_ptr<BatteryStatsEntity> entity, StatsUtils::StatsType statsType, 86 StatsUtils::StatsState state, int32_t uid = StatsUtils::INVALID_VALUE); 87 void UpdateTimer(std::shared_ptr<BatteryStatsEntity> entity, StatsUtils::StatsType statsType, 88 int64_t time, int32_t uid = StatsUtils::INVALID_VALUE); 89 void UpdateCameraTimer(StatsUtils::StatsState state, int32_t uid, const std::string& deviceId); 90 void UpdateScreenTimer(StatsUtils::StatsState state); 91 void UpdateBrightnessTimer(StatsUtils::StatsState state, int16_t level); 92 void UpdateCounter(std::shared_ptr<BatteryStatsEntity> entity, StatsUtils::StatsType statsType, 93 int64_t data, int32_t uid = StatsUtils::INVALID_VALUE); 94 void UpdateScreenStats(StatsUtils::StatsType statsType, StatsUtils::StatsState state, int16_t level); 95 void UpdateCameraStats(StatsUtils::StatsType statsType, StatsUtils::StatsState state, int32_t uid, 96 const std::string& deviceId); 97 void UpdatePhoneStats(StatsUtils::StatsType statsType, StatsUtils::StatsState state, int16_t level); 98 void UpdateConnectivityStats(StatsUtils::StatsType statsType, StatsUtils::StatsState state, int32_t uid); 99 void UpdateCommonStats(StatsUtils::StatsType statsType, StatsUtils::StatsState state, int32_t uid); 100 void CreatePartEntity(); 101 void CreateAppEntity(); 102 void SaveForHardware(Json::Value& root); 103 void SaveForSoftware(Json::Value& root); 104 void SaveForSoftwareCommon(Json::Value& root, int32_t uid); 105 void SaveForSoftwareConnectivity(Json::Value& root, int32_t uid); 106 void SaveForPower(Json::Value& root); 107 }; 108 } // namespace PowerMgr 109 } // namespace OHOS 110 #endif // BATTERY_STATS_CORE_H