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 class Notifier;
58 
59 /**
60  * This class implements the Thread Network Data contributed by the local device.
61  *
62  */
63 class Local : public MutableNetworkData, private NonCopyable
64 {
65     friend class Notifier;
66 
67 public:
68     /**
69      * This constructor initializes the local Network Data.
70      *
71      * @param[in]  aInstance     A reference to the OpenThread instance.
72      *
73      */
Local(Instance & aInstance)74     explicit Local(Instance &aInstance)
75         : MutableNetworkData(aInstance, mTlvBuffer, 0, sizeof(mTlvBuffer))
76     {
77     }
78 
79 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
80     /**
81      * This method adds a Border Router entry to the Thread Network Data.
82      *
83      * @param[in]  aConfig  A reference to the on mesh prefix configuration.
84      *
85      * @retval kErrorNone         Successfully added the Border Router entry.
86      * @retval kErrorNoBufs       Insufficient space to add the Border Router entry.
87      * @retval kErrorInvalidArgs  The prefix is mesh local prefix.
88      *
89      */
90     Error AddOnMeshPrefix(const OnMeshPrefixConfig &aConfig);
91 
92     /**
93      * This method removes a Border Router entry from the Thread Network Data.
94      *
95      * @param[in]  aPrefix        The Prefix to remove.
96      *
97      * @retval kErrorNone       Successfully removed the Border Router entry.
98      * @retval kErrorNotFound   Could not find the Border Router entry.
99      *
100      */
RemoveOnMeshPrefix(const Ip6::Prefix & aPrefix)101     Error RemoveOnMeshPrefix(const Ip6::Prefix &aPrefix) { return RemovePrefix(aPrefix); }
102 
103     /**
104      * This method indicates whether or not the Thread Network Data contains a given on mesh prefix.
105      *
106      * @param[in]  aPrefix   The on mesh prefix to check.
107      *
108      * @retval TRUE  if Network Data contains mesh prefix @p aPrefix.
109      * @retval FALSE if Network Data does not contain mesh prefix @p aPrefix.
110      *
111      */
112     bool ContainsOnMeshPrefix(const Ip6::Prefix &aPrefix) const;
113 
114     /**
115      * This method adds a Has Route entry to the Thread Network data.
116      *
117      * @param[in]  aConfig       A reference to the external route configuration.
118      *
119      * @retval kErrorNone         Successfully added the Has Route entry.
120      * @retval kErrorInvalidArgs  One or more parameters in @p aConfig were invalid.
121      * @retval kErrorNoBufs       Insufficient space to add the Has Route entry.
122      *
123      */
124     Error AddHasRoutePrefix(const ExternalRouteConfig &aConfig);
125 
126     /**
127      * This method removes a Border Router entry from the Thread Network Data.
128      *
129      * @param[in]  aPrefix        The Prefix to remove.
130      *
131      * @retval kErrorNone       Successfully removed the Border Router entry.
132      * @retval kErrorNotFound   Could not find the Border Router entry.
133      *
134      */
RemoveHasRoutePrefix(const Ip6::Prefix & aPrefix)135     Error RemoveHasRoutePrefix(const Ip6::Prefix &aPrefix) { return RemovePrefix(aPrefix); }
136 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
137 
138 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
139     /**
140      * This method adds a Service entry to the Thread Network local data.
141      *
142      * @param[in]  aEnterpriseNumber  Enterprise Number (IANA-assigned) for Service TLV.
143      * @param[in]  aServiceData       The Service Data.
144      * @param[in]  aServerStable      The Stable flag value for Server TLV.
145      * @param[in]  aServerData        The Server Data.
146      *
147      * @retval kErrorNone     Successfully added the Service entry.
148      * @retval kErrorNoBufs   Insufficient space to add the Service entry.
149      *
150      */
151     Error AddService(uint32_t           aEnterpriseNumber,
152                      const ServiceData &aServiceData,
153                      bool               aServerStable,
154                      const ServerData  &aServerData);
155 
156     /**
157      * This method removes a Service entry from the Thread Network local data.
158      *
159      * @param[in]  aEnterpriseNumber   Enterprise Number of the service to be deleted.
160      * @param[in]  aServiceData        The service data.
161      *
162      * @retval kErrorNone       Successfully removed the Service entry.
163      * @retval kErrorNotFound   Could not find the Service entry.
164      *
165      */
166     Error RemoveService(uint32_t aEnterpriseNumber, const ServiceData &aServiceData);
167 #endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
168 
169 private:
170     void UpdateRloc(void);
171 
172 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
173     Error AddPrefix(const Ip6::Prefix &aPrefix, NetworkDataTlv::Type aSubTlvType, uint16_t aFlags, bool aStable);
174     Error RemovePrefix(const Ip6::Prefix &aPrefix);
175     void  UpdateRloc(PrefixTlv &aPrefixTlv);
176 #endif
177 
178 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
179     void UpdateRloc(ServiceTlv &aService);
180 #endif
181 
182     uint8_t mTlvBuffer[kMaxSize];
183 };
184 
185 } // namespace NetworkData
186 
187 /**
188  * @}
189  */
190 
191 } // namespace ot
192 
193 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
194 
195 #endif // NETWORK_DATA_LOCAL_HPP_
196