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 
30 namespace OHOS {
31 namespace PowerMgr {
32 class BatteryStatsService final : public SystemAbility, public BatteryStatsStub {
33     DECLARE_SYSTEM_ABILITY(BatteryStatsService);
34     DISALLOW_COPY_AND_MOVE(BatteryStatsService);
35 
36 public:
37     BatteryStatsService();
38     virtual ~BatteryStatsService();
39     virtual void OnStart() override;
40     virtual void OnStop() override;
41     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
42 
43     bool IsServiceReady() const;
44     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
45     BatteryStatsInfoList GetBatteryStats() override;
46     double GetAppStatsMah(const int32_t& uid) override;
47     double GetAppStatsPercent(const int32_t& uid) override;
48     double GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type) override;
49     double GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type) override;
50     uint64_t GetTotalTimeSecond(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE)
51         override;
52     uint64_t GetTotalDataBytes(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE)
53         override;
54     void Reset() override;
55     void SetOnBattery(bool isOnBattery) override;
56     std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override;
57     std::shared_ptr<BatteryStatsCore> GetBatteryStatsCore() const;
58     std::shared_ptr<BatteryStatsParser> GetBatteryStatsParser() const;
59     std::shared_ptr<BatteryStatsDetector> GetBatteryStatsDetector() const;
60     StatsError GetLastError() override;
61     static sptr<BatteryStatsService> GetInstance();
62     static void DestroyInstance();
63 
64 #ifndef STATS_SERVICE_UT_TEST
65 private:
66 #endif
67     static constexpr int32_t DEPENDENCY_CHECK_DELAY_MS = 2000;
68     bool Init();
69     std::shared_ptr<BatteryStatsCore> core_;
70     std::shared_ptr<BatteryStatsParser> parser_;
71     std::shared_ptr<BatteryStatsDetector> detector_;
72     std::shared_ptr<EventFwk::CommonEventSubscriber> subscriberPtr_;
73     std::shared_ptr<HiviewDFX::HiSysEventListener> listenerPtr_;
74     bool ready_ = false;
75     static std::atomic_bool isBootCompleted_;
76     std::mutex mutex_;
77     StatsError lastError_ = StatsError::ERR_OK;
78     bool SubscribeCommonEvent();
79     bool AddHiSysEventListener();
80     void RegisterBootCompletedCallback();
81     static sptr<BatteryStatsService> instance_;
82     static std::mutex singletonMutex_;
83 };
84 } // namespace PowerMgr
85 } // namespace OHOS
86 #endif // BATTERY_STATS_SERVICE_H