1 /*
2 * Copyright (c) 2016-2017, 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 Thread `Router` and `Parent`.
32 */
33
34 #include "router.hpp"
35
36 #include "common/array.hpp"
37 #include "common/code_utils.hpp"
38 #include "common/debug.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/num_utils.hpp"
41 #include "instance/instance.hpp"
42
43 namespace ot {
44
SetFrom(const Router & aRouter)45 void Router::Info::SetFrom(const Router &aRouter)
46 {
47 Clear();
48 mRloc16 = aRouter.GetRloc16();
49 mRouterId = Mle::RouterIdFromRloc16(mRloc16);
50 mExtAddress = aRouter.GetExtAddress();
51 mAllocated = true;
52 mNextHop = aRouter.GetNextHop();
53 mLinkEstablished = aRouter.IsStateValid();
54 mPathCost = aRouter.GetCost();
55 mLinkQualityIn = aRouter.GetLinkQualityIn();
56 mLinkQualityOut = aRouter.GetLinkQualityOut();
57 mAge = static_cast<uint8_t>(Time::MsecToSec(TimerMilli::GetNow() - aRouter.GetLastHeard()));
58 mVersion = ClampToUint8(aRouter.GetVersion());
59 }
60
SetFrom(const Parent & aParent)61 void Router::Info::SetFrom(const Parent &aParent)
62 {
63 SetFrom(static_cast<const Router &>(aParent));
64 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
65 mCslClockAccuracy = aParent.GetCslAccuracy().GetClockAccuracy();
66 mCslUncertainty = aParent.GetCslAccuracy().GetUncertainty();
67 #endif
68 }
69
Clear(void)70 void Router::Clear(void)
71 {
72 Instance &instance = GetInstance();
73
74 ClearAllBytes(*this);
75 Init(instance);
76 }
77
GetTwoWayLinkQuality(void) const78 LinkQuality Router::GetTwoWayLinkQuality(void) const { return Min(GetLinkQualityIn(), GetLinkQualityOut()); }
79
SetFrom(const Parent & aParent)80 void Router::SetFrom(const Parent &aParent)
81 {
82 // We use an intermediate pointer to copy `aParent` to silence
83 // code checkers warning about object slicing (assigning a
84 // sub-class to base class instance).
85
86 const Router *parentAsRouter = &aParent;
87
88 *this = *parentAsRouter;
89 }
90
Clear(void)91 void Parent::Clear(void)
92 {
93 Instance &instance = GetInstance();
94
95 ClearAllBytes(*this);
96 Init(instance);
97 }
98
SetNextHopAndCost(uint8_t aNextHop,uint8_t aCost)99 bool Router::SetNextHopAndCost(uint8_t aNextHop, uint8_t aCost)
100 {
101 bool changed = false;
102
103 if (mNextHop != aNextHop)
104 {
105 mNextHop = aNextHop;
106 changed = true;
107 }
108
109 if (mCost != aCost)
110 {
111 mCost = aCost;
112 changed = true;
113 }
114
115 return changed;
116 }
117
SetNextHopToInvalid(void)118 bool Router::SetNextHopToInvalid(void) { return SetNextHopAndCost(Mle::kInvalidRouterId, 0); }
119
120 } // namespace ot
121