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