• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "entities/wifi_entity.h"
17 
18 #include <cinttypes>
19 
20 #include "battery_stats_service.h"
21 #include "stats_log.h"
22 
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26     auto g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
27 }
28 
WifiEntity()29 WifiEntity::WifiEntity()
30 {
31     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_WIFI;
32 }
33 
Calculate(int32_t uid)34 void WifiEntity::Calculate(int32_t uid)
35 {
36     // Calculate Wifi on power
37     auto wifiOnAverageMa = g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_WIFI_ON);
38     auto wifiOnTimeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_WIFI_ON);
39     auto wifiOnPowerMah = wifiOnAverageMa * wifiOnTimeMs / StatsUtils::MS_IN_HOUR;
40 
41     // Calculate Wifi scan power
42     auto wifiScanAverageMa = g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_WIFI_SCAN);
43     auto wifiScanCount = GetConsumptionCount(StatsUtils::STATS_TYPE_WIFI_SCAN);
44     auto wifiScanPowerMah = wifiScanAverageMa * wifiScanCount;
45 
46     wifiPowerMah_ = wifiOnPowerMah + wifiScanPowerMah;
47     totalPowerMah_ += wifiPowerMah_;
48     std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
49     statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_WIFI);
50     statsInfo->SetPower(wifiPowerMah_);
51     statsInfoList_.push_back(statsInfo);
52     STATS_HILOGD(COMP_SVC, "Calculate wifi power consumption: %{public}lfmAh", wifiPowerMah_);
53 }
54 
GetActiveTimeMs(StatsUtils::StatsType statsType,int16_t level)55 int64_t WifiEntity::GetActiveTimeMs(StatsUtils::StatsType statsType, int16_t level)
56 {
57     int64_t time = StatsUtils::DEFAULT_VALUE;
58     if (statsType != StatsUtils::STATS_TYPE_WIFI_ON) {
59         return time;
60     }
61     if (wifiOnTimer_) {
62         time = wifiOnTimer_->GetRunningTimeMs();
63         STATS_HILOGD(COMP_SVC, "Get wifi on time: %{public}" PRId64 "ms", time);
64         return time;
65     }
66     STATS_HILOGD(COMP_SVC, "Wifi has not been turned on yet, return 0");
67     return time;
68 }
69 
GetEntityPowerMah(int32_t uidOrUserId)70 double WifiEntity::GetEntityPowerMah(int32_t uidOrUserId)
71 {
72     return wifiPowerMah_;
73 }
74 
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)75 double WifiEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
76 {
77     return wifiPowerMah_;
78 }
79 
GetConsumptionCount(StatsUtils::StatsType statsType,int32_t uid)80 int64_t WifiEntity::GetConsumptionCount(StatsUtils::StatsType statsType, int32_t uid)
81 {
82     int64_t count = StatsUtils::DEFAULT_VALUE;
83     if (statsType != StatsUtils::STATS_TYPE_WIFI_SCAN) {
84         return count;
85     }
86     if (wifiScanCounter_) {
87         count = wifiScanCounter_->GetCount();
88         STATS_HILOGD(COMP_SVC, "Get wifi scan count: %{public}" PRId64 "", count);
89         return count;
90     }
91     STATS_HILOGD(COMP_SVC, "Wifi scan has not been triggered yet, return 0");
92     return count;
93 }
94 
GetOrCreateTimer(StatsUtils::StatsType statsType,int16_t level)95 std::shared_ptr<StatsHelper::ActiveTimer> WifiEntity::GetOrCreateTimer(StatsUtils::StatsType statsType, int16_t level)
96 {
97     if (statsType != StatsUtils::STATS_TYPE_WIFI_ON) {
98         return nullptr;
99     }
100 
101     if (wifiOnTimer_ != nullptr) {
102         STATS_HILOGD(COMP_SVC, "Get wifi on timer");
103         return wifiOnTimer_;
104     }
105     wifiOnTimer_ = std::make_shared<StatsHelper::ActiveTimer>();
106     return wifiOnTimer_;
107 }
108 
GetOrCreateCounter(StatsUtils::StatsType statsType,int32_t uid)109 std::shared_ptr<StatsHelper::Counter> WifiEntity::GetOrCreateCounter(StatsUtils::StatsType statsType, int32_t uid)
110 {
111     if (statsType != StatsUtils::STATS_TYPE_WIFI_SCAN) {
112         return nullptr;
113     }
114     if (wifiScanCounter_ != nullptr) {
115         STATS_HILOGD(COMP_SVC, "Get wifi scan counter");
116         return wifiScanCounter_;
117     }
118     STATS_HILOGD(COMP_SVC, "Create wifi scan counter");
119     wifiScanCounter_ = std::make_shared<StatsHelper::Counter>();
120     return wifiScanCounter_;
121 }
122 
Reset()123 void WifiEntity::Reset()
124 {
125     // Reset Wifi power consumption
126     wifiPowerMah_ = StatsUtils::DEFAULT_VALUE;
127 
128     // Reset Wifi on timer
129     if (wifiOnTimer_) {
130         wifiOnTimer_->Reset();
131     }
132 
133     // Reset Wifi scan counter
134     if (wifiScanCounter_) {
135         wifiScanCounter_->Reset();
136     }
137 }
138 
DumpInfo(std::string & result,int32_t uid)139 void WifiEntity::DumpInfo(std::string& result, int32_t uid)
140 {
141     int64_t time = GetActiveTimeMs(StatsUtils::STATS_TYPE_WIFI_ON);
142     int64_t conut = GetConsumptionCount(StatsUtils::STATS_TYPE_WIFI_SCAN);
143     result.append("Wifi dump:\n")
144         .append("Wifi on time: ")
145         .append(ToString(time))
146         .append("ms")
147         .append("\n")
148         .append("Wifi scan count: ")
149         .append(ToString(conut))
150         .append("\n");
151 }
152 } // namespace PowerMgr
153 } // namespace OHOS
154