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