• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements mechanism to track device's uptime.
32  */
33 
34 #include "uptime.hpp"
35 
36 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
37 
38 #include "common/code_utils.hpp"
39 #include "common/debug.hpp"
40 #include "common/instance.hpp"
41 #include "common/locator_getters.hpp"
42 
43 namespace ot {
44 
Uptime(Instance & aInstance)45 Uptime::Uptime(Instance &aInstance)
46     : InstanceLocator(aInstance)
47     , mStartTime(TimerMilli::GetNow())
48     , mOverflowCount(0)
49     , mTimer(aInstance, HandleTimer)
50 {
51     mTimer.FireAt(mStartTime + kTimerInterval);
52 }
53 
GetUptime(void) const54 uint64_t Uptime::GetUptime(void) const
55 {
56     TimeMilli now           = TimerMilli::GetNow();
57     uint32_t  overflowCount = mOverflowCount;
58 
59     // `mTimer` is scheduled with duration `kTimerInterval = (1 << 30)`
60     // so it takes four timer fires for us to go over the entire 32-bit
61     // range and get back to `mStartTime` (at which point we increment
62     // the `mOverflowCount` in `HandleTimer()`).
63     //
64     // Here, we handle the corner case where we may have reached or
65     // passed beyond the `mStartTime` but the `mTimer` is not yet
66     // handled. In this case we increment `overflowCount` (which is
67     // our copy of the current `mOverflowCount` value). We use the
68     // check `(mTimer.GetFireTime() == mStartTime)` to determine if
69     // we are in the last of the four timer fires before overflow.
70 
71     if ((mTimer.GetFireTime() == mStartTime) && (now >= mStartTime))
72     {
73         overflowCount++;
74     }
75 
76     // The uptime is returned as a `uint64_t`. The `overflowCount`
77     // gives the higher 32 bits, and `(now - mStartTime)` gives the
78     // lower 32 bits (which is always correct even under the corner
79     // case where the `HandleTimer()` is not yet handled).
80 
81     return (static_cast<uint64_t>(overflowCount) << 32) + (now - mStartTime);
82 }
83 
GetUptime(char * aBuffer,uint16_t aSize) const84 void Uptime::GetUptime(char *aBuffer, uint16_t aSize) const
85 {
86     StringWriter writer(aBuffer, aSize);
87 
88     UptimeToString(GetUptime(), writer);
89 }
90 
HandleTimer(Timer & aTimer)91 void Uptime::HandleTimer(Timer &aTimer)
92 {
93     aTimer.Get<Uptime>().HandleTimer();
94 }
95 
HandleTimer(void)96 void Uptime::HandleTimer(void)
97 {
98     if (mTimer.GetFireTime() == mStartTime)
99     {
100         mOverflowCount++;
101     }
102 
103     mTimer.FireAt(mTimer.GetFireTime() + kTimerInterval);
104 }
105 
DivideAndGetRemainder(uint32_t & aDividend,uint32_t aDivisor)106 static uint32_t DivideAndGetRemainder(uint32_t &aDividend, uint32_t aDivisor)
107 {
108     // Returns the quotient of division `aDividend / aDivisor` and updates
109     // `aDividend` to returns the remainder
110 
111     uint32_t quotient = aDividend / aDivisor;
112 
113     aDividend -= quotient * aDivisor;
114 
115     return quotient;
116 }
117 
UptimeToString(uint64_t aUptime,StringWriter & aWriter)118 void Uptime::UptimeToString(uint64_t aUptime, StringWriter &aWriter)
119 {
120     uint64_t days = aUptime / Time::kOneDayInMsec;
121     uint32_t remainder;
122     uint32_t hours;
123     uint32_t minutes;
124     uint32_t seconds;
125 
126     if (days > 0)
127     {
128         aWriter.Append("%lud.", days);
129         aUptime -= days * Time::kOneDayInMsec;
130     }
131 
132     remainder = static_cast<uint32_t>(aUptime);
133     hours     = DivideAndGetRemainder(remainder, Time::kOneHourInMsec);
134     minutes   = DivideAndGetRemainder(remainder, Time::kOneMinuteInMsec);
135     seconds   = DivideAndGetRemainder(remainder, Time::kOneSecondInMsec);
136 
137     aWriter.Append("%02u:%02u:%02u.%03u", hours, minutes, seconds, remainder);
138 }
139 
140 } // namespace ot
141 
142 #endif // OPENTHREAD_CONFIG_UPTIME_ENABLE
143