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 includes definitions Thread `Router` and `Parent`.
32  */
33 
34 #ifndef ROUTER_HPP_
35 #define ROUTER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include "thread/neighbor.hpp"
40 
41 namespace ot {
42 
43 class Parent;
44 
45 /**
46  * Represents a Thread Router
47  *
48  */
49 class Router : public Neighbor
50 {
51 public:
52     /**
53      * Represents diagnostic information for a Thread Router.
54      *
55      */
56     class Info : public otRouterInfo, public Clearable<Info>
57     {
58     public:
59         /**
60          * Sets the `Info` instance from a given `Router`.
61          *
62          * @param[in] aRouter   A router.
63          *
64          */
65         void SetFrom(const Router &aRouter);
66 
67         /**
68          * Sets the `Info` instance from a given `Parent`.
69          *
70          * @param[in] aParent   A parent.
71          *
72          */
73         void SetFrom(const Parent &aParent);
74     };
75 
76     /**
77      * Initializes the `Router` object.
78      *
79      * @param[in] aInstance  A reference to OpenThread instance.
80      *
81      */
Init(Instance & aInstance)82     void Init(Instance &aInstance) { Neighbor::Init(aInstance); }
83 
84     /**
85      * Clears the router entry.
86      *
87      */
88     void Clear(void);
89 
90     /**
91      * Sets the `Router` entry from a `Parent`
92      *
93      */
94     void SetFrom(const Parent &aParent);
95 
96     /**
97      * Gets the router ID of the next hop to this router.
98      *
99      * @returns The router ID of the next hop to this router.
100      *
101      */
GetNextHop(void) const102     uint8_t GetNextHop(void) const { return mNextHop; }
103 
104     /**
105      * Gets the link quality out value for this router.
106      *
107      * @returns The link quality out value for this router.
108      *
109      */
GetLinkQualityOut(void) const110     LinkQuality GetLinkQualityOut(void) const { return static_cast<LinkQuality>(mLinkQualityOut); }
111 
112     /**
113      * Sets the link quality out value for this router.
114      *
115      * @param[in]  aLinkQuality  The link quality out value for this router.
116      *
117      */
SetLinkQualityOut(LinkQuality aLinkQuality)118     void SetLinkQualityOut(LinkQuality aLinkQuality) { mLinkQualityOut = aLinkQuality; }
119 
120     /**
121      * Gets the two-way link quality value (minimum of link quality in and out).
122      *
123      * @returns The two-way link quality value.
124      *
125      */
126     LinkQuality GetTwoWayLinkQuality(void) const;
127 
128     /**
129      * Get the route cost to this router.
130      *
131      * @returns The route cost to this router.
132      *
133      */
GetCost(void) const134     uint8_t GetCost(void) const { return mCost; }
135 
136     /**
137      * Sets the next hop and cost to this router.
138      *
139      * @param[in]  aNextHop  The Router ID of the next hop to this router.
140      * @param[in]  aCost     The cost to this router.
141      *
142      * @retval TRUE   If there was a change, i.e., @p aNextHop or @p aCost were different from their previous values.
143      * @retval FALSE  If no change to next hop and cost values (new values are the same as before).
144      *
145      */
146     bool SetNextHopAndCost(uint8_t aNextHop, uint8_t aCost);
147 
148     /**
149      * Sets the next hop to this router as invalid and clears the cost.
150      *
151      * @retval TRUE   If there was a change (next hop was valid before).
152      * @retval FALSE  No change to next hop (next hop was invalid before).
153      *
154      */
155     bool SetNextHopToInvalid(void);
156 
157 private:
158     uint8_t mNextHop;            ///< The next hop towards this router
159     uint8_t mLinkQualityOut : 2; ///< The link quality out for this router
160 
161 #if OPENTHREAD_CONFIG_MLE_LONG_ROUTES_ENABLE
162     uint8_t mCost; ///< The cost to this router via neighbor router
163 #else
164     uint8_t mCost : 4; ///< The cost to this router via neighbor router
165 #endif
166 };
167 
168 /**
169  * Represent parent of a child node.
170  *
171  */
172 class Parent : public Router
173 {
174 public:
175     /**
176      * Initializes the `Parent`.
177      *
178      * @param[in] aInstance  A reference to OpenThread instance.
179      *
180      */
Init(Instance & aInstance)181     void Init(Instance &aInstance)
182     {
183         Neighbor::Init(aInstance);
184 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
185         mCslAccuracy.Init();
186 #endif
187     }
188 
189     /**
190      * Clears the parent entry.
191      *
192      */
193     void Clear(void);
194 
195     /**
196      * Gets route cost from parent to leader.
197      *
198      * @returns The route cost from parent to leader
199      *
200      */
GetLeaderCost(void) const201     uint8_t GetLeaderCost(void) const { return mLeaderCost; }
202 
203     /**
204      * Sets route cost from parent to leader.
205      *
206      * @param[in] aLeaderCost  The route cost.
207      *
208      */
SetLeaderCost(uint8_t aLeaderCost)209     void SetLeaderCost(uint8_t aLeaderCost) { mLeaderCost = aLeaderCost; }
210 
211 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
212     /**
213      * Gets the CSL accuracy (clock accuracy and uncertainty).
214      *
215      * @returns The CSL accuracy.
216      *
217      */
GetCslAccuracy(void) const218     const Mac::CslAccuracy &GetCslAccuracy(void) const { return mCslAccuracy; }
219 
220     /**
221      * Sets CSL accuracy.
222      *
223      * @param[in] aCslAccuracy  The CSL accuracy.
224      *
225      */
SetCslAccuracy(const Mac::CslAccuracy & aCslAccuracy)226     void SetCslAccuracy(const Mac::CslAccuracy &aCslAccuracy) { mCslAccuracy = aCslAccuracy; }
227 #endif
228 
229 private:
230     uint8_t mLeaderCost;
231 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
232     Mac::CslAccuracy mCslAccuracy; // CSL accuracy (clock accuracy in ppm and uncertainty).
233 #endif
234 };
235 
236 DefineCoreType(otRouterInfo, Router::Info);
237 
238 } // namespace ot
239 
240 #endif // ROUTER_HPP_
241