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