1 /*
2  *  Copyright (c) 2016, 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 common MeshCoP timestamp processing.
32  */
33 
34 #include "timestamp.hpp"
35 
36 #include "common/code_utils.hpp"
37 #include "common/num_utils.hpp"
38 #include "common/numeric_limits.hpp"
39 
40 namespace ot {
41 namespace MeshCoP {
42 
ConvertTo(Info & aInfo) const43 void Timestamp::ConvertTo(Info &aInfo) const
44 {
45     aInfo.mSeconds       = GetSeconds();
46     aInfo.mTicks         = GetTicks();
47     aInfo.mAuthoritative = GetAuthoritative();
48 }
49 
SetFrom(const Info & aInfo)50 void Timestamp::SetFrom(const Info &aInfo)
51 {
52     SetSeconds(aInfo.mSeconds);
53     SetTicks(aInfo.mTicks);
54     SetAuthoritative(aInfo.mAuthoritative);
55 }
56 
SetToInvalid(void)57 void Timestamp::SetToInvalid(void)
58 {
59     mSeconds16        = NumericLimits<uint16_t>::kMax;
60     mSeconds32        = NumericLimits<uint32_t>::kMax;
61     mTicksAndAuthFlag = NumericLimits<uint16_t>::kMax;
62 }
63 
IsValid(void) const64 bool Timestamp::IsValid(void) const
65 {
66     return (mSeconds16 != NumericLimits<uint16_t>::kMax) || (mSeconds32 != NumericLimits<uint32_t>::kMax) ||
67            (mTicksAndAuthFlag != NumericLimits<uint16_t>::kMax);
68 }
69 
SetToOrphanAnnounce(void)70 void Timestamp::SetToOrphanAnnounce(void)
71 {
72     mSeconds16 = 0;
73     mSeconds32 = 0;
74     SetTicksAndAuthFlag(kAuthoritativeFlag);
75 }
76 
IsOrphanAnnounce(void) const77 bool Timestamp::IsOrphanAnnounce(void) const
78 {
79     return (mSeconds16 == 0) && (mSeconds32 == 0) && (GetTicksAndAuthFlag() == kAuthoritativeFlag);
80 }
81 
GetSeconds(void) const82 uint64_t Timestamp::GetSeconds(void) const
83 {
84     return (static_cast<uint64_t>(BigEndian::HostSwap16(mSeconds16)) << 32) + BigEndian::HostSwap32(mSeconds32);
85 }
86 
SetSeconds(uint64_t aSeconds)87 void Timestamp::SetSeconds(uint64_t aSeconds)
88 {
89     mSeconds16 = BigEndian::HostSwap16(static_cast<uint16_t>(aSeconds >> 32));
90     mSeconds32 = BigEndian::HostSwap32(static_cast<uint32_t>(aSeconds & 0xffffffff));
91 }
92 
SetTicks(uint16_t aTicks)93 void Timestamp::SetTicks(uint16_t aTicks)
94 {
95     SetTicksAndAuthFlag((GetTicksAndAuthFlag() & ~kTicksMask) | ((aTicks << kTicksOffset) & kTicksMask));
96 }
97 
SetAuthoritative(bool aAuthoritative)98 void Timestamp::SetAuthoritative(bool aAuthoritative)
99 {
100     SetTicksAndAuthFlag((GetTicksAndAuthFlag() & kTicksMask) | (aAuthoritative ? kAuthoritativeFlag : 0));
101 }
102 
Compare(const Timestamp & aFirst,const Timestamp & aSecond)103 int Timestamp::Compare(const Timestamp &aFirst, const Timestamp &aSecond)
104 {
105     int rval;
106 
107     rval = ThreeWayCompare(aFirst.IsValid(), aSecond.IsValid());
108     VerifyOrExit(rval == 0);
109 
110     rval = ThreeWayCompare(aFirst.GetSeconds(), aSecond.GetSeconds());
111     VerifyOrExit(rval == 0);
112 
113     rval = ThreeWayCompare(aFirst.GetTicks(), aSecond.GetTicks());
114     VerifyOrExit(rval == 0);
115 
116     rval = ThreeWayCompare(aFirst.GetAuthoritative(), aSecond.GetAuthoritative());
117 
118 exit:
119     return rval;
120 }
121 
AdvanceRandomTicks(void)122 void Timestamp::AdvanceRandomTicks(void)
123 {
124     uint16_t ticks = GetTicks();
125 
126     ticks += Random::NonCrypto::GetUint32InRange(1, kMaxTicks + 1);
127 
128     if (ticks > kMaxTicks)
129     {
130         ticks -= (kMaxTicks + 1);
131         SetSeconds(GetSeconds() + 1);
132     }
133 
134     SetTicks(ticks);
135 }
136 
137 } // namespace MeshCoP
138 } // namespace ot
139