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 NetworkData, private NonCopyable
62 {
63 public:
64     /**
65      * This constructor initializes the local Network Data.
66      *
67      * @param[in]  aNetif  A reference to the Thread network interface.
68      *
69      */
70     explicit Local(Instance &aInstance);
71 
72 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
73     /**
74      * This method adds a Border Router entry to the Thread Network Data.
75      *
76      * @param[in]  aConfig  A reference to the on mesh prefix configuration.
77      *
78      * @retval kErrorNone         Successfully added the Border Router entry.
79      * @retval kErrorNoBufs       Insufficient space to add the Border Router entry.
80      * @retval kErrorInvalidArgs  The prefix is mesh local prefix.
81      *
82      */
83     Error AddOnMeshPrefix(const OnMeshPrefixConfig &aConfig);
84 
85     /**
86      * This method removes a Border Router entry from the Thread Network Data.
87      *
88      * @param[in]  aPrefix        The Prefix to remove.
89      *
90      * @retval kErrorNone       Successfully removed the Border Router entry.
91      * @retval kErrorNotFound   Could not find the Border Router entry.
92      *
93      */
94     Error RemoveOnMeshPrefix(const Ip6::Prefix &aPrefix);
95 
96     /**
97      * This method adds a Has Route entry to the Thread Network data.
98      *
99      * @param[in]  aConfig       A reference to the external route configuration.
100      *
101      * @retval kErrorNone         Successfully added the Has Route entry.
102      * @retval kErrorInvalidArgs  One or more parameters in @p aConfig were invalid.
103      * @retval kErrorNoBufs       Insufficient space to add the Has Route entry.
104      *
105      */
106     Error AddHasRoutePrefix(const ExternalRouteConfig &aConfig);
107 
108     /**
109      * This method removes a Border Router entry from the Thread Network Data.
110      *
111      * @param[in]  aPrefix        The Prefix to remove.
112      *
113      * @retval kErrorNone       Successfully removed the Border Router entry.
114      * @retval kErrorNotFound   Could not find the Border Router entry.
115      *
116      */
117     Error RemoveHasRoutePrefix(const Ip6::Prefix &aPrefix);
118 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
119 
120 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
121     /**
122      * This method adds a Service entry to the Thread Network local data.
123      *
124      * @param[in]  aEnterpriseNumber  Enterprise Number (IANA-assigned) for Service TLV
125      * @param[in]  aServiceData       A pointer to the Service Data
126      * @param[in]  aServiceDataLength The length of @p aServiceData in bytes.
127      * @param[in]  aServerStable      The Stable flag value for Server TLV
128      * @param[in]  aServerData        A pointer to the Server Data
129      * @param[in]  aServerDataLength  The length of @p aServerData in bytes.
130      *
131      * @retval kErrorNone     Successfully added the Service entry.
132      * @retval kErrorNoBufs   Insufficient space to add the Service entry.
133      *
134      */
135     Error AddService(uint32_t       aEnterpriseNumber,
136                      const uint8_t *aServiceData,
137                      uint8_t        aServiceDataLength,
138                      bool           aServerStable,
139                      const uint8_t *aServerData,
140                      uint8_t        aServerDataLength);
141 
142     /**
143      * This method removes a Service entry from the Thread Network local data.
144      *
145      * @param[in]  aEnterpriseNumber   Enterprise Number of the service to be deleted.
146      * @param[in]  aServiceData        A pointer to the service data.
147      * @param[in]  aServiceDataLength  The length of @p aServiceData in bytes.
148      *
149      * @retval kErrorNone       Successfully removed the Service entry.
150      * @retval kErrorNotFound   Could not find the Service entry.
151      *
152      */
153     Error RemoveService(uint32_t aEnterpriseNumber, const uint8_t *aServiceData, uint8_t aServiceDataLength);
154 #endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
155 
156     /**
157      * This method sends a Server Data Notification message to the Leader.
158      *
159      * @param[in]  aHandler  A function pointer that is called when the transaction ends.
160      * @param[in]  aContext  A pointer to arbitrary context information.
161      *
162      * @retval kErrorNone          Successfully enqueued the notification message.
163      * @retval kErrorNoBufs        Insufficient message buffers to generate the notification message.
164      * @retval kErrorInvalidState  Device is a REED and is in the process of becoming a Router.
165      * @retval kErrorNotFound      Server Data is already consistent with network data.
166      *
167      */
168     Error UpdateInconsistentServerData(Coap::ResponseHandler aHandler, void *aContext);
169 
170 private:
171     void UpdateRloc(void);
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, NetworkDataTlv::Type aSubTlvType);
175     void  UpdateRloc(PrefixTlv &aPrefixTlv);
176     bool  IsOnMeshPrefixConsistent(void) const;
177     bool  IsExternalRouteConsistent(void) const;
178 #endif
179 
180 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
181     void UpdateRloc(ServiceTlv &aService);
182     bool IsServiceConsistent(void) const;
183 #endif
184 
185     uint16_t mOldRloc;
186 };
187 
188 } // namespace NetworkData
189 
190 /**
191  * @}
192  */
193 
194 } // namespace ot
195 
196 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
197 
198 #endif // NETWORK_DATA_LOCAL_HPP_
199