• 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 #include "stats_helper.h"
16 
17 #include <ctime>
18 
19 #include "battery_stats_info.h"
20 
21 namespace OHOS {
22 namespace PowerMgr {
23 int64_t StatsHelper::latestUnplugBootTimeMs_ = StatsUtils::DEFAULT_VALUE;
24 int64_t StatsHelper::latestUnplugUpTimeMs_ = StatsUtils::DEFAULT_VALUE;
25 int64_t StatsHelper::onBatteryBootTimeMs_ = StatsUtils::DEFAULT_VALUE;
26 int64_t StatsHelper::onBatteryUpTimeMs_ = StatsUtils::DEFAULT_VALUE;
27 bool StatsHelper::onBattery_ = false;
28 bool StatsHelper::screenOff_ = false;
29 
GetBootTimeMs()30 int64_t StatsHelper::GetBootTimeMs()
31 {
32     int64_t bootTimeMs = StatsUtils::DEFAULT_VALUE;
33     struct timespec rawBootTime;
34     int errCode = clock_gettime(CLOCK_BOOTTIME, &rawBootTime);
35     if (errCode != 0) {
36         STATS_HILOGE(COMP_SVC, "Get boot time failed, return default time");
37     } else {
38         bootTimeMs = static_cast<int64_t>(rawBootTime.tv_sec * StatsUtils::MS_IN_SECOND +
39             rawBootTime.tv_nsec / StatsUtils::NS_IN_MS);
40         STATS_HILOGD(COMP_SVC, "Get boot time: %{public}" PRId64 "", bootTimeMs);
41     }
42     return bootTimeMs;
43 }
44 
GetUpTimeMs()45 int64_t StatsHelper::GetUpTimeMs()
46 {
47     int64_t upTimeMs = StatsUtils::DEFAULT_VALUE;
48     struct timespec rawUpTime;
49     int errCode = clock_gettime(CLOCK_MONOTONIC, &rawUpTime);
50     if (errCode != 0) {
51         STATS_HILOGE(COMP_SVC, "Get up time failed, return default time");
52     } else {
53         upTimeMs = static_cast<int64_t>(rawUpTime.tv_sec * StatsUtils::MS_IN_SECOND +
54             rawUpTime.tv_nsec / StatsUtils::NS_IN_MS);
55         STATS_HILOGD(COMP_SVC, "Get up time: %{public}" PRId64 "", upTimeMs);
56     }
57     return upTimeMs;
58 }
59 
SetOnBattery(bool onBattery)60 void StatsHelper::SetOnBattery(bool onBattery)
61 {
62     if (onBattery_ != onBattery) {
63         onBattery_ = onBattery;
64         // when onBattery is ture, status is unplugin.
65         int64_t currentBootTimeMs = GetBootTimeMs();
66         int64_t currentUpTimeMs = GetUpTimeMs();
67         if (onBattery) {
68             latestUnplugBootTimeMs_ = currentBootTimeMs;
69             latestUnplugUpTimeMs_ = currentUpTimeMs;
70         } else {
71             onBatteryBootTimeMs_ += currentBootTimeMs - latestUnplugBootTimeMs_;
72             onBatteryUpTimeMs_ += currentUpTimeMs - latestUnplugUpTimeMs_;
73         }
74         STATS_HILOGI(COMP_SVC, "Update battery state:  %{public}d", onBattery);
75     }
76 }
77 
SetScreenOff(bool screenOff)78 void StatsHelper::SetScreenOff(bool screenOff)
79 {
80     if (screenOff_ != screenOff) {
81         screenOff_ = screenOff;
82         STATS_HILOGD(COMP_SVC, "Update screen off state: %{public}d", screenOff);
83     }
84 }
85 
IsOnBattery()86 bool StatsHelper::IsOnBattery()
87 {
88     return onBattery_;
89 }
90 
IsOnBatteryScreenOff()91 bool StatsHelper::IsOnBatteryScreenOff()
92 {
93     return onBattery_ && screenOff_;
94 }
95 
GetOnBatteryBootTimeMs()96 int64_t StatsHelper::GetOnBatteryBootTimeMs()
97 {
98     int64_t onBatteryBootTimeMs = onBatteryBootTimeMs_;
99     int64_t currentBootTimeMs = GetBootTimeMs();
100     if (IsOnBattery()) {
101         onBatteryBootTimeMs += currentBootTimeMs - latestUnplugBootTimeMs_;
102     }
103     STATS_HILOGD(COMP_SVC, "Get on battery boot time: %{public}" PRId64 ", currentBootTimeMs: %{public}" PRId64 "," \
104         "latestUnplugBootTimeMs_: %{public}" PRId64 "",
105         onBatteryBootTimeMs, currentBootTimeMs, latestUnplugBootTimeMs_);
106     return onBatteryBootTimeMs;
107 }
108 
GetOnBatteryUpTimeMs()109 int64_t StatsHelper::GetOnBatteryUpTimeMs()
110 {
111     int64_t onBatteryUpTimeMs = onBatteryUpTimeMs_;
112     int64_t currentUpTimeMs = GetUpTimeMs();
113     if (IsOnBattery()) {
114         onBatteryUpTimeMs += currentUpTimeMs - latestUnplugUpTimeMs_;
115     }
116     STATS_HILOGD(COMP_SVC, "Get on battery up time: %{public}" PRId64 "", onBatteryUpTimeMs);
117     return onBatteryUpTimeMs;
118 }
119 } // namespace PowerMgr
120 } // namespace OHOS