1 /*
2  *  Copyright (c) 2019, 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 MLE types and constants.
32  */
33 
34 #ifndef MLE_TYPES_HPP_
35 #define MLE_TYPES_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <limits.h>
40 #include <stdint.h>
41 #include <string.h>
42 
43 #include <openthread/thread.h>
44 
45 #include "common/clearable.hpp"
46 #include "common/code_utils.hpp"
47 #include "common/encoding.hpp"
48 #include "common/equatable.hpp"
49 #include "common/string.hpp"
50 #include "mac/mac_types.hpp"
51 #include "net/ip6_address.hpp"
52 
53 namespace ot {
54 namespace Mle {
55 
56 /**
57  * @addtogroup core-mle-core
58  *
59  * @brief
60  *   This module includes definition for MLE types and constants.
61  *
62  * @{
63  *
64  */
65 
66 constexpr uint16_t kMaxChildren               = OPENTHREAD_CONFIG_MLE_MAX_CHILDREN;
67 constexpr uint8_t  kMaxChildKeepAliveAttempts = 4; ///< Max keep alive attempts before reattach to a new Parent.
68 constexpr uint8_t  kFailedChildTransmissions  = OPENTHREAD_CONFIG_FAILED_CHILD_TRANSMISSIONS;
69 
70 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
71 // Extra one for core Backbone Router Service.
72 constexpr uint8_t kMaxServiceAlocs = OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_MAX_ALOCS + 1;
73 #else
74 constexpr uint8_t  kMaxServiceAlocs      = OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_MAX_ALOCS;
75 #endif
76 
77 constexpr uint8_t  kThreadVersion = OPENTHREAD_CONFIG_THREAD_VERSION; ///< Thread Version
78 constexpr uint16_t kUdpPort       = 19788;                            ///< MLE UDP Port
79 
80 /*
81  * MLE Protocol delays and timeouts.
82  *
83  */
84 constexpr uint32_t kParentRequestRouterTimeout     = 750;  ///< Router Parent Request timeout (in msec)
85 constexpr uint32_t kParentRequestDuplicateMargin   = 50;   ///< Margin for duplicate parent request
86 constexpr uint32_t kParentRequestReedTimeout       = 1250; ///< Router and REEDs Parent Request timeout (in msec)
87 constexpr uint32_t kAttachStartJitter              = 50;   ///< Max jitter time added to start of attach (in msec)
88 constexpr uint32_t kAnnounceProcessTimeout         = 250;  ///< Delay after Announce rx before channel/pan-id change
89 constexpr uint32_t kAnnounceTimeout                = 1400; ///< Total timeout for sending Announce messages (in msec)
90 constexpr uint32_t kMinAnnounceDelay               = 80;   ///< Min delay between Announcement messages (in msec)
91 constexpr uint32_t kParentResponseMaxDelayRouters  = 500;  ///< Max response delay for Parent Req to routers (in msec)
92 constexpr uint32_t kParentResponseMaxDelayAll      = 1000; ///< Max response delay for Parent Req to all (in msec)
93 constexpr uint32_t kUnicastRetransmissionDelay     = 1000; ///< Base delay before an MLE unicast retx (in msec)
94 constexpr uint32_t kChildUpdateRequestPendingDelay = 100;  ///< Delay for aggregating Child Update Req (in msec)
95 constexpr uint8_t  kMaxTransmissionCount           = 3;    ///< Max number of times an MLE message may be transmitted
96 constexpr uint32_t kMaxResponseDelay               = 1000; ///< Max response delay for a multicast request (in msec)
97 constexpr uint32_t kMaxChildIdRequestTimeout       = 5000; ///< Max delay to rx a Child ID Request (in msec)
98 constexpr uint32_t kMaxChildUpdateResponseTimeout  = 2000; ///< Max delay to rx a Child Update Response (in msec)
99 constexpr uint32_t kMaxLinkRequestTimeout          = 2000; ///< Max delay to rx a Link Accept
100 
101 constexpr uint32_t kMinTimeoutKeepAlive = (((kMaxChildKeepAliveAttempts + 1) * kUnicastRetransmissionDelay) / 1000);
102 constexpr uint32_t kMinPollPeriod       = OPENTHREAD_CONFIG_MAC_MINIMUM_POLL_PERIOD;
103 constexpr uint32_t kRetxPollPeriod      = OPENTHREAD_CONFIG_MAC_RETX_POLL_PERIOD;
104 constexpr uint32_t kMinTimeoutDataPoll  = (kMinPollPeriod + kFailedChildTransmissions * kRetxPollPeriod) / 1000;
105 constexpr uint32_t kMinTimeout          = OT_MAX(kMinTimeoutKeepAlive, kMinTimeoutDataPoll); ///< Min timeout (in sec)
106 
107 #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
108 constexpr uint8_t kLinkAcceptMaxRouters = 3; ///< Max Route TLV entries in a Link Accept message
109 #else
110 constexpr uint8_t  kLinkAcceptMaxRouters = 20; ///< Max Route TLV entries in a Link Accept message
111 #endif
112 constexpr uint8_t kLinkAcceptSequenceRollback = 64; ///< Route Sequence value rollback in a Link Accept message.
113 
114 constexpr uint16_t kMinChildId = 1;   ///< Minimum Child ID
115 constexpr uint16_t kMaxChildId = 511; ///< Maximum Child ID
116 
117 constexpr uint8_t kRouterIdOffset   = 10; ///< Bit offset of Router ID in RLOC16
118 constexpr uint8_t kRlocPrefixLength = 14; ///< Prefix length of RLOC in bytes
119 
120 constexpr uint8_t kMinChallengeSize = 4; ///< Minimum Challenge size in bytes.
121 constexpr uint8_t kMaxChallengeSize = 8; ///< Maximum Challenge size in bytes.
122 
123 /*
124  * Routing Protocol Constants
125  *
126  */
127 constexpr uint32_t kAdvertiseIntervalMin = 1; ///< Min Advertise interval (in sec)
128 #if OPENTHREAD_CONFIG_MLE_LONG_ROUTES_ENABLE
129 constexpr uint32_t kAdvertiseIntervalMax = 5; ///< Max Advertise interval (in sec)
130 #else
131 constexpr uint32_t kAdvertiseIntervalMax = 32; ///< Max Advertise interval (in sec)
132 #endif
133 
134 constexpr uint8_t kFailedRouterTransmissions = 4;
135 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
136 constexpr uint8_t kFailedCslDataPollTransmissions = 15;
137 #endif
138 
139 constexpr uint8_t  kRouterIdReuseDelay     = 100; ///< (in sec)
140 constexpr uint32_t kRouterIdSequencePeriod = 10;  ///< (in sec)
141 constexpr uint32_t kMaxNeighborAge         = 100; ///< (in sec)
142 
143 #if OPENTHREAD_CONFIG_MLE_LONG_ROUTES_ENABLE
144 constexpr uint8_t kMaxRouteCost = 127;
145 #else
146 constexpr uint8_t  kMaxRouteCost         = 16;
147 #endif
148 
149 constexpr uint8_t kMaxRouterId           = OT_NETWORK_MAX_ROUTER_ID; ///< Max Router ID
150 constexpr uint8_t kInvalidRouterId       = kMaxRouterId + 1;         ///< Value indicating incorrect Router ID
151 constexpr uint8_t kMaxRouters            = OPENTHREAD_CONFIG_MLE_MAX_ROUTERS;
152 constexpr uint8_t kMinDowngradeNeighbors = 7;
153 
154 constexpr uint8_t kNetworkIdTimeout           = 120; ///< (in sec)
155 constexpr uint8_t kParentRouteToLeaderTimeout = 20;  ///< (in sec)
156 constexpr uint8_t kRouterSelectionJitter      = 120; ///< (in sec)
157 
158 constexpr uint8_t kRouterDowngradeThreshold = 23;
159 constexpr uint8_t kRouterUpgradeThreshold   = 16;
160 
161 constexpr uint32_t kMaxLeaderToRouterTimeout = 90;  ///< (in sec)
162 constexpr uint32_t kReedAdvertiseInterval    = 570; ///< (in sec)
163 constexpr uint32_t kReedAdvertiseJitter      = 60;  ///< (in sec)
164 
165 constexpr uint8_t  kLeaderWeight             = 64;                                          ///< Default leader weight
166 constexpr uint32_t kMleEndDeviceTimeout      = OPENTHREAD_CONFIG_MLE_CHILD_TIMEOUT_DEFAULT; ///< (in sec)
167 constexpr uint8_t  kMeshLocalPrefixContextId = 0; ///< 0 is reserved for Mesh Local Prefix
168 
169 constexpr int8_t kParentPriorityHigh        = 1;  ///< Parent Priority High
170 constexpr int8_t kParentPriorityMedium      = 0;  ///< Parent Priority Medium (default)
171 constexpr int8_t kParentPriorityLow         = -1; ///< Parent Priority Low
172 constexpr int8_t kParentPriorityUnspecified = -2; ///< Parent Priority Unspecified
173 
174 constexpr uint8_t kLinkQuality3LinkCost = 1;             ///< Link Cost for Link Quality 3
175 constexpr uint8_t kLinkQuality2LinkCost = 2;             ///< Link Cost for Link Quality 2
176 constexpr uint8_t kLinkQuality1LinkCost = 4;             ///< Link Cost for Link Quality 1
177 constexpr uint8_t kLinkQuality0LinkCost = kMaxRouteCost; ///< Link Cost for Link Quality 0
178 
179 constexpr uint8_t kMplChildDataMessageTimerExpirations  = 0; ///< Number of MPL retransmissions for Children.
180 constexpr uint8_t kMplRouterDataMessageTimerExpirations = 2; ///< Number of MPL retransmissions for Routers.
181 
182 /**
183  * This type represents a Thread device role.
184  *
185  */
186 enum DeviceRole : uint8_t
187 {
188     kRoleDisabled = OT_DEVICE_ROLE_DISABLED, ///< The Thread stack is disabled.
189     kRoleDetached = OT_DEVICE_ROLE_DETACHED, ///< Not currently participating in a Thread network/partition.
190     kRoleChild    = OT_DEVICE_ROLE_CHILD,    ///< The Thread Child role.
191     kRoleRouter   = OT_DEVICE_ROLE_ROUTER,   ///< The Thread Router role.
192     kRoleLeader   = OT_DEVICE_ROLE_LEADER,   ///< The Thread Leader role.
193 };
194 
195 /**
196  * MLE Attach modes
197  *
198  */
199 enum AttachMode : uint8_t
200 {
201     kAttachAny           = 0, ///< Attach to any Thread partition.
202     kAttachSame1         = 1, ///< Attach to the same Thread partition (attempt 1 when losing connectivity).
203     kAttachSame2         = 2, ///< Attach to the same Thread partition (attempt 2 when losing connectivity).
204     kAttachBetter        = 3, ///< Attach to a better (i.e. higher weight/partition id) Thread partition.
205     kAttachSameDowngrade = 4, ///< Attach to the same Thread partition during downgrade process.
206 };
207 
208 constexpr uint16_t kAloc16Leader                      = 0xfc00;
209 constexpr uint16_t kAloc16DhcpAgentStart              = 0xfc01;
210 constexpr uint16_t kAloc16DhcpAgentEnd                = 0xfc0f;
211 constexpr uint16_t kAloc16ServiceStart                = 0xfc10;
212 constexpr uint16_t kAloc16ServiceEnd                  = 0xfc2f;
213 constexpr uint16_t kAloc16CommissionerStart           = 0xfc30;
214 constexpr uint16_t kAloc16CommissionerEnd             = 0xfc37;
215 constexpr uint16_t kAloc16BackboneRouterPrimary       = 0xfc38;
216 constexpr uint16_t kAloc16CommissionerMask            = 0x0007;
217 constexpr uint16_t kAloc16NeighborDiscoveryAgentStart = 0xfc40;
218 constexpr uint16_t kAloc16NeighborDiscoveryAgentEnd   = 0xfc4e;
219 
220 constexpr uint8_t kServiceMinId = 0x00; ///< Minimal Service ID.
221 constexpr uint8_t kServiceMaxId = 0x0f; ///< Maximal Service ID.
222 
223 #if OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2
224 
225 /*
226  * Backbone Router / DUA / MLR constants
227  *
228  */
229 constexpr uint16_t kRegistrationDelayDefault         = 1200;              ///< In seconds.
230 constexpr uint32_t kMlrTimeoutDefault                = 3600;              ///< In seconds.
231 constexpr uint32_t kMlrTimeoutMin                    = 300;               ///< In seconds.
232 constexpr uint32_t kMlrTimeoutMax                    = 0x7fffffff / 1000; ///< In seconds (about 24 days).
233 constexpr uint8_t  kBackboneRouterRegistrationJitter = 5;                 ///< In seconds.
234 constexpr uint8_t  kParentAggregateDelay             = 5;                 ///< In seconds.
235 constexpr uint8_t  kNoBufDelay                       = 5;                 ///< In seconds.
236 constexpr uint8_t  kImmediateReRegisterDelay         = 1;                 ///< In seconds.
237 constexpr uint8_t  KResponseTimeoutDelay             = 30;                ///< In seconds.
238 
239 /**
240  * Time period after which the address becomes "Preferred" if no duplicate address error (in seconds).
241  *
242  */
243 constexpr uint32_t kDuaDadPeriod = 100;
244 
245 /**
246  * Maximum number of times the multicast DAD query and wait time DUA_DAD_QUERY_TIMEOUT are repeated by the BBR, as
247  * part of the DAD process.
248  *
249  */
250 constexpr uint8_t kDuaDadRepeats = 3;
251 
252 /**
253  * Time period (in seconds) during which a DUA registration is considered 'recent' at a BBR.
254  *
255  */
256 constexpr uint32_t kDuaRecentTime               = 20;
257 constexpr uint32_t kTimeSinceLastTransactionMax = 10 * 86400; ///< In seconds (10 days).
258 constexpr uint8_t  kDefaultBackboneHoplimit     = 1;          ///< default hoplimit for Backbone Link Protocol messages
259 
260 static_assert(kMlrTimeoutDefault >= kMlrTimeoutMin && kMlrTimeoutDefault <= kMlrTimeoutMax,
261               "kMlrTimeoutDefault must be larger than or equal to kMlrTimeoutMin");
262 
263 static_assert(Mle::kParentAggregateDelay > 1, "kParentAggregateDelay should be larger than 1 second");
264 static_assert(kMlrTimeoutMax * 1000 > kMlrTimeoutMax, "SecToMsec(kMlrTimeoutMax) will overflow");
265 
266 static_assert(kTimeSinceLastTransactionMax * 1000 > kTimeSinceLastTransactionMax,
267               "SecToMsec(kTimeSinceLastTransactionMax) will overflow");
268 
269 /**
270  * State change of Child's DUA
271  *
272  */
273 enum class ChildDuaState : uint8_t
274 {
275     kAdded,     ///< A new DUA registered by the Child via Address Registration.
276     kChanged,   ///< A different DUA registered by the Child via Address Registration.
277     kRemoved,   ///< DUA registered by the Child is removed and not in Address Registration.
278     kUnchanged, ///< The Child registers the same DUA again.
279 };
280 
281 #endif // OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2
282 
283 /**
284  * This type represents a MLE device mode.
285  *
286  */
287 class DeviceMode : public Equatable<DeviceMode>
288 {
289 public:
290     static constexpr uint8_t kModeRxOnWhenIdle     = 1 << 3; ///< If to keep receiver on when not transmitting.
291     static constexpr uint8_t kModeReserved         = 1 << 2; ///< Set on transmission, ignore on reception.
292     static constexpr uint8_t kModeFullThreadDevice = 1 << 1; ///< If the device is an FTD.
293     static constexpr uint8_t kModeFullNetworkData  = 1 << 0; ///< If the device requires the full Network Data.
294 
295     static constexpr uint16_t kInfoStringSize = 45; ///< String buffer size used for `ToString()`.
296 
297     /**
298      * This type defines the fixed-length `String` object returned from `ToString()`.
299      *
300      */
301     typedef String<kInfoStringSize> InfoString;
302 
303     /**
304      *  This structure represents an MLE Mode configuration.
305      *
306      */
307     typedef otLinkModeConfig ModeConfig;
308 
309     /**
310      * This is the default constructor for `DeviceMode` object.
311      *
312      */
313     DeviceMode(void) = default;
314 
315     /**
316      * This constructor initializes a `DeviceMode` object from a given mode TLV bitmask.
317      *
318      * @param[in] aMode   A mode TLV bitmask to initialize the `DeviceMode` object.
319      *
320      */
DeviceMode(uint8_t aMode)321     explicit DeviceMode(uint8_t aMode) { Set(aMode); }
322 
323     /**
324      * This constructor initializes a `DeviceMode` object from a given mode configuration structure.
325      *
326      * @param[in] aModeConfig   A mode configuration to initialize the `DeviceMode` object.
327      *
328      */
DeviceMode(ModeConfig aModeConfig)329     explicit DeviceMode(ModeConfig aModeConfig) { Set(aModeConfig); }
330 
331     /**
332      * This method gets the device mode as a mode TLV bitmask.
333      *
334      * @returns The device mode as a mode TLV bitmask.
335      *
336      */
Get(void) const337     uint8_t Get(void) const { return mMode; }
338 
339     /**
340      * This method sets the device mode from a given mode TLV bitmask.
341      *
342      * @param[in] aMode   A mode TLV bitmask.
343      *
344      */
Set(uint8_t aMode)345     void Set(uint8_t aMode) { mMode = aMode | kModeReserved; }
346 
347     /**
348      * This method gets the device mode as a mode configuration structure.
349      *
350      * @param[out] aModeConfig   A reference to a mode configuration structure to output the device mode.
351      *
352      */
353     void Get(ModeConfig &aModeConfig) const;
354 
355     /**
356      * this method sets the device mode from a given mode configuration structure.
357      *
358      * @param[in] aModeConfig   A mode configuration structure.
359      *
360      */
361     void Set(const ModeConfig &aModeConfig);
362 
363     /**
364      * This method indicates whether or not the device is rx-on-when-idle.
365      *
366      * @retval TRUE   If the device is rx-on-when-idle (non-sleepy).
367      * @retval FALSE  If the device is not rx-on-when-idle (sleepy).
368      *
369      */
IsRxOnWhenIdle(void) const370     bool IsRxOnWhenIdle(void) const { return (mMode & kModeRxOnWhenIdle) != 0; }
371 
372     /**
373      * This method indicates whether or not the device is a Full Thread Device.
374      *
375      * @retval TRUE   If the device is Full Thread Device.
376      * @retval FALSE  If the device if not Full Thread Device.
377      *
378      */
IsFullThreadDevice(void) const379     bool IsFullThreadDevice(void) const { return (mMode & kModeFullThreadDevice) != 0; }
380 
381     /**
382      * This method indicates whether or not the device requests Full Network Data.
383      *
384      * @retval TRUE   If the device requests Full Network Data.
385      * @retval FALSE  If the device does not request Full Network Data (only stable Network Data).
386      *
387      */
IsFullNetworkData(void) const388     bool IsFullNetworkData(void) const { return (mMode & kModeFullNetworkData) != 0; }
389 
390     /**
391      * This method indicates whether or not the device is a Minimal End Device.
392      *
393      * @retval TRUE   If the device is a Minimal End Device.
394      * @retval FALSE  If the device is not a Minimal End Device.
395      *
396      */
IsMinimalEndDevice(void) const397     bool IsMinimalEndDevice(void) const
398     {
399         return (mMode & (kModeFullThreadDevice | kModeRxOnWhenIdle)) != (kModeFullThreadDevice | kModeRxOnWhenIdle);
400     }
401 
402     /**
403      * This method indicates whether or not the device mode flags are valid.
404      *
405      * An FTD which is not rx-on-when-idle (is sleepy) is considered invalid.
406      *
407      * @returns TRUE if , FALSE otherwise.
408      * @retval TRUE   If the device mode flags are valid.
409      * @retval FALSE  If the device mode flags are not valid.
410      *
411      */
IsValid(void) const412     bool IsValid(void) const { return !IsFullThreadDevice() || IsRxOnWhenIdle(); }
413 
414     /**
415      * This method converts the device mode into a human-readable string.
416      *
417      * @returns An `InfoString` object representing the device mode.
418      *
419      */
420     InfoString ToString(void) const;
421 
422 private:
423     uint8_t mMode;
424 };
425 
426 /**
427  * This class represents a Mesh Local Prefix.
428  *
429  */
430 OT_TOOL_PACKED_BEGIN
431 class MeshLocalPrefix : public Ip6::NetworkPrefix
432 {
433 public:
434     /**
435      * This method derives and sets the Mesh Local Prefix from an Extended PAN ID.
436      *
437      * @param[in] aExtendedPanId   An Extended PAN ID.
438      *
439      */
440     void SetFromExtendedPanId(const Mac::ExtendedPanId &aExtendedPanId);
441 
442 } OT_TOOL_PACKED_END;
443 
444 /**
445  * This class represents the Thread Leader Data.
446  *
447  */
448 class LeaderData : public otLeaderData, public Clearable<LeaderData>
449 {
450 public:
451     /**
452      * This method returns the Partition ID value.
453      *
454      * @returns The Partition ID value.
455      *
456      */
GetPartitionId(void) const457     uint32_t GetPartitionId(void) const { return mPartitionId; }
458 
459     /**
460      * This method sets the Partition ID value.
461      *
462      * @param[in]  aPartitionId  The Partition ID value.
463      *
464      */
SetPartitionId(uint32_t aPartitionId)465     void SetPartitionId(uint32_t aPartitionId) { mPartitionId = aPartitionId; }
466 
467     /**
468      * This method returns the Weighting value.
469      *
470      * @returns The Weighting value.
471      *
472      */
GetWeighting(void) const473     uint8_t GetWeighting(void) const { return mWeighting; }
474 
475     /**
476      * This method sets the Weighting value.
477      *
478      * @param[in]  aWeighting  The Weighting value.
479      *
480      */
SetWeighting(uint8_t aWeighting)481     void SetWeighting(uint8_t aWeighting) { mWeighting = aWeighting; }
482 
483     /**
484      * This method returns the Data Version value.
485      *
486      * @returns The Data Version value.
487      *
488      */
GetDataVersion(void) const489     uint8_t GetDataVersion(void) const { return mDataVersion; }
490 
491     /**
492      * This method sets the Data Version value.
493      *
494      * @param[in]  aVersion  The Data Version value.
495      *
496      */
SetDataVersion(uint8_t aVersion)497     void SetDataVersion(uint8_t aVersion) { mDataVersion = aVersion; }
498 
499     /**
500      * This method returns the Stable Data Version value.
501      *
502      * @returns The Stable Data Version value.
503      *
504      */
GetStableDataVersion(void) const505     uint8_t GetStableDataVersion(void) const { return mStableDataVersion; }
506 
507     /**
508      * This method sets the Stable Data Version value.
509      *
510      * @param[in]  aVersion  The Stable Data Version value.
511      *
512      */
SetStableDataVersion(uint8_t aVersion)513     void SetStableDataVersion(uint8_t aVersion) { mStableDataVersion = aVersion; }
514 
515     /**
516      * This method returns the Leader Router ID value.
517      *
518      * @returns The Leader Router ID value.
519      *
520      */
GetLeaderRouterId(void) const521     uint8_t GetLeaderRouterId(void) const { return mLeaderRouterId; }
522 
523     /**
524      * This method sets the Leader Router ID value.
525      *
526      * @param[in]  aRouterId  The Leader Router ID value.
527      *
528      */
SetLeaderRouterId(uint8_t aRouterId)529     void SetLeaderRouterId(uint8_t aRouterId) { mLeaderRouterId = aRouterId; }
530 };
531 
532 OT_TOOL_PACKED_BEGIN
533 class RouterIdSet : public Equatable<RouterIdSet>
534 {
535 public:
536     /**
537      * This method clears the Router Id Set.
538      *
539      */
Clear(void)540     void Clear(void) { memset(mRouterIdSet, 0, sizeof(mRouterIdSet)); }
541 
542     /**
543      * This method indicates whether or not a Router ID bit is set.
544      *
545      * @param[in]  aRouterId  The Router ID.
546      *
547      * @retval TRUE   If the Router ID bit is set.
548      * @retval FALSE  If the Router ID bit is not set.
549      *
550      */
Contains(uint8_t aRouterId) const551     bool Contains(uint8_t aRouterId) const { return (mRouterIdSet[aRouterId / 8] & (0x80 >> (aRouterId % 8))) != 0; }
552 
553     /**
554      * This method sets a given Router ID.
555      *
556      * @param[in]  aRouterId  The Router ID to set.
557      *
558      */
Add(uint8_t aRouterId)559     void Add(uint8_t aRouterId) { mRouterIdSet[aRouterId / 8] |= 0x80 >> (aRouterId % 8); }
560 
561     /**
562      * This method removes a given Router ID.
563      *
564      * @param[in]  aRouterId  The Router ID to remove.
565      *
566      */
Remove(uint8_t aRouterId)567     void Remove(uint8_t aRouterId) { mRouterIdSet[aRouterId / 8] &= ~(0x80 >> (aRouterId % 8)); }
568 
569 private:
570     uint8_t mRouterIdSet[BitVectorBytes(Mle::kMaxRouterId + 1)];
571 } OT_TOOL_PACKED_END;
572 
573 /**
574  * This class represents a MLE key.
575  *
576  */
577 typedef Mac::Key Key;
578 
579 /**
580  * @}
581  *
582  */
583 
584 } // namespace Mle
585 } // namespace ot
586 
587 #endif // MLE_TYPES_HPP_
588