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