• 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_SERVICE_H
17 #define BATTERY_STATS_SERVICE_H
18 
19 #include <atomic>
20 #include "common_event_subscriber.h"
21 #include "hisysevent_listener.h"
22 #include "system_ability.h"
23 
24 #include "battery_stats_core.h"
25 #include "battery_stats_detector.h"
26 #include "battery_stats_info.h"
27 #include "battery_stats_parser.h"
28 #include "battery_stats_stub.h"
29 #include "delayed_stats_sp_singleton.h"
30 
31 namespace OHOS {
32 namespace PowerMgr {
33 class BatteryStatsService final : public SystemAbility, public BatteryStatsStub {
34     DECLARE_SYSTEM_ABILITY(BatteryStatsService)
35     DECLARE_DELAYED_STATS_SP_SINGLETON(BatteryStatsService);
36 
37 public:
38     virtual void OnStart() override;
39     virtual void OnStop() override;
40     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
41 
42     bool IsServiceReady() const;
43     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
44     BatteryStatsInfoList GetBatteryStats() override;
45     double GetAppStatsMah(const int32_t& uid) override;
46     double GetAppStatsPercent(const int32_t& uid) override;
47     double GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type) override;
48     double GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type) override;
49     uint64_t GetTotalTimeSecond(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE)
50         override;
51     uint64_t GetTotalDataBytes(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE)
52         override;
53     void Reset() override;
54     void SetOnBattery(bool isOnBattery) override;
55     std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override;
56     std::shared_ptr<BatteryStatsCore> GetBatteryStatsCore() const;
57     std::shared_ptr<BatteryStatsParser> GetBatteryStatsParser() const;
58     std::shared_ptr<BatteryStatsDetector> GetBatteryStatsDetector() const;
59     StatsError GetLastError() override;
60 
61 #ifndef STATS_SERVICE_UT_TEST
62 private:
63 #endif
64     static constexpr int32_t DEPENDENCY_CHECK_DELAY_MS = 2000;
65     bool Init();
66     std::shared_ptr<BatteryStatsCore> core_;
67     std::shared_ptr<BatteryStatsParser> parser_;
68     std::shared_ptr<BatteryStatsDetector> detector_;
69     std::shared_ptr<EventFwk::CommonEventSubscriber> subscriberPtr_;
70     std::shared_ptr<HiviewDFX::HiSysEventListener> listenerPtr_;
71     bool ready_ = false;
72     static std::atomic_bool isBootCompleted_;
73     std::mutex mutex_;
74     StatsError lastError_ = StatsError::ERR_OK;
75     bool SubscribeCommonEvent();
76     bool AddHiSysEventListener();
77     void RegisterBootCompletedCallback();
78 };
79 } // namespace PowerMgr
80 } // namespace OHOS
81 #endif // BATTERY_STATS_SERVICE_H