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 
38 namespace ot {
39 namespace MeshCoP {
40 
ConvertTo(otTimestamp & aTimestamp) const41 void Timestamp::ConvertTo(otTimestamp &aTimestamp) const
42 {
43     aTimestamp.mSeconds       = GetSeconds();
44     aTimestamp.mTicks         = GetTicks();
45     aTimestamp.mAuthoritative = GetAuthoritative();
46 }
47 
SetFromTimestamp(const otTimestamp & aTimestamp)48 void Timestamp::SetFromTimestamp(const otTimestamp &aTimestamp)
49 {
50     SetSeconds(aTimestamp.mSeconds);
51     SetTicks(aTimestamp.mTicks);
52     SetAuthoritative(aTimestamp.mAuthoritative);
53 }
54 
Compare(const Timestamp * aFirst,const Timestamp * aSecond)55 int Timestamp::Compare(const Timestamp *aFirst, const Timestamp *aSecond)
56 {
57     int rval;
58 
59     if (aFirst == nullptr)
60     {
61         // When `aFirst` is null but `aSecond is not, we return -1,
62         // (indicate `aFirst (null) < aSecond (non-null)`).
63         ExitNow(rval = (aSecond == nullptr) ? 0 : -1);
64     }
65 
66     if (aSecond == nullptr)
67     {
68         // When `aFirst` is not null, but `aSecond` is, we return +1,
69         // (indicate `aFirst (non-null) > aSecond (null)`).
70         ExitNow(rval = 1);
71     }
72 
73     // Both are non-null.
74 
75     rval = Compare(*aFirst, *aSecond);
76 
77 exit:
78     return rval;
79 }
80 
Compare(const Timestamp & aFirst,const Timestamp & aSecond)81 int Timestamp::Compare(const Timestamp &aFirst, const Timestamp &aSecond)
82 {
83     int      rval;
84     uint64_t firstSeconds;
85     uint64_t secondSeconds;
86     uint16_t firstTicks;
87     uint16_t secondTicks;
88     bool     firstAuthoritative;
89     bool     secondAuthoritative;
90 
91     firstSeconds  = aFirst.GetSeconds();
92     secondSeconds = aSecond.GetSeconds();
93 
94     if (firstSeconds != secondSeconds)
95     {
96         ExitNow(rval = (firstSeconds > secondSeconds) ? 1 : -1);
97     }
98 
99     firstTicks  = aFirst.GetTicks();
100     secondTicks = aSecond.GetTicks();
101 
102     if (firstTicks != secondTicks)
103     {
104         ExitNow(rval = (firstTicks > secondTicks) ? 1 : -1);
105     }
106 
107     firstAuthoritative  = aFirst.GetAuthoritative();
108     secondAuthoritative = aSecond.GetAuthoritative();
109 
110     if (firstAuthoritative != secondAuthoritative)
111     {
112         ExitNow(rval = firstAuthoritative ? 1 : -1);
113     }
114 
115     rval = 0;
116 
117 exit:
118     return rval;
119 }
120 
AdvanceRandomTicks(void)121 void Timestamp::AdvanceRandomTicks(void)
122 {
123     uint16_t ticks = GetTicks();
124 
125     ticks += Random::NonCrypto::GetUint32InRange(1, kMaxRandomTicks);
126 
127     if (ticks & (kTicksMask >> kTicksOffset))
128     {
129         SetSeconds(GetSeconds() + 1);
130     }
131 
132     SetTicks(ticks);
133 }
134 
135 } // namespace MeshCoP
136 } // namespace ot
137