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