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/locator_getters.hpp"
41 #include "instance/instance.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)
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, /* aIncludeMsec */ true);
89 }
90
HandleTimer(void)91 void Uptime::HandleTimer(void)
92 {
93 if (mTimer.GetFireTime() == mStartTime)
94 {
95 mOverflowCount++;
96 }
97
98 mTimer.FireAt(mTimer.GetFireTime() + kTimerInterval);
99 }
100
DivideAndGetRemainder(uint32_t & aDividend,uint32_t aDivisor)101 static uint16_t DivideAndGetRemainder(uint32_t &aDividend, uint32_t aDivisor)
102 {
103 // Returns the quotient of division `aDividend / aDivisor` and updates
104 // `aDividend` to returns the remainder
105
106 uint32_t quotient = aDividend / aDivisor;
107
108 aDividend -= quotient * aDivisor;
109
110 return static_cast<uint16_t>(quotient);
111 }
112
UptimeToString(uint64_t aUptime,StringWriter & aWriter,bool aIncludeMsec)113 void Uptime::UptimeToString(uint64_t aUptime, StringWriter &aWriter, bool aIncludeMsec)
114 {
115 uint64_t days = aUptime / Time::kOneDayInMsec;
116 uint32_t remainder;
117 uint16_t hours;
118 uint16_t minutes;
119 uint16_t seconds;
120
121 if (days > 0)
122 {
123 aWriter.Append("%lud.", static_cast<unsigned long>(days));
124 aUptime -= days * Time::kOneDayInMsec;
125 }
126
127 remainder = static_cast<uint32_t>(aUptime);
128 hours = DivideAndGetRemainder(remainder, Time::kOneHourInMsec);
129 minutes = DivideAndGetRemainder(remainder, Time::kOneMinuteInMsec);
130 seconds = DivideAndGetRemainder(remainder, Time::kOneSecondInMsec);
131
132 aWriter.Append("%02u:%02u:%02u", hours, minutes, seconds);
133
134 if (aIncludeMsec)
135 {
136 aWriter.Append(".%03u", static_cast<uint16_t>(remainder));
137 }
138 }
139
140 } // namespace ot
141
142 #endif // OPENTHREAD_CONFIG_UPTIME_ENABLE
143