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