0.01.00
timestamp.hpp
Go to the documentation of this file.
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 
35 #ifndef MESHCOP_TIMESTAMP_HPP_
36 #define MESHCOP_TIMESTAMP_HPP_
37 
38 #include "openthread-core-config.h"
39 
40 #include <string.h>
41 
42 #include <openthread/platform/toolchain.h>
43 
44 #include "common/encoding.hpp"
45 
46 using ot::Encoding::BigEndian::HostSwap16;
47 
48 namespace ot {
49 namespace MeshCoP {
50 
56 class Timestamp
57 {
58 public:
63  void Init(void) { memset(mSeconds, 0, sizeof(mSeconds)); mTicks = 0; }
64 
75  int Compare(const Timestamp &aCompare) const;
76 
83  uint64_t GetSeconds(void) const {
84  uint64_t seconds = 0;
85 
86  for (size_t i = 0; i < sizeof(mSeconds); i++) {
87  seconds = (seconds << 8) | mSeconds[i];
88  }
89 
90  return seconds;
91  }
92 
99  void SetSeconds(uint64_t aSeconds) {
100  for (size_t i = 0; i < sizeof(mSeconds); i++, aSeconds >>= 8) {
101  mSeconds[sizeof(mSeconds) - 1 - i] = aSeconds & 0xff;
102  }
103  }
104 
111  uint16_t GetTicks(void) const { return HostSwap16(mTicks) >> kTicksOffset; }
112 
119  void SetTicks(uint16_t aTicks) {
120  mTicks = HostSwap16((HostSwap16(mTicks) & ~kTicksMask) | ((aTicks << kTicksOffset) & kTicksMask));
121  }
122 
129  bool GetAuthoritative(void) const { return (HostSwap16(mTicks) & kAuthoritativeMask) != 0; }
130 
137  void SetAuthoritative(bool aAuthoritative) {
138  mTicks = HostSwap16((HostSwap16(mTicks) & kTicksMask) | ((aAuthoritative << kAuthoritativeOffset) &
139  kAuthoritativeMask));
140  }
141 
142 private:
143  uint8_t mSeconds[6];
144 
145  enum
146  {
147  kTicksOffset = 1,
148  kTicksMask = 0x7fff << kTicksOffset,
149  kAuthoritativeOffset = 0,
150  kAuthoritativeMask = 1 << kAuthoritativeOffset,
151  };
152  uint16_t mTicks;
154 
155 } // namespace MeshCoP
156 } // namespace ot
157 
158 #endif // MESHCOP_TIMESTAMP_HPP_
void SetTicks(uint16_t aTicks)
This method sets the Ticks value.
Definition: timestamp.hpp:119
Definition: cli.cpp:90
uint16_t GetTicks(void) const
This method returns the Ticks value.
Definition: timestamp.hpp:111
int Compare(const Timestamp &aCompare) const
This method compares this timestamp to another.
Definition: timestamp.cpp:43
#define OT_TOOL_PACKED_BEGIN
Compiler-specific indication that a class or struct must be byte packed.
Definition: toolchain.h:170
This file includes definitions for byte-ordering encoding.
void SetSeconds(uint64_t aSeconds)
This method sets the Seconds value.
Definition: timestamp.hpp:99
#define OT_TOOL_PACKED_END
Compiler-specific indication at the end of a byte packed class or struct.
Definition: toolchain.h:172
bool GetAuthoritative(void) const
This method returns the Authoritative value.
Definition: timestamp.hpp:129
void SetAuthoritative(bool aAuthoritative)
This method sets the Authoritative value.
Definition: timestamp.hpp:137
This class implements Timestamp generation and parsing.
Definition: timestamp.hpp:56
uint64_t GetSeconds(void) const
This method returns the Seconds value.
Definition: timestamp.hpp:83
This file includes compile-time configuration constants for OpenThread.
void Init(void)
This method initializes the Timestamp.
Definition: timestamp.hpp:63