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  * This structure 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  * This structure 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  * This structure 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 } otExternalRouteConfig;
98 
99 /**
100  * Defines valid values for `mPreference` in `otExternalRouteConfig` and `otBorderRouterConfig`.
101  *
102  */
103 typedef enum otRoutePreference
104 {
105     OT_ROUTE_PREFERENCE_LOW  = -1, ///< Low route preference.
106     OT_ROUTE_PREFERENCE_MED  = 0,  ///< Medium route preference.
107     OT_ROUTE_PREFERENCE_HIGH = 1,  ///< High route preference.
108 } otRoutePreference;
109 
110 #define OT_SERVICE_DATA_MAX_SIZE 252 ///< Max size of Service Data in bytes.
111 #define OT_SERVER_DATA_MAX_SIZE 248  ///< Max size of Server Data in bytes. Theoretical limit, practically much lower.
112 
113 /**
114  * This structure represents a Server configuration.
115  *
116  */
117 typedef struct otServerConfig
118 {
119     bool     mStable : 1;                          ///< Whether this config is considered Stable Network Data.
120     uint8_t  mServerDataLength;                    ///< Length of server data.
121     uint8_t  mServerData[OT_SERVER_DATA_MAX_SIZE]; ///< Server data bytes.
122     uint16_t mRloc16;                              ///< The Server RLOC16.
123 } otServerConfig;
124 
125 /**
126  * This structure represents a Service configuration.
127  *
128  */
129 typedef struct otServiceConfig
130 {
131     uint8_t        mServiceId;                             ///< Service ID (when iterating over the  Network Data).
132     uint32_t       mEnterpriseNumber;                      ///< IANA Enterprise Number.
133     uint8_t        mServiceDataLength;                     ///< Length of service data.
134     uint8_t        mServiceData[OT_SERVICE_DATA_MAX_SIZE]; ///< Service data bytes.
135     otServerConfig mServerConfig;                          ///< The Server configuration.
136 } otServiceConfig;
137 
138 /**
139  * Provide full or stable copy of the Partition's Thread Network Data.
140  *
141  * @param[in]      aInstance    A pointer to an OpenThread instance.
142  * @param[in]      aStable      TRUE when copying the stable version, FALSE when copying the full version.
143  * @param[out]     aData        A pointer to the data buffer.
144  * @param[in,out]  aDataLength  On entry, size of the data buffer pointed to by @p aData.
145  *                              On exit, number of copied bytes.
146  *
147  * @retval OT_ERROR_NONE    Successfully copied the Thread Network Data into @p aData and updated @p aDataLength.
148  * @retval OT_ERROR_NO_BUFS Not enough space in @p aData to fully copy the Thread Network Data.
149  *
150  */
151 otError otNetDataGet(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength);
152 
153 /**
154  * Get the current length (number of bytes) of Partition's Thread Network Data.
155  *
156  * @param[in] aInstance    A pointer to an OpenThread instance.
157  *
158  * @return The length of the Network Data.
159  *
160  */
161 uint8_t otNetDataGetLength(otInstance *aInstance);
162 
163 /**
164  * Get the maximum observed length of the Thread Network Data since OT stack initialization or since the last call to
165  * `otNetDataResetMaxLength()`.
166  *
167  * @param[in] aInstance    A pointer to an OpenThread instance.
168  *
169  * @return The maximum length of the Network Data (high water mark for Network Data length).
170  *
171  */
172 uint8_t otNetDataGetMaxLength(otInstance *aInstance);
173 
174 /**
175  * Reset the tracked maximum length of the Thread Network Data.
176  *
177  * @param[in] aInstance    A pointer to an OpenThread instance.
178  *
179  * @sa otNetDataGetMaxLength
180  *
181  */
182 void otNetDataResetMaxLength(otInstance *aInstance);
183 
184 /**
185  * Get the next On Mesh Prefix in the partition's Network Data.
186  *
187  * @param[in]      aInstance  A pointer to an OpenThread instance.
188  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first on-mesh entry
189                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
190  * @param[out]     aConfig    A pointer to where the On Mesh Prefix information will be placed.
191  *
192  * @retval OT_ERROR_NONE       Successfully found the next On Mesh prefix.
193  * @retval OT_ERROR_NOT_FOUND  No subsequent On Mesh prefix exists in the Thread Network Data.
194  *
195  */
196 otError otNetDataGetNextOnMeshPrefix(otInstance            *aInstance,
197                                      otNetworkDataIterator *aIterator,
198                                      otBorderRouterConfig  *aConfig);
199 
200 /**
201  * Get the next external route in the partition's Network Data.
202  *
203  * @param[in]      aInstance  A pointer to an OpenThread instance.
204  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first external route entry
205                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
206  * @param[out]     aConfig    A pointer to where the External Route information will be placed.
207  *
208  * @retval OT_ERROR_NONE       Successfully found the next External Route.
209  * @retval OT_ERROR_NOT_FOUND  No subsequent external route entry exists in the Thread Network Data.
210  *
211  */
212 otError otNetDataGetNextRoute(otInstance *aInstance, otNetworkDataIterator *aIterator, otExternalRouteConfig *aConfig);
213 
214 /**
215  * Get the next service in the partition's Network Data.
216  *
217  * @param[in]      aInstance  A pointer to an OpenThread instance.
218  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first service entry
219                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
220  * @param[out]     aConfig    A pointer to where the service information will be placed.
221  *
222  * @retval OT_ERROR_NONE       Successfully found the next service.
223  * @retval OT_ERROR_NOT_FOUND  No subsequent service exists in the partition's Network Data.
224  *
225  */
226 otError otNetDataGetNextService(otInstance *aInstance, otNetworkDataIterator *aIterator, otServiceConfig *aConfig);
227 
228 /**
229  * Get the next 6LoWPAN Context ID info in the partition's Network Data.
230  *
231  * @param[in]      aInstance     A pointer to an OpenThread instance.
232  * @param[in,out]  aIterator     A pointer to the Network Data iterator. To get the first service entry
233                                  it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
234  * @param[out]     aContextInfo  A pointer to where the retrieved 6LoWPAN Context ID information will be placed.
235  *
236  * @retval OT_ERROR_NONE       Successfully found the next 6LoWPAN Context ID info.
237  * @retval OT_ERROR_NOT_FOUND  No subsequent 6LoWPAN Context info exists in the partition's Network Data.
238  *
239  */
240 otError otNetDataGetNextLowpanContextInfo(otInstance            *aInstance,
241                                           otNetworkDataIterator *aIterator,
242                                           otLowpanContextInfo   *aContextInfo);
243 
244 /**
245  * Get the Network Data Version.
246  *
247  * @param[in]  aInstance A pointer to an OpenThread instance.
248  *
249  * @returns The Network Data Version.
250  *
251  */
252 uint8_t otNetDataGetVersion(otInstance *aInstance);
253 
254 /**
255  * Get the Stable Network Data Version.
256  *
257  * @param[in]  aInstance A pointer to an OpenThread instance.
258  *
259  * @returns The Stable Network Data Version.
260  *
261  */
262 uint8_t otNetDataGetStableVersion(otInstance *aInstance);
263 
264 /**
265  * Check if the steering data includes a Joiner.
266  *
267  * @param[in]  aInstance          A pointer to an OpenThread instance.
268  * @param[in]  aEui64             A pointer to the Joiner's IEEE EUI-64.
269  *
270  * @retval OT_ERROR_NONE          @p aEui64 is included in the steering data.
271  * @retval OT_ERROR_INVALID_STATE No steering data present.
272  * @retval OT_ERROR_NOT_FOUND     @p aEui64 is not included in the steering data.
273  *
274  */
275 otError otNetDataSteeringDataCheckJoiner(otInstance *aInstance, const otExtAddress *aEui64);
276 
277 // Forward declaration
278 struct otJoinerDiscerner;
279 
280 /**
281  * Check if the steering data includes a Joiner with a given discerner value.
282  *
283  * @param[in]  aInstance          A pointer to an OpenThread instance.
284  * @param[in]  aDiscerner         A pointer to the Joiner Discerner.
285  *
286  * @retval OT_ERROR_NONE          @p aDiscerner is included in the steering data.
287  * @retval OT_ERROR_INVALID_STATE No steering data present.
288  * @retval OT_ERROR_NOT_FOUND     @p aDiscerner is not included in the steering data.
289  *
290  */
291 otError otNetDataSteeringDataCheckJoinerWithDiscerner(otInstance                     *aInstance,
292                                                       const struct otJoinerDiscerner *aDiscerner);
293 
294 /**
295  * Check whether a given Prefix can act as a valid OMR prefix and also the Leader's Network Data contains this prefix.
296  *
297  * @param[in]  aInstance  A pointer to an OpenThread instance.
298  * @param[in]  aPrefix    A pointer to the IPv6 prefix.
299  *
300  * @returns  Whether @p aPrefix is a valid OMR prefix and Leader's Network Data contains the OMR prefix @p aPrefix.
301  *
302  * @note This API is only available when `OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE` is used.
303  *
304  */
305 bool otNetDataContainsOmrPrefix(otInstance *aInstance, const otIp6Prefix *aPrefix);
306 
307 /**
308  * @}
309  *
310  */
311 
312 #ifdef __cplusplus
313 } // extern "C"
314 #endif
315 
316 #endif // OPENTHREAD_NETDATA_H_
317