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  * @brief
32  *  This file defines the OpenThread Network Data API.
33  */
34 
35 #ifndef OPENTHREAD_NETDATA_H_
36 #define OPENTHREAD_NETDATA_H_
37 
38 #include <openthread/ip6.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @addtogroup api-thread-general
46  *
47  * @{
48  *
49  */
50 
51 #define OT_NETWORK_DATA_ITERATOR_INIT 0 ///< Value to initialize `otNetworkDataIterator`.
52 
53 typedef uint32_t otNetworkDataIterator; ///< Used to iterate through Network Data information.
54 
55 /**
56  * Represents a Border Router configuration.
57  */
58 typedef struct otBorderRouterConfig
59 {
60     otIp6Prefix mPrefix;           ///< The IPv6 prefix.
61     signed int  mPreference : 2;   ///< A 2-bit signed int preference (`OT_ROUTE_PREFERENCE_*` values).
62     bool        mPreferred : 1;    ///< Whether prefix is preferred.
63     bool        mSlaac : 1;        ///< Whether prefix can be used for address auto-configuration (SLAAC).
64     bool        mDhcp : 1;         ///< Whether border router is DHCPv6 Agent.
65     bool        mConfigure : 1;    ///< Whether DHCPv6 Agent supplying other config data.
66     bool        mDefaultRoute : 1; ///< Whether border router is a default router for prefix.
67     bool        mOnMesh : 1;       ///< Whether this prefix is considered on-mesh.
68     bool        mStable : 1;       ///< Whether this configuration is considered Stable Network Data.
69     bool        mNdDns : 1;        ///< Whether this border router can supply DNS information via ND.
70     bool        mDp : 1;           ///< Whether prefix is a Thread Domain Prefix (added since Thread 1.2).
71     uint16_t    mRloc16;           ///< The border router's RLOC16 (value ignored on config add).
72 } otBorderRouterConfig;
73 
74 /**
75  * Represents 6LoWPAN Context ID information associated with a prefix in Network Data.
76  *
77  */
78 typedef struct otLowpanContextInfo
79 {
80     uint8_t     mContextId;    ///< The 6LoWPAN Context ID.
81     bool        mCompressFlag; ///< The compress flag.
82     otIp6Prefix mPrefix;       ///< The associated IPv6 prefix.
83 } otLowpanContextInfo;
84 
85 /**
86  * Represents an External Route configuration.
87  *
88  */
89 typedef struct otExternalRouteConfig
90 {
91     otIp6Prefix mPrefix;                  ///< The IPv6 prefix.
92     uint16_t    mRloc16;                  ///< The border router's RLOC16 (value ignored on config add).
93     signed int  mPreference : 2;          ///< A 2-bit signed int preference (`OT_ROUTE_PREFERENCE_*` values).
94     bool        mNat64 : 1;               ///< Whether this is a NAT64 prefix.
95     bool        mStable : 1;              ///< Whether this configuration is considered Stable Network Data.
96     bool        mNextHopIsThisDevice : 1; ///< Whether the next hop is this device (value ignored on config add).
97     bool        mAdvPio : 1;              ///< Whether or not BR is advertising a ULA prefix in PIO (AP flag).
98 } otExternalRouteConfig;
99 
100 /**
101  * Defines valid values for `mPreference` in `otExternalRouteConfig` and `otBorderRouterConfig`.
102  *
103  */
104 typedef enum otRoutePreference
105 {
106     OT_ROUTE_PREFERENCE_LOW  = -1, ///< Low route preference.
107     OT_ROUTE_PREFERENCE_MED  = 0,  ///< Medium route preference.
108     OT_ROUTE_PREFERENCE_HIGH = 1,  ///< High route preference.
109 } otRoutePreference;
110 
111 #define OT_SERVICE_DATA_MAX_SIZE 252 ///< Max size of Service Data in bytes.
112 #define OT_SERVER_DATA_MAX_SIZE 248  ///< Max size of Server Data in bytes. Theoretical limit, practically much lower.
113 
114 /**
115  * Represents a Server configuration.
116  *
117  */
118 typedef struct otServerConfig
119 {
120     bool     mStable : 1;                          ///< Whether this config is considered Stable Network Data.
121     uint8_t  mServerDataLength;                    ///< Length of server data.
122     uint8_t  mServerData[OT_SERVER_DATA_MAX_SIZE]; ///< Server data bytes.
123     uint16_t mRloc16;                              ///< The Server RLOC16.
124 } otServerConfig;
125 
126 /**
127  * Represents a Service configuration.
128  *
129  */
130 typedef struct otServiceConfig
131 {
132     uint8_t        mServiceId;                             ///< Service ID (when iterating over the  Network Data).
133     uint32_t       mEnterpriseNumber;                      ///< IANA Enterprise Number.
134     uint8_t        mServiceDataLength;                     ///< Length of service data.
135     uint8_t        mServiceData[OT_SERVICE_DATA_MAX_SIZE]; ///< Service data bytes.
136     otServerConfig mServerConfig;                          ///< The Server configuration.
137 } otServiceConfig;
138 
139 /**
140  * Provide full or stable copy of the Partition's Thread Network Data.
141  *
142  * @param[in]      aInstance    A pointer to an OpenThread instance.
143  * @param[in]      aStable      TRUE when copying the stable version, FALSE when copying the full version.
144  * @param[out]     aData        A pointer to the data buffer.
145  * @param[in,out]  aDataLength  On entry, size of the data buffer pointed to by @p aData.
146  *                              On exit, number of copied bytes.
147  *
148  * @retval OT_ERROR_NONE    Successfully copied the Thread Network Data into @p aData and updated @p aDataLength.
149  * @retval OT_ERROR_NO_BUFS Not enough space in @p aData to fully copy the Thread Network Data.
150  *
151  */
152 otError otNetDataGet(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength);
153 
154 /**
155  * Get the current length (number of bytes) of Partition's Thread Network Data.
156  *
157  * @param[in] aInstance    A pointer to an OpenThread instance.
158  *
159  * @return The length of the Network Data.
160  *
161  */
162 uint8_t otNetDataGetLength(otInstance *aInstance);
163 
164 /**
165  * Get the maximum observed length of the Thread Network Data since OT stack initialization or since the last call to
166  * `otNetDataResetMaxLength()`.
167  *
168  * @param[in] aInstance    A pointer to an OpenThread instance.
169  *
170  * @return The maximum length of the Network Data (high water mark for Network Data length).
171  *
172  */
173 uint8_t otNetDataGetMaxLength(otInstance *aInstance);
174 
175 /**
176  * Reset the tracked maximum length of the Thread Network Data.
177  *
178  * @param[in] aInstance    A pointer to an OpenThread instance.
179  *
180  * @sa otNetDataGetMaxLength
181  *
182  */
183 void otNetDataResetMaxLength(otInstance *aInstance);
184 
185 /**
186  * Get the next On Mesh Prefix in the partition's Network Data.
187  *
188  * @param[in]      aInstance  A pointer to an OpenThread instance.
189  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first on-mesh entry
190                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
191  * @param[out]     aConfig    A pointer to where the On Mesh Prefix information will be placed.
192  *
193  * @retval OT_ERROR_NONE       Successfully found the next On Mesh prefix.
194  * @retval OT_ERROR_NOT_FOUND  No subsequent On Mesh prefix exists in the Thread Network Data.
195  *
196  */
197 otError otNetDataGetNextOnMeshPrefix(otInstance            *aInstance,
198                                      otNetworkDataIterator *aIterator,
199                                      otBorderRouterConfig  *aConfig);
200 
201 /**
202  * Get the next external route in the partition's Network Data.
203  *
204  * @param[in]      aInstance  A pointer to an OpenThread instance.
205  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first external route entry
206                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
207  * @param[out]     aConfig    A pointer to where the External Route information will be placed.
208  *
209  * @retval OT_ERROR_NONE       Successfully found the next External Route.
210  * @retval OT_ERROR_NOT_FOUND  No subsequent external route entry exists in the Thread Network Data.
211  *
212  */
213 otError otNetDataGetNextRoute(otInstance *aInstance, otNetworkDataIterator *aIterator, otExternalRouteConfig *aConfig);
214 
215 /**
216  * Get the next service in the partition's Network Data.
217  *
218  * @param[in]      aInstance  A pointer to an OpenThread instance.
219  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first service entry
220                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
221  * @param[out]     aConfig    A pointer to where the service information will be placed.
222  *
223  * @retval OT_ERROR_NONE       Successfully found the next service.
224  * @retval OT_ERROR_NOT_FOUND  No subsequent service exists in the partition's Network Data.
225  *
226  */
227 otError otNetDataGetNextService(otInstance *aInstance, otNetworkDataIterator *aIterator, otServiceConfig *aConfig);
228 
229 /**
230  * Get the next 6LoWPAN Context ID info in the partition's Network Data.
231  *
232  * @param[in]      aInstance     A pointer to an OpenThread instance.
233  * @param[in,out]  aIterator     A pointer to the Network Data iterator. To get the first service entry
234                                  it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
235  * @param[out]     aContextInfo  A pointer to where the retrieved 6LoWPAN Context ID information will be placed.
236  *
237  * @retval OT_ERROR_NONE       Successfully found the next 6LoWPAN Context ID info.
238  * @retval OT_ERROR_NOT_FOUND  No subsequent 6LoWPAN Context info exists in the partition's Network Data.
239  *
240  */
241 otError otNetDataGetNextLowpanContextInfo(otInstance            *aInstance,
242                                           otNetworkDataIterator *aIterator,
243                                           otLowpanContextInfo   *aContextInfo);
244 
245 /**
246  * Get the Network Data Version.
247  *
248  * @param[in]  aInstance A pointer to an OpenThread instance.
249  *
250  * @returns The Network Data Version.
251  *
252  */
253 uint8_t otNetDataGetVersion(otInstance *aInstance);
254 
255 /**
256  * Get the Stable Network Data Version.
257  *
258  * @param[in]  aInstance A pointer to an OpenThread instance.
259  *
260  * @returns The Stable Network Data Version.
261  *
262  */
263 uint8_t otNetDataGetStableVersion(otInstance *aInstance);
264 
265 /**
266  * Check if the steering data includes a Joiner.
267  *
268  * @param[in]  aInstance          A pointer to an OpenThread instance.
269  * @param[in]  aEui64             A pointer to the Joiner's IEEE EUI-64.
270  *
271  * @retval OT_ERROR_NONE          @p aEui64 is included in the steering data.
272  * @retval OT_ERROR_INVALID_STATE No steering data present.
273  * @retval OT_ERROR_NOT_FOUND     @p aEui64 is not included in the steering data.
274  *
275  */
276 otError otNetDataSteeringDataCheckJoiner(otInstance *aInstance, const otExtAddress *aEui64);
277 
278 // Forward declaration
279 struct otJoinerDiscerner;
280 
281 /**
282  * Check if the steering data includes a Joiner with a given discerner value.
283  *
284  * @param[in]  aInstance          A pointer to an OpenThread instance.
285  * @param[in]  aDiscerner         A pointer to the Joiner Discerner.
286  *
287  * @retval OT_ERROR_NONE          @p aDiscerner is included in the steering data.
288  * @retval OT_ERROR_INVALID_STATE No steering data present.
289  * @retval OT_ERROR_NOT_FOUND     @p aDiscerner is not included in the steering data.
290  *
291  */
292 otError otNetDataSteeringDataCheckJoinerWithDiscerner(otInstance                     *aInstance,
293                                                       const struct otJoinerDiscerner *aDiscerner);
294 
295 /**
296  * Check whether a given Prefix can act as a valid OMR prefix and also the Leader's Network Data contains this prefix.
297  *
298  * @param[in]  aInstance  A pointer to an OpenThread instance.
299  * @param[in]  aPrefix    A pointer to the IPv6 prefix.
300  *
301  * @returns  Whether @p aPrefix is a valid OMR prefix and Leader's Network Data contains the OMR prefix @p aPrefix.
302  *
303  * @note This API is only available when `OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE` is used.
304  *
305  */
306 bool otNetDataContainsOmrPrefix(otInstance *aInstance, const otIp6Prefix *aPrefix);
307 
308 /**
309  * @}
310  *
311  */
312 
313 #ifdef __cplusplus
314 } // extern "C"
315 #endif
316 
317 #endif // OPENTHREAD_NETDATA_H_
318