• 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 
16 #include "timer_info.h"
17 
18 #include <cinttypes>
19 
20 #include "time_hilog.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
operator ==(const TimerInfo & other) const24 bool TimerInfo::operator==(const TimerInfo &other) const
25 {
26     return this->id == other.id;
27 }
28 
Matches(const std::string & packageName) const29 bool TimerInfo::Matches(const std::string &packageName) const
30 {
31     return false;
32 }
33 
TimerInfo(std::string _name,uint64_t _id,int _type,std::chrono::milliseconds _when,std::chrono::steady_clock::time_point _whenElapsed,std::chrono::milliseconds _windowLength,std::chrono::steady_clock::time_point _maxWhen,std::chrono::milliseconds _interval,std::function<int32_t (const uint64_t)> _callback,std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> _wantAgent,uint32_t _flags,bool _autoRestore,int _uid,int _pid,const std::string & _bundleName)34 TimerInfo::TimerInfo(std::string _name, uint64_t _id, int _type,
35                      std::chrono::milliseconds _when,
36                      std::chrono::steady_clock::time_point _whenElapsed,
37                      std::chrono::milliseconds _windowLength,
38                      std::chrono::steady_clock::time_point _maxWhen,
39                      std::chrono::milliseconds _interval,
40                      std::function<int32_t (const uint64_t)> _callback,
41                      std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> _wantAgent,
42                      uint32_t _flags,
43                      bool _autoRestore,
44                      int _uid,
45                      int _pid,
46                      const std::string &_bundleName)
47     : name {_name},
48       id {_id},
49       type {_type},
50       origWhen {_when},
51       wakeup {_type == ITimerManager::ELAPSED_REALTIME_WAKEUP || _type == ITimerManager::RTC_WAKEUP},
52       autoRestore {_autoRestore},
53       callback {std::move(_callback)},
54       wantAgent {_wantAgent},
55       flags {_flags},
56       uid {_uid},
57       pid {_pid},
58       when {_when},
59       windowLength {_windowLength},
60       whenElapsed {_whenElapsed},
61       maxWhenElapsed {_maxWhen},
62       repeatInterval {_interval},
63       bundleName {_bundleName}
64 {
65     originWhenElapsed = _whenElapsed;
66     originMaxWhenElapsed = _maxWhen;
67 }
68 
69 /* Please make sure that the first param is current boottime */
UpdateWhenElapsedFromNow(std::chrono::steady_clock::time_point now,std::chrono::nanoseconds offset)70 bool TimerInfo::UpdateWhenElapsedFromNow(std::chrono::steady_clock::time_point now, std::chrono::nanoseconds offset)
71 {
72     TIME_HILOGD(TIME_MODULE_SERVICE, "Update whenElapsed, id=%{public}" PRId64 "", id);
73     auto oldWhenElapsed = whenElapsed;
74     whenElapsed = now + offset;
75     auto oldMaxWhenElapsed = maxWhenElapsed;
76     maxWhenElapsed = whenElapsed + windowLength;
77     std::chrono::milliseconds currentTime;
78     if (type == ITimerManager::RTC || type == ITimerManager::RTC_WAKEUP) {
79         currentTime =
80             std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
81     } else {
82         currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
83     }
84     auto offsetMill = std::chrono::duration_cast<std::chrono::milliseconds>(offset);
85     when = currentTime + offsetMill;
86     return (oldWhenElapsed != whenElapsed) || (oldMaxWhenElapsed != maxWhenElapsed);
87 }
88 
AdjustTimer(const std::chrono::steady_clock::time_point & now,const uint32_t interval)89 bool TimerInfo::AdjustTimer(const std::chrono::steady_clock::time_point &now, const uint32_t interval)
90 {
91     auto oldWhenElapsed = whenElapsed;
92     auto oldMaxWhenElapsed = maxWhenElapsed;
93     std::chrono::duration<int, std::ratio<1, HALF_SECEND>> halfIntervalSec(interval);
94     std::chrono::duration<int, std::ratio<1, 1>> intervalSec(interval);
95     auto oldTimeSec = std::chrono::duration_cast<std::chrono::seconds>(whenElapsed.time_since_epoch());
96     auto timeSec = ((oldTimeSec + halfIntervalSec) / intervalSec) * intervalSec;
97     whenElapsed = std::chrono::steady_clock::time_point(timeSec);
98     if (windowLength == std::chrono::milliseconds::zero()) {
99         maxWhenElapsed = whenElapsed;
100     } else {
101         auto oldMaxTimeSec = std::chrono::duration_cast<std::chrono::seconds>(maxWhenElapsed.time_since_epoch());
102         auto maxTimeSec = ((oldMaxTimeSec + halfIntervalSec) / intervalSec) * intervalSec;
103         maxWhenElapsed = std::chrono::steady_clock::time_point(maxTimeSec);
104     }
105     if (whenElapsed < now) {
106         whenElapsed += std::chrono::duration_cast<std::chrono::milliseconds>(intervalSec);
107     }
108     if (maxWhenElapsed < now) {
109         maxWhenElapsed += std::chrono::duration_cast<std::chrono::milliseconds>(intervalSec);
110     }
111     auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(
112         whenElapsed.time_since_epoch() - oldWhenElapsed.time_since_epoch());
113     when = when + delta;
114     return (oldWhenElapsed != whenElapsed) || (oldMaxWhenElapsed != maxWhenElapsed);
115 }
116 
RestoreAdjustTimer()117 bool TimerInfo::RestoreAdjustTimer()
118 {
119     auto oldWhenElapsed = whenElapsed;
120     auto oldMaxWhenElapsed = maxWhenElapsed;
121     whenElapsed = originWhenElapsed;
122     maxWhenElapsed = originMaxWhenElapsed;
123     auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(
124         whenElapsed.time_since_epoch() - oldWhenElapsed.time_since_epoch());
125     when = when + delta;
126     return (oldWhenElapsed != whenElapsed) || (oldMaxWhenElapsed != maxWhenElapsed);
127 }
128 } // MiscServices
129 } // OHOS