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