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 an External Route configuration.
76  *
77  */
78 typedef struct otExternalRouteConfig
79 {
80     otIp6Prefix mPrefix;                  ///< The IPv6 prefix.
81     uint16_t    mRloc16;                  ///< The border router's RLOC16 (value ignored on config add).
82     signed int  mPreference : 2;          ///< A 2-bit signed int preference (`OT_ROUTE_PREFERENCE_*` values).
83     bool        mNat64 : 1;               ///< Whether this is a NAT64 prefix.
84     bool        mStable : 1;              ///< Whether this configuration is considered Stable Network Data.
85     bool        mNextHopIsThisDevice : 1; ///< Whether the next hop is this device (value ignored on config add).
86 } otExternalRouteConfig;
87 
88 /**
89  * Defines valid values for `mPreference` in `otExternalRouteConfig` and `otBorderRouterConfig`.
90  *
91  */
92 typedef enum otRoutePreference
93 {
94     OT_ROUTE_PREFERENCE_LOW  = -1, ///< Low route preference.
95     OT_ROUTE_PREFERENCE_MED  = 0,  ///< Medium route preference.
96     OT_ROUTE_PREFERENCE_HIGH = 1,  ///< High route preference.
97 } otRoutePreference;
98 
99 #define OT_SERVICE_DATA_MAX_SIZE 252 ///< Max size of Service Data in bytes.
100 #define OT_SERVER_DATA_MAX_SIZE 248  ///< Max size of Server Data in bytes. Theoretical limit, practically much lower.
101 
102 /**
103  * This structure represents a Server configuration.
104  *
105  */
106 typedef struct otServerConfig
107 {
108     bool     mStable : 1;                          ///< Whether this config is considered Stable Network Data.
109     uint8_t  mServerDataLength;                    ///< Length of server data.
110     uint8_t  mServerData[OT_SERVER_DATA_MAX_SIZE]; ///< Server data bytes.
111     uint16_t mRloc16;                              ///< The Server RLOC16.
112 } otServerConfig;
113 
114 /**
115  * This structure represents a Service configuration.
116  *
117  */
118 typedef struct otServiceConfig
119 {
120     uint8_t        mServiceId;                             ///< Service ID (when iterating over the  Network Data).
121     uint32_t       mEnterpriseNumber;                      ///< IANA Enterprise Number.
122     uint8_t        mServiceDataLength;                     ///< Length of service data.
123     uint8_t        mServiceData[OT_SERVICE_DATA_MAX_SIZE]; ///< Service data bytes.
124     otServerConfig mServerConfig;                          ///< The Server configuration.
125 } otServiceConfig;
126 
127 /**
128  * This method provides a full or stable copy of the Partition's Thread Network Data.
129  *
130  * @param[in]     aInstance    A pointer to an OpenThread instance.
131  * @param[in]     aStable      TRUE when copying the stable version, FALSE when copying the full version.
132  * @param[out]    aData        A pointer to the data buffer.
133  * @param[inout]  aDataLength  On entry, size of the data buffer pointed to by @p aData.
134  *                             On exit, number of copied bytes.
135  *
136  */
137 otError otNetDataGet(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength);
138 
139 /**
140  * This function gets the next On Mesh Prefix in the partition's Network Data.
141  *
142  * @param[in]     aInstance  A pointer to an OpenThread instance.
143  * @param[inout]  aIterator  A pointer to the Network Data iterator context. To get the first on-mesh entry
144                              it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
145  * @param[out]    aConfig    A pointer to where the On Mesh Prefix information will be placed.
146  *
147  * @retval OT_ERROR_NONE       Successfully found the next On Mesh prefix.
148  * @retval OT_ERROR_NOT_FOUND  No subsequent On Mesh prefix exists in the Thread Network Data.
149  *
150  */
151 otError otNetDataGetNextOnMeshPrefix(otInstance *           aInstance,
152                                      otNetworkDataIterator *aIterator,
153                                      otBorderRouterConfig * aConfig);
154 
155 /**
156  * This function gets the next external route in the partition's Network Data.
157  *
158  * @param[in]     aInstance  A pointer to an OpenThread instance.
159  * @param[inout]  aIterator  A pointer to the Network Data iterator context. To get the first external route entry
160                              it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
161  * @param[out]    aConfig    A pointer to where the External Route information will be placed.
162  *
163  * @retval OT_ERROR_NONE       Successfully found the next External Route.
164  * @retval OT_ERROR_NOT_FOUND  No subsequent external route entry exists in the Thread Network Data.
165  *
166  */
167 otError otNetDataGetNextRoute(otInstance *aInstance, otNetworkDataIterator *aIterator, otExternalRouteConfig *aConfig);
168 
169 /**
170  * This function gets the next service in the partition's Network Data.
171  *
172  * @param[in]     aInstance  A pointer to an OpenThread instance.
173  * @param[inout]  aIterator  A pointer to the Network Data iterator context. To get the first service entry
174                              it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
175  * @param[out]    aConfig    A pointer to where the service information will be placed.
176  *
177  * @retval OT_ERROR_NONE       Successfully found the next service.
178  * @retval OT_ERROR_NOT_FOUND  No subsequent service exists in the partition's Network Data.
179  *
180  */
181 otError otNetDataGetNextService(otInstance *aInstance, otNetworkDataIterator *aIterator, otServiceConfig *aConfig);
182 
183 /**
184  * Get the Network Data Version.
185  *
186  * @param[in]  aInstance A pointer to an OpenThread instance.
187  *
188  * @returns The Network Data Version.
189  *
190  */
191 uint8_t otNetDataGetVersion(otInstance *aInstance);
192 
193 /**
194  * Get the Stable Network Data Version.
195  *
196  * @param[in]  aInstance A pointer to an OpenThread instance.
197  *
198  * @returns The Stable Network Data Version.
199  *
200  */
201 uint8_t otNetDataGetStableVersion(otInstance *aInstance);
202 
203 /**
204  * Check if the steering data includes a Joiner.
205  *
206  * @param[in]  aInstance          A pointer to an OpenThread instance.
207  * @param[in]  aEui64             A pointer to the Joiner's IEEE EUI-64.
208  *
209  * @retval OT_ERROR_NONE          @p aEui64 is included in the steering data.
210  * @retval OT_ERROR_INVALID_STATE No steering data present.
211  * @retval OT_ERROR_NOT_FOUND     @p aEui64 is not included in the steering data.
212  *
213  */
214 otError otNetDataSteeringDataCheckJoiner(otInstance *aInstance, const otExtAddress *aEui64);
215 
216 // Forward declaration
217 struct otJoinerDiscerner;
218 
219 /**
220  * Check if the steering data includes a Joiner with a given discerner value.
221  *
222  * @param[in]  aInstance          A pointer to an OpenThread instance.
223  * @param[in]  aDiscerner         A pointer to the Joiner Discerner.
224  *
225  * @retval OT_ERROR_NONE          @p aDiscerner is included in the steering data.
226  * @retval OT_ERROR_INVALID_STATE No steering data present.
227  * @retval OT_ERROR_NOT_FOUND     @p aDiscerner is not included in the steering data.
228  *
229  */
230 otError otNetDataSteeringDataCheckJoinerWithDiscerner(otInstance *                    aInstance,
231                                                       const struct otJoinerDiscerner *aDiscerner);
232 
233 /**
234  * @}
235  *
236  */
237 
238 #ifdef __cplusplus
239 } // extern "C"
240 #endif
241 
242 #endif // OPENTHREAD_NETDATA_H_
243