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 "instance/instance.hpp"
39
40 namespace ot {
41
Uptime(Instance & aInstance)42 Uptime::Uptime(Instance &aInstance)
43 : InstanceLocator(aInstance)
44 , mStartTime(TimerMilli::GetNow())
45 , mOverflowCount(0)
46 , mTimer(aInstance)
47 {
48 mTimer.FireAt(mStartTime + kTimerInterval);
49 }
50
GetUptime(void) const51 uint64_t Uptime::GetUptime(void) const
52 {
53 TimeMilli now = TimerMilli::GetNow();
54 uint32_t overflowCount = mOverflowCount;
55
56 // `mTimer` is scheduled with duration `kTimerInterval = (1 << 30)`
57 // so it takes four timer fires for us to go over the entire 32-bit
58 // range and get back to `mStartTime` (at which point we increment
59 // the `mOverflowCount` in `HandleTimer()`).
60 //
61 // Here, we handle the corner case where we may have reached or
62 // passed beyond the `mStartTime` but the `mTimer` is not yet
63 // handled. In this case we increment `overflowCount` (which is
64 // our copy of the current `mOverflowCount` value). We use the
65 // check `(mTimer.GetFireTime() == mStartTime)` to determine if
66 // we are in the last of the four timer fires before overflow.
67
68 if ((mTimer.GetFireTime() == mStartTime) && (now >= mStartTime))
69 {
70 overflowCount++;
71 }
72
73 // The uptime is returned as a `uint64_t`. The `overflowCount`
74 // gives the higher 32 bits, and `(now - mStartTime)` gives the
75 // lower 32 bits (which is always correct even under the corner
76 // case where the `HandleTimer()` is not yet handled).
77
78 return (static_cast<uint64_t>(overflowCount) << 32) + (now - mStartTime);
79 }
80
GetUptime(char * aBuffer,uint16_t aSize) const81 void Uptime::GetUptime(char *aBuffer, uint16_t aSize) const
82 {
83 StringWriter writer(aBuffer, aSize);
84
85 UptimeToString(GetUptime(), writer, /* aIncludeMsec */ true);
86 }
87
HandleTimer(void)88 void Uptime::HandleTimer(void)
89 {
90 if (mTimer.GetFireTime() == mStartTime)
91 {
92 mOverflowCount++;
93 }
94
95 mTimer.FireAt(mTimer.GetFireTime() + kTimerInterval);
96 }
97
DivideAndGetRemainder(uint32_t & aDividend,uint32_t aDivisor)98 static uint16_t DivideAndGetRemainder(uint32_t &aDividend, uint32_t aDivisor)
99 {
100 // Returns the quotient of division `aDividend / aDivisor` and updates
101 // `aDividend` to returns the remainder
102
103 uint32_t quotient = aDividend / aDivisor;
104
105 aDividend -= quotient * aDivisor;
106
107 return static_cast<uint16_t>(quotient);
108 }
109
UptimeToString(uint64_t aUptime,StringWriter & aWriter,bool aIncludeMsec)110 void Uptime::UptimeToString(uint64_t aUptime, StringWriter &aWriter, bool aIncludeMsec)
111 {
112 uint64_t days = aUptime / Time::kOneDayInMsec;
113 uint32_t remainder;
114 uint16_t hours;
115 uint16_t minutes;
116 uint16_t seconds;
117
118 if (days > 0)
119 {
120 aWriter.Append("%lud.", static_cast<unsigned long>(days));
121 aUptime -= days * Time::kOneDayInMsec;
122 }
123
124 remainder = static_cast<uint32_t>(aUptime);
125 hours = DivideAndGetRemainder(remainder, Time::kOneHourInMsec);
126 minutes = DivideAndGetRemainder(remainder, Time::kOneMinuteInMsec);
127 seconds = DivideAndGetRemainder(remainder, Time::kOneSecondInMsec);
128
129 aWriter.Append("%02u:%02u:%02u", hours, minutes, seconds);
130
131 if (aIncludeMsec)
132 {
133 aWriter.Append(".%03u", static_cast<uint16_t>(remainder));
134 }
135 }
136
137 } // namespace ot
138
139 #endif // OPENTHREAD_CONFIG_UPTIME_ENABLE
140