• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Primary Backbone Router service management in the Thread Network.
32  */
33 
34 #ifndef BACKBONE_ROUTER_LEADER_HPP_
35 #define BACKBONE_ROUTER_LEADER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
40 
41 #include <openthread/backbone_router.h>
42 #include <openthread/backbone_router_ftd.h>
43 #include <openthread/ip6.h>
44 
45 #include "coap/coap.hpp"
46 #include "coap/coap_message.hpp"
47 #include "common/locator.hpp"
48 #include "common/log.hpp"
49 #include "common/non_copyable.hpp"
50 #include "net/ip6_address.hpp"
51 
52 namespace ot {
53 
54 namespace BackboneRouter {
55 
56 typedef otBackboneRouterConfig Config;
57 
58 constexpr uint16_t kDefaultRegistrationDelay  = 5;                 ///< Default registration delay (in sec).
59 constexpr uint32_t kDefaultMlrTimeout         = 3600;              ///< Default MLR Timeout (in sec).
60 constexpr uint32_t kMinMlrTimeout             = 300;               ///< Minimum MLR Timeout (in sec).
61 constexpr uint32_t kMaxMlrTimeout             = 0x7fffffff / 1000; ///< Max MLR Timeout (in sec ~ about 24 days.
62 constexpr uint8_t  kDefaultRegistrationJitter = 5;                 ///< Default registration jitter (in sec).
63 constexpr uint8_t  kParentAggregateDelay      = 5;                 ///< Parent Aggregate Delay (in sec).
64 
65 static_assert(kDefaultMlrTimeout >= kMinMlrTimeout && kDefaultMlrTimeout <= kMaxMlrTimeout,
66               "kDefaultMlrTimeout is not in valid range");
67 static_assert(kMaxMlrTimeout * 1000 > kMaxMlrTimeout, "SecToMsec(kMaxMlrTimeout) will overflow");
68 static_assert(kParentAggregateDelay > 1, "kParentAggregateDelay should be larger than 1 second");
69 
70 /**
71  * Represents Domain Prefix changes.
72  */
73 enum DomainPrefixEvent : uint8_t
74 {
75     kDomainPrefixAdded     = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_ADDED,   ///< Domain Prefix Added.
76     kDomainPrefixRemoved   = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_REMOVED, ///< Domain Prefix Removed.
77     kDomainPrefixRefreshed = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_CHANGED, ///< Domain Prefix Changed.
78     kDomainPrefixUnchanged,                                            ///< Domain Prefix did not change.
79 };
80 
81 /**
82  * Implements the basic Primary Backbone Router service operations.
83  */
84 class Leader : public InstanceLocator, private NonCopyable
85 {
86 public:
87     // Primary Backbone Router Service state or state change.
88     enum State : uint8_t
89     {
90         kStateNone = 0,       ///< Not exist (trigger Backbone Router register its service).
91         kStateAdded,          ///< Newly added.
92         kStateRemoved,        ///< Newly removed (trigger Backbone Router register its service).
93         kStateToTriggerRereg, ///< Short address or sequence number changes (trigger re-registration).
94                               ///< May also have ReregistrationDelay or MlrTimeout update.
95         kStateRefreshed,      ///< Only ReregistrationDelay or MlrTimeout changes.
96         kStateUnchanged,      ///< No change on Primary Backbone Router information (only for logging).
97     };
98 
99     /**
100      * Initializes the `Leader`.
101      *
102      * @param[in] aInstance  A reference to the OpenThread instance.
103      */
104     explicit Leader(Instance &aInstance);
105 
106     /**
107      * Resets the cached Primary Backbone Router.
108      */
109     void Reset(void);
110 
111     /**
112      * Updates the cached Primary Backbone Router if any when new network data is available.
113      */
114     void Update(void);
115 
116     /**
117      * Gets the Primary Backbone Router in the Thread Network.
118      *
119      * @param[out]  aConfig        The Primary Backbone Router information.
120      *
121      * @retval kErrorNone          Successfully got the Primary Backbone Router information.
122      * @retval kErrorNotFound      No Backbone Router in the Thread Network.
123      */
124     Error GetConfig(Config &aConfig) const;
125 
126     /**
127      * Gets the Backbone Router Service ID.
128      *
129      * @param[out]  aServiceId     The reference whether to put the Backbone Router Service ID.
130      *
131      * @retval kErrorNone          Successfully got the Backbone Router Service ID.
132      * @retval kErrorNotFound      Backbone Router service doesn't exist.
133      */
134     Error GetServiceId(uint8_t &aServiceId) const;
135 
136     /**
137      * Gets the short address of the Primary Backbone Router.
138      *
139      * @returns short address of Primary Backbone Router, or Mle::kInvalidRloc16 if no Primary Backbone Router.
140      */
GetServer16(void) const141     uint16_t GetServer16(void) const { return mConfig.mServer16; }
142 
143     /**
144      * Indicates whether or not there is Primary Backbone Router.
145      *
146      * @retval TRUE   If there is Primary Backbone Router.
147      * @retval FALSE  If there is no Primary Backbone Router.
148      */
HasPrimary(void) const149     bool HasPrimary(void) const { return mConfig.mServer16 != Mle::kInvalidRloc16; }
150 
151     /**
152      * Gets the Domain Prefix in the Thread Network.
153      *
154      * @retval A pointer to the Domain Prefix or nullptr if there is no Domain Prefix.
155      */
GetDomainPrefix(void) const156     const Ip6::Prefix *GetDomainPrefix(void) const
157     {
158         return (mDomainPrefix.GetLength() == 0) ? nullptr : &mDomainPrefix;
159     }
160 
161     /**
162      * Indicates whether or not the Domain Prefix is available in the Thread Network.
163      *
164      * @retval TRUE   If there is Domain Prefix.
165      * @retval FALSE  If there is no Domain Prefix.
166      */
HasDomainPrefix(void) const167     bool HasDomainPrefix(void) const { return (mDomainPrefix.GetLength() > 0); }
168 
169     /**
170      * Indicates whether or not the address is a Domain Unicast Address.
171      *
172      * @param[in]  aAddress A reference to the address.
173      *
174      * @retval true  @p aAddress is a Domain Unicast Address.
175      * @retval false @p aAddress is not a Domain Unicast Address.
176      */
177     bool IsDomainUnicast(const Ip6::Address &aAddress) const;
178 
179 private:
180     void UpdateBackboneRouterPrimary(void);
181     void UpdateDomainPrefixConfig(void);
182 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
183     void               LogBackboneRouterPrimary(State aState, const Config &aConfig) const;
184     static const char *StateToString(State aState);
185     static const char *DomainPrefixEventToString(DomainPrefixEvent aEvent);
186 #else
LogBackboneRouterPrimary(State,const Config &) const187     void LogBackboneRouterPrimary(State, const Config &) const {}
188 #endif
189 
190     Config      mConfig;       ///< Primary Backbone Router information.
191     Ip6::Prefix mDomainPrefix; ///< Domain Prefix in the Thread network.
192 };
193 
194 } // namespace BackboneRouter
195 
196 /**
197  * @}
198  */
199 
200 } // namespace ot
201 
202 #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
203 
204 #endif // BACKBONE_ROUTER_LEADER_HPP_
205