• 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 includes definitions for tracking device's uptime.
32  */
33 
34 #ifndef UPTIME_HPP_
35 #define UPTIME_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if !OPENTHREAD_CONFIG_UPTIME_ENABLE && OPENTHREAD_FTD
40 #error "OPENTHREAD_CONFIG_UPTIME_ENABLE is required for FTD"
41 #endif
42 
43 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
44 
45 #include "common/locator.hpp"
46 #include "common/non_copyable.hpp"
47 #include "common/string.hpp"
48 #include "common/time.hpp"
49 #include "common/timer.hpp"
50 
51 namespace ot {
52 
53 /**
54  * Implements tracking of device uptime (in msec).
55  */
56 class Uptime : public InstanceLocator, private NonCopyable
57 {
58 public:
59     static constexpr uint16_t kStringSize = OT_UPTIME_STRING_SIZE; ///< Recommended string size to represent uptime.
60 
61     /**
62      * Initializes an `Uptime` instance.
63      *
64      * @param[in] aInstance   The OpenThread instance.
65      */
66     explicit Uptime(Instance &aInstance);
67 
68     /**
69      * Returns the current device uptime (in msec).
70      *
71      * The uptime is maintained as number of milliseconds since OpenThread instance was initialized.
72      *
73      * @returns The uptime (number of milliseconds).
74      */
75     uint64_t GetUptime(void) const;
76 
77     /**
78      * Gets the current uptime as a human-readable string.
79      *
80      * The string follows the format "<hh>:<mm>:<ss>.<mmmm>" for hours, minutes, seconds and millisecond (if uptime is
81      * shorter than one day) or "<dd>d.<hh>:<mm>:<ss>.<mmmm>" (if longer than a day).
82      *
83      * If the resulting string does not fit in @p aBuffer (within its @p aSize characters), the string will be
84      * truncated but the outputted string is always null-terminated.
85      *
86      * @param[out] aBuffer   A pointer to a char array to output the string.
87      * @param[in]  aSize     The size of @p aBuffer (in bytes). Recommended to use `OT_UPTIME_STRING_SIZE`.
88      */
89     void GetUptime(char *aBuffer, uint16_t aSize) const;
90 
91     /**
92      * Converts an uptime value (number of milliseconds) to a human-readable string.
93      *
94      * The string follows the format "<hh>:<mm>:<ss>.<mmmm>" for hours, minutes, seconds and millisecond (if uptime is
95      * shorter than one day) or "<dd>d.<hh>:<mm>:<ss>.<mmmm>" (if longer than a day). @p aIncludeMsec can be used
96      * to determine whether `.<mmm>` milliseconds is included or omitted in the resulting string.
97      *
98      * @param[in]     aUptime        The uptime to convert.
99      * @param[in,out] aWriter        A `StringWriter` to append the converted string to.
100      * @param[in]     aIncludeMsec   Whether to include `.<mmm>` milliseconds in the string.
101      */
102     static void UptimeToString(uint64_t aUptime, StringWriter &aWriter, bool aIncludeMsec);
103 
104     /**
105      * Converts a given uptime as number of milliseconds to number of seconds.
106      *
107      * @param[in] aUptimeInMilliseconds    Uptime in milliseconds (as `uint64_t`).
108      *
109      * @returns The converted @p aUptimeInMilliseconds to seconds (as `uint32_t`).
110      */
MsecToSec(uint64_t aUptimeInMilliseconds)111     static uint32_t MsecToSec(uint64_t aUptimeInMilliseconds)
112     {
113         return static_cast<uint32_t>(aUptimeInMilliseconds / 1000u);
114     }
115 
116     /**
117      * Converts a given uptime as number of seconds to number of milliseconds.
118      *
119      * @param[in] aUptimeInSeconds    Uptime in seconds (as `uint32_t`).
120      *
121      * @returns The converted @p aUptimeInSeconds to milliseconds (as `uint64_t`).
122      */
SecToMsec(uint32_t aUptimeInSeconds)123     static uint64_t SecToMsec(uint32_t aUptimeInSeconds) { return static_cast<uint64_t>(aUptimeInSeconds) * 1000u; }
124 
125 private:
126     static constexpr uint32_t kTimerInterval = (1 << 30);
127 
128     static_assert(static_cast<uint32_t>(4 * kTimerInterval) == 0, "kTimerInterval is not correct");
129 
130     void HandleTimer(void);
131 
132     using UptimeTimer = TimerMilliIn<Uptime, &Uptime::HandleTimer>;
133 
134     TimeMilli   mStartTime;
135     uint32_t    mOverflowCount;
136     UptimeTimer mTimer;
137 };
138 
139 } // namespace ot
140 
141 #endif // OPENTHREAD_CONFIG_UPTIME_ENABLE
142 
143 #endif // UPTIME_HPP_
144