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