• 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 #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     switch (statsType) {
59         case StatsUtils::STATS_TYPE_WIFI_ON: {
60             if (wifiOnTimer_) {
61                 time = wifiOnTimer_->GetRunningTimeMs();
62                 STATS_HILOGD(COMP_SVC, "Get wifi on time: %{public}" PRId64 "ms", time);
63                 break;
64             }
65             STATS_HILOGD(COMP_SVC, "Wifi has not been turned on yet, return 0");
66             break;
67         }
68         default:
69             break;
70     }
71     return time;
72 }
73 
GetEntityPowerMah(int32_t uidOrUserId)74 double WifiEntity::GetEntityPowerMah(int32_t uidOrUserId)
75 {
76     return wifiPowerMah_;
77 }
78 
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)79 double WifiEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
80 {
81     return wifiPowerMah_;
82 }
83 
GetConsumptionCount(StatsUtils::StatsType statsType,int32_t uid)84 int64_t WifiEntity::GetConsumptionCount(StatsUtils::StatsType statsType, int32_t uid)
85 {
86     int64_t count = StatsUtils::DEFAULT_VALUE;
87     switch (statsType) {
88         case StatsUtils::STATS_TYPE_WIFI_SCAN: {
89             if (wifiScanCounter_) {
90                 count = wifiScanCounter_->GetCount();
91                 STATS_HILOGD(COMP_SVC, "Get wifi scan count: %{public}" PRId64 "", count);
92                 break;
93             }
94             STATS_HILOGD(COMP_SVC, "Wifi scan has not been triggered yet, return 0");
95             break;
96         }
97         default:
98             break;
99     }
100     return count;
101 }
102 
GetOrCreateTimer(StatsUtils::StatsType statsType,int16_t level)103 std::shared_ptr<StatsHelper::ActiveTimer> WifiEntity::GetOrCreateTimer(StatsUtils::StatsType statsType, int16_t level)
104 {
105     std::shared_ptr<StatsHelper::ActiveTimer> timer = nullptr;
106     switch (statsType) {
107         case StatsUtils::STATS_TYPE_WIFI_ON: {
108             if (wifiOnTimer_ != nullptr) {
109                 STATS_HILOGD(COMP_SVC, "Get wifi on timer");
110                 timer = wifiOnTimer_;
111                 break;
112             }
113             STATS_HILOGD(COMP_SVC, "Create wifi on timer");
114             wifiOnTimer_ = std::make_shared<StatsHelper::ActiveTimer>();
115             timer = wifiOnTimer_;
116             break;
117         }
118         default:
119             STATS_HILOGW(COMP_SVC, "Create active timer failed");
120             break;
121     }
122     return timer;
123 }
124 
GetOrCreateCounter(StatsUtils::StatsType statsType,int32_t uid)125 std::shared_ptr<StatsHelper::Counter> WifiEntity::GetOrCreateCounter(StatsUtils::StatsType statsType, int32_t uid)
126 {
127     std::shared_ptr<StatsHelper::Counter> counter = nullptr;
128     switch (statsType) {
129         case StatsUtils::STATS_TYPE_WIFI_SCAN: {
130             if (wifiScanCounter_ != nullptr) {
131                 STATS_HILOGD(COMP_SVC, "Get wifi scan counter");
132                 counter = wifiScanCounter_;
133                 break;
134             }
135             STATS_HILOGD(COMP_SVC, "Create wifi scan counter");
136             wifiScanCounter_ = std::make_shared<StatsHelper::Counter>();
137             counter = wifiScanCounter_;
138             break;
139         }
140         default:
141             STATS_HILOGW(COMP_SVC, "Create wifi scan counter failed");
142             break;
143     }
144     return counter;
145 }
146 
Reset()147 void WifiEntity::Reset()
148 {
149     // Reset Wifi power consumption
150     wifiPowerMah_ = StatsUtils::DEFAULT_VALUE;
151 
152     // Reset Wifi on timer
153     if (wifiOnTimer_) {
154         wifiOnTimer_->Reset();
155     }
156 
157     // Reset Wifi scan counter
158     if (wifiScanCounter_) {
159         wifiScanCounter_->Reset();
160     }
161 }
162 
DumpInfo(std::string & result,int32_t uid)163 void WifiEntity::DumpInfo(std::string& result, int32_t uid)
164 {
165     int64_t time = GetActiveTimeMs(StatsUtils::STATS_TYPE_WIFI_ON);
166     int64_t conut = GetConsumptionCount(StatsUtils::STATS_TYPE_WIFI_SCAN);
167     result.append("Wifi dump:\n")
168         .append("Wifi on time: ")
169         .append(ToString(time))
170         .append("ms")
171         .append("\n")
172         .append("Wifi scan count: ")
173         .append(ToString(conut))
174         .append("\n");
175 }
176 } // namespace PowerMgr
177 } // namespace OHOS
178