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
39 namespace ot {
40 namespace MeshCoP {
41
ConvertTo(otTimestamp & aTimestamp) const42 void Timestamp::ConvertTo(otTimestamp &aTimestamp) const
43 {
44 aTimestamp.mSeconds = GetSeconds();
45 aTimestamp.mTicks = GetTicks();
46 aTimestamp.mAuthoritative = GetAuthoritative();
47 }
48
SetFromTimestamp(const otTimestamp & aTimestamp)49 void Timestamp::SetFromTimestamp(const otTimestamp &aTimestamp)
50 {
51 SetSeconds(aTimestamp.mSeconds);
52 SetTicks(aTimestamp.mTicks);
53 SetAuthoritative(aTimestamp.mAuthoritative);
54 }
55
Compare(const Timestamp * aFirst,const Timestamp * aSecond)56 int Timestamp::Compare(const Timestamp *aFirst, const Timestamp *aSecond)
57 {
58 int rval;
59
60 if (aFirst == nullptr)
61 {
62 // When `aFirst` is null but `aSecond is not, we return -1,
63 // (indicate `aFirst (null) < aSecond (non-null)`).
64 ExitNow(rval = (aSecond == nullptr) ? 0 : -1);
65 }
66
67 if (aSecond == nullptr)
68 {
69 // When `aFirst` is not null, but `aSecond` is, we return +1,
70 // (indicate `aFirst (non-null) > aSecond (null)`).
71 ExitNow(rval = 1);
72 }
73
74 // Both are non-null.
75
76 rval = Compare(*aFirst, *aSecond);
77
78 exit:
79 return rval;
80 }
81
Compare(const Timestamp & aFirst,const Timestamp & aSecond)82 int Timestamp::Compare(const Timestamp &aFirst, const Timestamp &aSecond)
83 {
84 int rval;
85
86 rval = ThreeWayCompare(aFirst.GetSeconds(), aSecond.GetSeconds());
87 VerifyOrExit(rval == 0);
88
89 rval = ThreeWayCompare(aFirst.GetTicks(), aSecond.GetTicks());
90 VerifyOrExit(rval == 0);
91
92 rval = ThreeWayCompare(aFirst.GetAuthoritative(), aSecond.GetAuthoritative());
93
94 exit:
95 return rval;
96 }
97
AdvanceRandomTicks(void)98 void Timestamp::AdvanceRandomTicks(void)
99 {
100 uint16_t ticks = GetTicks();
101
102 ticks += Random::NonCrypto::GetUint32InRange(1, kMaxRandomTicks);
103
104 if (ticks & (kTicksMask >> kTicksOffset))
105 {
106 SetSeconds(GetSeconds() + 1);
107 }
108
109 SetTicks(ticks);
110 }
111
112 } // namespace MeshCoP
113 } // namespace ot
114