• 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/gnss_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 
GnssEntity()29 GnssEntity::GnssEntity()
30 {
31     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_GNSS;
32 }
33 
GetActiveTimeMs(int32_t uid,StatsUtils::StatsType statsType,int16_t level)34 int64_t GnssEntity::GetActiveTimeMs(int32_t uid, StatsUtils::StatsType statsType, int16_t level)
35 {
36     int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
37     switch (statsType) {
38         case StatsUtils::STATS_TYPE_GNSS_ON: {
39             auto iter = gnssTimerMap_.find(uid);
40             if (iter != gnssTimerMap_.end()) {
41                 activeTimeMs = iter->second->GetRunningTimeMs();
42                 STATS_HILOGD(COMP_SVC, "Get gnss on time: %{public}" PRId64 "ms for uid: %{public}d", activeTimeMs,
43                     uid);
44                 break;
45             }
46             STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d, return 0", uid);
47             break;
48         }
49         default:
50             break;
51     }
52     return activeTimeMs;
53 }
54 
Calculate(int32_t uid)55 void GnssEntity::Calculate(int32_t uid)
56 {
57     auto gnssOnAverageMa = g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_GNSS_ON);
58     auto gnssOnTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_GNSS_ON);
59     auto gnssOnPowerMah = gnssOnAverageMa * gnssOnTimeMs / StatsUtils::MS_IN_HOUR;
60     auto iter = gnssPowerMap_.find(uid);
61     if (iter != gnssPowerMap_.end()) {
62         STATS_HILOGD(COMP_SVC, "Update gnss on power consumption: %{public}lfmAh for uid: %{public}d",
63             gnssOnAverageMa, uid);
64         iter->second = gnssOnPowerMah;
65     } else {
66         STATS_HILOGD(COMP_SVC, "Create gnss on power consumption: %{public}lfmAh for uid: %{public}d",
67             gnssOnAverageMa, uid);
68         gnssPowerMap_.insert(std::pair<int32_t, double>(uid, gnssOnPowerMah));
69     }
70 }
71 
GetEntityPowerMah(int32_t uidOrUserId)72 double GnssEntity::GetEntityPowerMah(int32_t uidOrUserId)
73 {
74     double power = StatsUtils::DEFAULT_VALUE;
75     auto iter = gnssPowerMap_.find(uidOrUserId);
76     if (iter != gnssPowerMap_.end()) {
77         power = iter->second;
78         STATS_HILOGD(COMP_SVC, "Get app gnss power consumption: %{public}lfmAh for uid: %{public}d",
79             power, uidOrUserId);
80     } else {
81         STATS_HILOGD(COMP_SVC,
82             "No app gnss power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
83     }
84     return power;
85 }
86 
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)87 double GnssEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
88 {
89     double power = StatsUtils::DEFAULT_VALUE;
90     if (statsType == StatsUtils::STATS_TYPE_GNSS_ON) {
91         auto gnssOnIter = gnssPowerMap_.find(uid);
92         if (gnssOnIter != gnssPowerMap_.end()) {
93             power = gnssOnIter->second;
94             STATS_HILOGD(COMP_SVC, "Get gnss on power consumption: %{public}lfmAh for uid: %{public}d",
95                 power, uid);
96         } else {
97             STATS_HILOGD(COMP_SVC,
98                 "No gnss on power consumption related to uid: %{public}d was found, return 0", uid);
99         }
100     }
101     return power;
102 }
103 
GetOrCreateTimer(int32_t uid,StatsUtils::StatsType statsType,int16_t level)104 std::shared_ptr<StatsHelper::ActiveTimer> GnssEntity::GetOrCreateTimer(int32_t uid, StatsUtils::StatsType statsType,
105     int16_t level)
106 {
107     std::shared_ptr<StatsHelper::ActiveTimer> timer = nullptr;
108     switch (statsType) {
109         case StatsUtils::STATS_TYPE_GNSS_ON: {
110             auto gnssOnIter = gnssTimerMap_.find(uid);
111             if (gnssOnIter != gnssTimerMap_.end()) {
112                 STATS_HILOGD(COMP_SVC, "Get gnss on timer for uid: %{public}d", uid);
113                 timer = gnssOnIter->second;
114                 break;
115             }
116             STATS_HILOGD(COMP_SVC, "Create gnss on timer for uid: %{public}d", uid);
117             std::shared_ptr<StatsHelper::ActiveTimer> gnssTimer = std::make_shared<StatsHelper::ActiveTimer>();
118             gnssTimerMap_.insert(std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, gnssTimer));
119             timer = gnssTimer;
120             break;
121         }
122         default:
123             STATS_HILOGW(COMP_SVC, "Create active timer failed");
124             break;
125     }
126     return timer;
127 }
128 
Reset()129 void GnssEntity::Reset()
130 {
131     // Reset app Gnss on total power consumption
132     for (auto& iter : gnssPowerMap_) {
133         iter.second = StatsUtils::DEFAULT_VALUE;
134     }
135 
136     // Reset Gnss on timer
137     for (auto& iter : gnssTimerMap_) {
138         if (iter.second) {
139             iter.second->Reset();
140         }
141     }
142 }
143 } // namespace PowerMgr
144 } // namespace OHOS