• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 long StatsHelper::latestUnplugBootTimeMs_ = StatsUtils::DEFAULT_VALUE;
24 long StatsHelper::latestUnplugUpTimeMs_ = StatsUtils::DEFAULT_VALUE;
25 long StatsHelper::onBatteryBootTimeMs_ = StatsUtils::DEFAULT_VALUE;
26 long StatsHelper::onBatteryUpTimeMs_ = StatsUtils::DEFAULT_VALUE;
27 bool StatsHelper::onBattery_ = false;
28 bool StatsHelper::screenOff_ = false;
29 
GetBootTimeMs()30 long StatsHelper::GetBootTimeMs()
31 {
32     STATS_HILOGI(STATS_MODULE_SERVICE, "Enter");
33     long bootTimeMs = StatsUtils::DEFAULT_VALUE;
34     struct timespec rawBootTime;
35     int errCode = clock_gettime(CLOCK_BOOTTIME, &rawBootTime);
36     if (errCode != 0) {
37         STATS_HILOGE(STATS_MODULE_SERVICE, "Get boot time failed, return default time.");
38     } else {
39         bootTimeMs =
40             (long) (rawBootTime.tv_sec * StatsUtils::MS_IN_SECOND + rawBootTime.tv_nsec / StatsUtils::NS_IN_MS);
41         STATS_HILOGI(STATS_MODULE_SERVICE, "Got boot time: %{public}ld", bootTimeMs);
42     }
43     return bootTimeMs;
44 }
45 
GetUpTimeMs()46 long StatsHelper::GetUpTimeMs()
47 {
48     STATS_HILOGI(STATS_MODULE_SERVICE, "Enter");
49     long upTimeMs = StatsUtils::DEFAULT_VALUE;
50     struct timespec rawUpTime;
51     int errCode = clock_gettime(CLOCK_MONOTONIC, &rawUpTime);
52     if (errCode != 0) {
53         STATS_HILOGE(STATS_MODULE_SERVICE, "Get up time failed, return default time.");
54     } else {
55         upTimeMs = (long) (rawUpTime.tv_sec * StatsUtils::MS_IN_SECOND + rawUpTime.tv_nsec / StatsUtils::NS_IN_MS);
56         STATS_HILOGI(STATS_MODULE_SERVICE, "Got up time: %{public}ld", upTimeMs);
57     }
58     return upTimeMs;
59 }
60 
SetOnBattery(bool onBattery)61 void StatsHelper::SetOnBattery(bool onBattery)
62 {
63     STATS_HILOGD(STATS_MODULE_SERVICE, "Enter");
64     if (onBattery_ != onBattery) {
65         onBattery_ = onBattery;
66         // when onBattery is ture, status is unplugin.
67         long currentBootTimeMs = GetBootTimeMs();
68         long currentUpTimeMs = GetUpTimeMs();
69         if (onBattery) {
70             STATS_HILOGD(STATS_MODULE_SERVICE, "Power supply is disconnected");
71             latestUnplugBootTimeMs_ = currentBootTimeMs;
72             latestUnplugUpTimeMs_ = currentUpTimeMs;
73         } else {
74             STATS_HILOGD(STATS_MODULE_SERVICE, "Power supply is connected");
75             onBatteryBootTimeMs_ += currentBootTimeMs - latestUnplugBootTimeMs_;
76             onBatteryUpTimeMs_ += currentUpTimeMs - latestUnplugUpTimeMs_;
77         }
78     }
79     STATS_HILOGD(STATS_MODULE_SERVICE, "Exit");
80 }
81 
SetScreenOff(bool screenOff)82 void StatsHelper::SetScreenOff(bool screenOff)
83 {
84     STATS_HILOGD(STATS_MODULE_SERVICE, "Enter");
85     if (screenOff_ != screenOff) {
86         screenOff_ = screenOff;
87         STATS_HILOGD(STATS_MODULE_SERVICE, "Update screen off state: %{public}d", screenOff);
88     }
89     STATS_HILOGD(STATS_MODULE_SERVICE, "Exit");
90 }
91 
IsOnBattery()92 bool StatsHelper::IsOnBattery()
93 {
94     STATS_HILOGD(STATS_MODULE_SERVICE, "onBattery_ = %{public}d", onBattery_);
95     return onBattery_;
96 }
97 
IsOnBatteryScreenOff()98 bool StatsHelper::IsOnBatteryScreenOff()
99 {
100     return onBattery_ && screenOff_;
101 }
102 
GetOnBatteryBootTimeMs()103 long StatsHelper::GetOnBatteryBootTimeMs()
104 {
105     STATS_HILOGD(STATS_MODULE_SERVICE, "Enter");
106     long onBatteryBootTimeMs = onBatteryBootTimeMs_;
107     long currentBootTimeMs = GetBootTimeMs();
108     STATS_HILOGI(STATS_MODULE_SERVICE, "onBatteryBootTimeMs: %{public}ld", onBatteryBootTimeMs);
109     STATS_HILOGI(STATS_MODULE_SERVICE, "currentBootTimeMs: %{public}ld", currentBootTimeMs);
110     STATS_HILOGI(STATS_MODULE_SERVICE, "latestUnplugBootTimeMs_: %{public}ld", latestUnplugBootTimeMs_);
111     if (IsOnBattery()) {
112         onBatteryBootTimeMs += currentBootTimeMs - latestUnplugBootTimeMs_;
113     }
114     STATS_HILOGI(STATS_MODULE_SERVICE, "Got on battery boot time: %{public}ld", onBatteryBootTimeMs);
115     return onBatteryBootTimeMs;
116 }
117 
GetOnBatteryUpTimeMs()118 long StatsHelper::GetOnBatteryUpTimeMs()
119 {
120     STATS_HILOGD(STATS_MODULE_SERVICE, "Enter");
121     long onBatteryUpTimeMs = onBatteryUpTimeMs_;
122     long currentUpTimeMs = GetUpTimeMs();
123     if (IsOnBattery()) {
124         onBatteryUpTimeMs += currentUpTimeMs - latestUnplugUpTimeMs_;
125     }
126     STATS_HILOGI(STATS_MODULE_SERVICE, "Got on battery up time: %{public}ld", onBatteryUpTimeMs);
127     return onBatteryUpTimeMs;
128 }
129 } // namespace PowerMgr
130 } // namespace OHOS