• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016, 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 manipulating local Thread Network Data.
32  */
33 
34 #ifndef NETWORK_DATA_LOCAL_HPP_
35 #define NETWORK_DATA_LOCAL_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
40 
41 #include "common/non_copyable.hpp"
42 #include "thread/network_data.hpp"
43 
44 namespace ot {
45 
46 /**
47  * @addtogroup core-netdata-local
48  *
49  * @brief
50  *   This module includes definitions for manipulating local Thread Network Data.
51  *
52  * @{
53  */
54 
55 namespace NetworkData {
56 
57 /**
58  * This class implements the Thread Network Data contributed by the local device.
59  *
60  */
61 class Local : public MutableNetworkData, private NonCopyable
62 {
63 public:
64     /**
65      * This constructor initializes the local Network Data.
66      *
67      * @param[in]  aInstance     A reference to the OpenThread instance.
68      *
69      */
Local(Instance & aInstance)70     explicit Local(Instance &aInstance)
71         : MutableNetworkData(aInstance, mTlvBuffer, 0, sizeof(mTlvBuffer))
72         , mOldRloc(Mac::kShortAddrInvalid)
73     {
74     }
75 
76 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
77     /**
78      * This method adds a Border Router entry to the Thread Network Data.
79      *
80      * @param[in]  aConfig  A reference to the on mesh prefix configuration.
81      *
82      * @retval kErrorNone         Successfully added the Border Router entry.
83      * @retval kErrorNoBufs       Insufficient space to add the Border Router entry.
84      * @retval kErrorInvalidArgs  The prefix is mesh local prefix.
85      *
86      */
87     Error AddOnMeshPrefix(const OnMeshPrefixConfig &aConfig);
88 
89     /**
90      * This method removes a Border Router entry from the Thread Network Data.
91      *
92      * @param[in]  aPrefix        The Prefix to remove.
93      *
94      * @retval kErrorNone       Successfully removed the Border Router entry.
95      * @retval kErrorNotFound   Could not find the Border Router entry.
96      *
97      */
RemoveOnMeshPrefix(const Ip6::Prefix & aPrefix)98     Error RemoveOnMeshPrefix(const Ip6::Prefix &aPrefix) { return RemovePrefix(aPrefix); }
99 
100     /**
101      * This method indicates whether or not the Thread Network Data contains a given on mesh prefix.
102      *
103      * @param[in]  aPrefix   The on mesh prefix to check.
104      *
105      * @retval TRUE  if Network Data contains mesh prefix @p aPrefix.
106      * @retval FALSE if Network Data does not contain mesh prefix @p aPrefix.
107      *
108      */
109     bool ContainsOnMeshPrefix(const Ip6::Prefix &aPrefix) const;
110 
111     /**
112      * This method adds a Has Route entry to the Thread Network data.
113      *
114      * @param[in]  aConfig       A reference to the external route configuration.
115      *
116      * @retval kErrorNone         Successfully added the Has Route entry.
117      * @retval kErrorInvalidArgs  One or more parameters in @p aConfig were invalid.
118      * @retval kErrorNoBufs       Insufficient space to add the Has Route entry.
119      *
120      */
121     Error AddHasRoutePrefix(const ExternalRouteConfig &aConfig);
122 
123     /**
124      * This method removes a Border Router entry from the Thread Network Data.
125      *
126      * @param[in]  aPrefix        The Prefix to remove.
127      *
128      * @retval kErrorNone       Successfully removed the Border Router entry.
129      * @retval kErrorNotFound   Could not find the Border Router entry.
130      *
131      */
RemoveHasRoutePrefix(const Ip6::Prefix & aPrefix)132     Error RemoveHasRoutePrefix(const Ip6::Prefix &aPrefix) { return RemovePrefix(aPrefix); }
133 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
134 
135 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
136     /**
137      * This method adds a Service entry to the Thread Network local data.
138      *
139      * @param[in]  aEnterpriseNumber  Enterprise Number (IANA-assigned) for Service TLV.
140      * @param[in]  aServiceData       The Service Data.
141      * @param[in]  aServerStable      The Stable flag value for Server TLV.
142      * @param[in]  aServerData        The Server Data.
143      *
144      * @retval kErrorNone     Successfully added the Service entry.
145      * @retval kErrorNoBufs   Insufficient space to add the Service entry.
146      *
147      */
148     Error AddService(uint32_t           aEnterpriseNumber,
149                      const ServiceData &aServiceData,
150                      bool               aServerStable,
151                      const ServerData & aServerData);
152 
153     /**
154      * This method removes a Service entry from the Thread Network local data.
155      *
156      * @param[in]  aEnterpriseNumber   Enterprise Number of the service to be deleted.
157      * @param[in]  aServiceData        The service data.
158      *
159      * @retval kErrorNone       Successfully removed the Service entry.
160      * @retval kErrorNotFound   Could not find the Service entry.
161      *
162      */
163     Error RemoveService(uint32_t aEnterpriseNumber, const ServiceData &aServiceData);
164 #endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
165 
166     /**
167      * This method sends a Server Data Notification message to the Leader.
168      *
169      * @param[in]  aHandler  A function pointer that is called when the transaction ends.
170      * @param[in]  aContext  A pointer to arbitrary context information.
171      *
172      * @retval kErrorNone          Successfully enqueued the notification message.
173      * @retval kErrorNoBufs        Insufficient message buffers to generate the notification message.
174      * @retval kErrorInvalidState  Device is a REED and is in the process of becoming a Router.
175      * @retval kErrorNotFound      Server Data is already consistent with network data.
176      *
177      */
178     Error UpdateInconsistentServerData(Coap::ResponseHandler aHandler, void *aContext);
179 
180 private:
181     void UpdateRloc(void);
182     bool IsConsistent(void) const;
183 
184 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
185     Error AddPrefix(const Ip6::Prefix &aPrefix, NetworkDataTlv::Type aSubTlvType, uint16_t aFlags, bool aStable);
186     Error RemovePrefix(const Ip6::Prefix &aPrefix);
187     void  UpdateRloc(PrefixTlv &aPrefixTlv);
188 #endif
189 
190 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
191     void UpdateRloc(ServiceTlv &aService);
192 #endif
193 
194     uint8_t  mTlvBuffer[kMaxSize];
195     uint16_t mOldRloc;
196 };
197 
198 } // namespace NetworkData
199 
200 /**
201  * @}
202  */
203 
204 } // namespace ot
205 
206 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
207 
208 #endif // NETWORK_DATA_LOCAL_HPP_
209