• 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/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 BackboneRouterConfig;
56 
57 /**
58  * This class implements the basic Primary Backbone Router service operations.
59  *
60  */
61 class Leader : public InstanceLocator, private NonCopyable
62 {
63 public:
64     // Primary Backbone Router Service state or state change.
65     enum State : uint8_t
66     {
67         kStateNone = 0,       ///< Not exist (trigger Backbone Router register its service).
68         kStateAdded,          ///< Newly added.
69         kStateRemoved,        ///< Newly removed (trigger Backbone Router register its service).
70         kStateToTriggerRereg, ///< Short address or sequence number changes (trigger re-registration).
71                               ///< May also have ReregistrationDelay or MlrTimeout update.
72         kStateRefreshed,      ///< Only ReregistrationDelay or MlrTimeout changes.
73         kStateUnchanged,      ///< No change on Primary Backbone Router information (only for logging).
74     };
75 
76     // Domain Prefix state or state change.
77     enum DomainPrefixState : uint8_t
78     {
79         kDomainPrefixNone = 0,  ///< Not available.
80         kDomainPrefixAdded,     ///< Added.
81         kDomainPrefixRemoved,   ///< Removed.
82         kDomainPrefixRefreshed, ///< Changed.
83         kDomainPrefixUnchanged, ///< Nothing changed.
84     };
85 
86     /**
87      * This constructor initializes the `Leader`.
88      *
89      * @param[in] aInstance  A reference to the OpenThread instance.
90      *
91      */
92     explicit Leader(Instance &aInstance);
93 
94     /**
95      * This method resets the cached Primary Backbone Router.
96      *
97      */
98     void Reset(void);
99 
100     /**
101      * This method updates the cached Primary Backbone Router if any when new network data is available.
102      *
103      */
104     void Update(void);
105 
106     /**
107      * This method gets the Primary Backbone Router in the Thread Network.
108      *
109      * @param[out]  aConfig        The Primary Backbone Router information.
110      *
111      * @retval kErrorNone          Successfully got the Primary Backbone Router information.
112      * @retval kErrorNotFound      No Backbone Router in the Thread Network.
113      *
114      */
115     Error GetConfig(BackboneRouterConfig &aConfig) const;
116 
117     /**
118      * This method gets the Backbone Router Service ID.
119      *
120      * @param[out]  aServiceId     The reference whether to put the Backbone Router Service ID.
121      *
122      * @retval kErrorNone          Successfully got the Backbone Router Service ID.
123      * @retval kErrorNotFound      Backbone Router service doesn't exist.
124      *
125      */
126     Error GetServiceId(uint8_t &aServiceId) const;
127 
128     /**
129      * This method gets the short address of the Primary Backbone Router.
130      *
131      * @returns short address of Primary Backbone Router, or Mac::kShortAddrInvalid if no Primary Backbone Router.
132      *
133      */
GetServer16(void) const134     uint16_t GetServer16(void) const { return mConfig.mServer16; }
135 
136     /**
137      * This method indicates whether or not there is Primary Backbone Router.
138      *
139      * @retval TRUE   If there is Primary Backbone Router.
140      * @retval FALSE  If there is no Primary Backbone Router.
141      *
142      */
HasPrimary(void) const143     bool HasPrimary(void) const { return mConfig.mServer16 != Mac::kShortAddrInvalid; }
144 
145     /**
146      * This method gets the Domain Prefix in the Thread Network.
147      *
148      * @retval A pointer to the Domain Prefix or nullptr if there is no Domain Prefix.
149      *
150      */
GetDomainPrefix(void) const151     const Ip6::Prefix *GetDomainPrefix(void) const
152     {
153         return (mDomainPrefix.GetLength() == 0) ? nullptr : &mDomainPrefix;
154     }
155 
156     /**
157      * This method indicates whether or not the Domain Prefix is available in the Thread Network.
158      *
159      * @retval TRUE   If there is Domain Prefix.
160      * @retval FALSE  If there is no Domain Prefix.
161      *
162      */
HasDomainPrefix(void) const163     bool HasDomainPrefix(void) const { return (mDomainPrefix.GetLength() > 0); }
164 
165     /**
166      * This method indicates whether or not the address is a Domain Unicast Address.
167      *
168      * @param[in]  aAddress A reference to the address.
169      *
170      * @retval true  @p aAddress is a Domain Unicast Address.
171      * @retval false @p aAddress is not a Domain Unicast Address.
172      *
173      */
174     bool IsDomainUnicast(const Ip6::Address &aAddress) const;
175 
176 private:
177     void UpdateBackboneRouterPrimary(void);
178     void UpdateDomainPrefixConfig(void);
179 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
180     void               LogBackboneRouterPrimary(State aState, const BackboneRouterConfig &aConfig) const;
181     void               LogDomainPrefix(DomainPrefixState aState, const Ip6::Prefix &aPrefix) const;
182     static const char *StateToString(State aState);
183     static const char *DomainPrefixStateToString(DomainPrefixState aState);
184 #else
LogBackboneRouterPrimary(State,const BackboneRouterConfig &) const185     void LogBackboneRouterPrimary(State, const BackboneRouterConfig &) const {}
LogDomainPrefix(DomainPrefixState,const Ip6::Prefix &) const186     void LogDomainPrefix(DomainPrefixState, const Ip6::Prefix &) const {}
187 #endif
188 
189     BackboneRouterConfig mConfig;       ///< Primary Backbone Router information.
190     Ip6::Prefix          mDomainPrefix; ///< Domain Prefix in the Thread network.
191 };
192 
193 } // namespace BackboneRouter
194 
195 /**
196  * @}
197  */
198 
199 } // namespace ot
200 
201 #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
202 
203 #endif // BACKBONE_ROUTER_LEADER_HPP_
204