1 /* 2 * Copyright (c) 2020, 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 Diagnostic API. 33 */ 34 35 #ifndef OPENTHREAD_NETDIAG_H_ 36 #define OPENTHREAD_NETDIAG_H_ 37 38 #include <openthread/ip6.h> 39 #include <openthread/thread.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_DIAGNOSTIC_TLV_EXT_ADDRESS 0 ///< MAC Extended Address TLV 52 #define OT_NETWORK_DIAGNOSTIC_TLV_SHORT_ADDRESS 1 ///< Address16 TLV 53 #define OT_NETWORK_DIAGNOSTIC_TLV_MODE 2 ///< Mode TLV 54 #define OT_NETWORK_DIAGNOSTIC_TLV_TIMEOUT 3 ///< Timeout TLV (max polling time period for SEDs) 55 #define OT_NETWORK_DIAGNOSTIC_TLV_CONNECTIVITY 4 ///< Connectivity TLV 56 #define OT_NETWORK_DIAGNOSTIC_TLV_ROUTE 5 ///< Route64 TLV 57 #define OT_NETWORK_DIAGNOSTIC_TLV_LEADER_DATA 6 ///< Leader Data TLV 58 #define OT_NETWORK_DIAGNOSTIC_TLV_NETWORK_DATA 7 ///< Network Data TLV 59 #define OT_NETWORK_DIAGNOSTIC_TLV_IP6_ADDR_LIST 8 ///< IPv6 Address List TLV 60 #define OT_NETWORK_DIAGNOSTIC_TLV_MAC_COUNTERS 9 ///< MAC Counters TLV 61 #define OT_NETWORK_DIAGNOSTIC_TLV_BATTERY_LEVEL 14 ///< Battery Level TLV 62 #define OT_NETWORK_DIAGNOSTIC_TLV_SUPPLY_VOLTAGE 15 ///< Supply Voltage TLV 63 #define OT_NETWORK_DIAGNOSTIC_TLV_CHILD_TABLE 16 ///< Child Table TLV 64 #define OT_NETWORK_DIAGNOSTIC_TLV_CHANNEL_PAGES 17 ///< Channel Pages TLV 65 #define OT_NETWORK_DIAGNOSTIC_TLV_TYPE_LIST 18 ///< Type List TLV 66 #define OT_NETWORK_DIAGNOSTIC_TLV_MAX_CHILD_TIMEOUT 19 ///< Max Child Timeout TLV 67 #define OT_NETWORK_DIAGNOSTIC_TLV_EUI64 23 ///< EUI64 TLV 68 #define OT_NETWORK_DIAGNOSTIC_TLV_VERSION 24 ///< Thread Version TLV 69 #define OT_NETWORK_DIAGNOSTIC_TLV_VENDOR_NAME 25 ///< Vendor Name TLV 70 #define OT_NETWORK_DIAGNOSTIC_TLV_VENDOR_MODEL 26 ///< Vendor Model TLV 71 #define OT_NETWORK_DIAGNOSTIC_TLV_VENDOR_SW_VERSION 27 ///< Vendor SW Version TLV 72 #define OT_NETWORK_DIAGNOSTIC_TLV_THREAD_STACK_VERSION 28 ///< Thread Stack Version TLV (codebase/commit version) 73 #define OT_NETWORK_DIAGNOSTIC_TLV_CHILD 29 ///< Child TLV 74 #define OT_NETWORK_DIAGNOSTIC_TLV_CHILD_IP6_ADDR_LIST 30 ///< Child IPv6 Address List TLV 75 #define OT_NETWORK_DIAGNOSTIC_TLV_ROUTER_NEIGHBOR 31 ///< Router Neighbor TLV 76 #define OT_NETWORK_DIAGNOSTIC_TLV_ANSWER 32 ///< Answer TLV 77 #define OT_NETWORK_DIAGNOSTIC_TLV_QUERY_ID 33 ///< Query ID TLV 78 #define OT_NETWORK_DIAGNOSTIC_TLV_MLE_COUNTERS 34 ///< MLE Counters TLV 79 #define OT_NETWORK_DIAGNOSTIC_TLV_VENDOR_APP_URL 35 ///< Vendor App URL TLV 80 81 #define OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_NAME_TLV_LENGTH 32 ///< Max length of Vendor Name TLV. 82 #define OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_MODEL_TLV_LENGTH 32 ///< Max length of Vendor Model TLV. 83 #define OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_SW_VERSION_TLV_LENGTH 16 ///< Max length of Vendor SW Version TLV. 84 #define OT_NETWORK_DIAGNOSTIC_MAX_THREAD_STACK_VERSION_TLV_LENGTH 64 ///< Max length of Thread Stack Version TLV. 85 #define OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_APP_URL_TLV_LENGTH 96 ///< Max length of Vendor App URL TLV. 86 87 #define OT_NETWORK_DIAGNOSTIC_ITERATOR_INIT 0 ///< Initializer for `otNetworkDiagIterator`. 88 89 typedef uint16_t otNetworkDiagIterator; ///< Used to iterate through Network Diagnostic TLV. 90 91 /** 92 * Represents a Network Diagnostic Connectivity value. 93 */ 94 typedef struct otNetworkDiagConnectivity 95 { 96 int8_t mParentPriority; ///< The priority of the sender as a parent. 97 uint8_t mLinkQuality3; ///< Number of neighbors with link of quality 3. 98 uint8_t mLinkQuality2; ///< Number of neighbors with link of quality 2. 99 uint8_t mLinkQuality1; ///< Number of neighbors with link of quality 1. 100 uint8_t mLeaderCost; ///< Cost to the Leader. 101 uint8_t mIdSequence; ///< Most recent received ID seq number. 102 uint8_t mActiveRouters; ///< Number of active routers. 103 uint16_t mSedBufferSize; ///< Buffer capacity in bytes for SEDs. Optional. 104 uint8_t mSedDatagramCount; ///< Queue capacity (number of IPv6 datagrams) per SED. Optional. 105 } otNetworkDiagConnectivity; 106 107 /** 108 * Represents a Network Diagnostic Route data. 109 */ 110 typedef struct otNetworkDiagRouteData 111 { 112 uint8_t mRouterId; ///< The Assigned Router ID. 113 uint8_t mLinkQualityOut : 2; ///< Link Quality Out. 114 uint8_t mLinkQualityIn : 2; ///< Link Quality In. 115 uint8_t mRouteCost : 4; ///< Routing Cost. Infinite routing cost is represented by value 0. 116 } otNetworkDiagRouteData; 117 118 /** 119 * Represents a Network Diagnostic Route64 TLV value. 120 */ 121 typedef struct otNetworkDiagRoute 122 { 123 /** 124 * The sequence number associated with the set of Router ID assignments in #mRouteData. 125 */ 126 uint8_t mIdSequence; ///< Sequence number for Router ID assignments. 127 uint8_t mRouteCount; ///< Number of routes. 128 otNetworkDiagRouteData mRouteData[OT_NETWORK_MAX_ROUTER_ID + 1]; ///< Link Quality and Routing Cost data. 129 } otNetworkDiagRoute; 130 131 /** 132 * Represents a Network Diagnostic Mac Counters value. 133 * 134 * See <a href="https://www.ietf.org/rfc/rfc2863">RFC 2863</a> for definitions of member fields. 135 */ 136 typedef struct otNetworkDiagMacCounters 137 { 138 uint32_t mIfInUnknownProtos; 139 uint32_t mIfInErrors; 140 uint32_t mIfOutErrors; 141 uint32_t mIfInUcastPkts; 142 uint32_t mIfInBroadcastPkts; 143 uint32_t mIfInDiscards; 144 uint32_t mIfOutUcastPkts; 145 uint32_t mIfOutBroadcastPkts; 146 uint32_t mIfOutDiscards; 147 } otNetworkDiagMacCounters; 148 149 /** 150 * Represents a Network Diagnostics MLE Counters value. 151 */ 152 typedef struct otNetworkDiagMleCounters 153 { 154 uint16_t mDisabledRole; ///< Number of times device entered disabled role. 155 uint16_t mDetachedRole; ///< Number of times device entered detached role. 156 uint16_t mChildRole; ///< Number of times device entered child role. 157 uint16_t mRouterRole; ///< Number of times device entered router role. 158 uint16_t mLeaderRole; ///< Number of times device entered leader role. 159 uint16_t mAttachAttempts; ///< Number of attach attempts while device was detached. 160 uint16_t mPartitionIdChanges; ///< Number of changes to partition ID. 161 uint16_t mBetterPartitionAttachAttempts; ///< Number of attempts to attach to a better partition. 162 uint16_t mParentChanges; ///< Number of time device changed its parent. 163 uint64_t mTrackedTime; ///< Milliseconds tracked by next counters (zero if not supported). 164 uint64_t mDisabledTime; ///< Milliseconds device has been in disabled role. 165 uint64_t mDetachedTime; ///< Milliseconds device has been in detached role. 166 uint64_t mChildTime; ///< Milliseconds device has been in child role. 167 uint64_t mRouterTime; ///< Milliseconds device has been in router role. 168 uint64_t mLeaderTime; ///< Milliseconds device has been in leader role. 169 } otNetworkDiagMleCounters; 170 171 /** 172 * Represents a Network Diagnostic Child Table Entry. 173 */ 174 typedef struct otNetworkDiagChildEntry 175 { 176 uint16_t mTimeout : 5; ///< Expected poll timeout expressed as 2^(Timeout-4) seconds. 177 uint8_t mLinkQuality : 2; ///< Link Quality In value [0,3]. Zero indicate sender cannot provide link quality info. 178 uint16_t mChildId : 9; ///< Child ID (derived from child RLOC) 179 otLinkModeConfig mMode; ///< Link mode. 180 } otNetworkDiagChildEntry; 181 182 /** 183 * Represents a Network Diagnostic TLV. 184 */ 185 typedef struct otNetworkDiagTlv 186 { 187 uint8_t mType; ///< The Network Diagnostic TLV type. 188 189 union 190 { 191 otExtAddress mExtAddress; 192 otExtAddress mEui64; 193 uint16_t mAddr16; 194 otLinkModeConfig mMode; 195 uint32_t mTimeout; 196 otNetworkDiagConnectivity mConnectivity; 197 otNetworkDiagRoute mRoute; 198 otLeaderData mLeaderData; 199 otNetworkDiagMacCounters mMacCounters; 200 otNetworkDiagMleCounters mMleCounters; 201 uint8_t mBatteryLevel; 202 uint16_t mSupplyVoltage; 203 uint32_t mMaxChildTimeout; 204 uint16_t mVersion; 205 char mVendorName[OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_NAME_TLV_LENGTH + 1]; 206 char mVendorModel[OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_MODEL_TLV_LENGTH + 1]; 207 char mVendorSwVersion[OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_SW_VERSION_TLV_LENGTH + 1]; 208 char mThreadStackVersion[OT_NETWORK_DIAGNOSTIC_MAX_THREAD_STACK_VERSION_TLV_LENGTH + 1]; 209 char mVendorAppUrl[OT_NETWORK_DIAGNOSTIC_MAX_VENDOR_APP_URL_TLV_LENGTH + 1]; 210 struct 211 { 212 uint8_t mCount; 213 uint8_t m8[OT_NETWORK_BASE_TLV_MAX_LENGTH]; 214 } mNetworkData; 215 struct 216 { 217 uint8_t mCount; 218 otIp6Address mList[OT_NETWORK_BASE_TLV_MAX_LENGTH / sizeof(otIp6Address)]; 219 } mIp6AddrList; 220 struct 221 { 222 uint8_t mCount; 223 otNetworkDiagChildEntry mTable[OT_NETWORK_BASE_TLV_MAX_LENGTH / sizeof(otNetworkDiagChildEntry)]; 224 } mChildTable; 225 struct 226 { 227 uint8_t mCount; 228 uint8_t m8[OT_NETWORK_BASE_TLV_MAX_LENGTH]; 229 } mChannelPages; 230 } mData; 231 } otNetworkDiagTlv; 232 233 /** 234 * Gets the next Network Diagnostic TLV in the message. 235 * 236 * Requires `OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE`. 237 * 238 * @param[in] aMessage A pointer to a message. 239 * @param[in,out] aIterator A pointer to the Network Diagnostic iterator context. To get the first 240 * Network Diagnostic TLV it should be set to OT_NETWORK_DIAGNOSTIC_ITERATOR_INIT. 241 * @param[out] aNetworkDiagTlv A pointer to where the Network Diagnostic TLV information will be placed. 242 * 243 * @retval OT_ERROR_NONE Successfully found the next Network Diagnostic TLV. 244 * @retval OT_ERROR_NOT_FOUND No subsequent Network Diagnostic TLV exists in the message. 245 * @retval OT_ERROR_PARSE Parsing the next Network Diagnostic failed. 246 * 247 * @Note A subsequent call to this function is allowed only when current return value is OT_ERROR_NONE. 248 */ 249 otError otThreadGetNextDiagnosticTlv(const otMessage *aMessage, 250 otNetworkDiagIterator *aIterator, 251 otNetworkDiagTlv *aNetworkDiagTlv); 252 253 /** 254 * Pointer is called when Network Diagnostic Get response is received. 255 * 256 * @param[in] aError The error when failed to get the response. 257 * @param[in] aMessage A pointer to the message buffer containing the received Network Diagnostic 258 * Get response payload. Available only when @p aError is `OT_ERROR_NONE`. 259 * @param[in] aMessageInfo A pointer to the message info for @p aMessage. Available only when 260 * @p aError is `OT_ERROR_NONE`. 261 * @param[in] aContext A pointer to application-specific context. 262 */ 263 typedef void (*otReceiveDiagnosticGetCallback)(otError aError, 264 otMessage *aMessage, 265 const otMessageInfo *aMessageInfo, 266 void *aContext); 267 268 /** 269 * Send a Network Diagnostic Get request. 270 * 271 * Requires `OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE`. 272 * 273 * @param[in] aInstance A pointer to an OpenThread instance. 274 * @param[in] aDestination A pointer to destination address. 275 * @param[in] aTlvTypes An array of Network Diagnostic TLV types. 276 * @param[in] aCount Number of types in aTlvTypes. 277 * @param[in] aCallback A pointer to a function that is called when Network Diagnostic Get response 278 * is received or NULL to disable the callback. 279 * @param[in] aCallbackContext A pointer to application-specific context. 280 * 281 * @retval OT_ERROR_NONE Successfully queued the DIAG_GET.req. 282 * @retval OT_ERROR_NO_BUFS Insufficient message buffers available to send DIAG_GET.req. 283 */ 284 otError otThreadSendDiagnosticGet(otInstance *aInstance, 285 const otIp6Address *aDestination, 286 const uint8_t aTlvTypes[], 287 uint8_t aCount, 288 otReceiveDiagnosticGetCallback aCallback, 289 void *aCallbackContext); 290 291 /** 292 * Send a Network Diagnostic Reset request. 293 * 294 * Requires `OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE`. 295 * 296 * @param[in] aInstance A pointer to an OpenThread instance. 297 * @param[in] aDestination A pointer to destination address. 298 * @param[in] aTlvTypes An array of Network Diagnostic TLV types. Currently only Type 9 is allowed. 299 * @param[in] aCount Number of types in aTlvTypes 300 * 301 * @retval OT_ERROR_NONE Successfully queued the DIAG_RST.ntf. 302 * @retval OT_ERROR_NO_BUFS Insufficient message buffers available to send DIAG_RST.ntf. 303 */ 304 otError otThreadSendDiagnosticReset(otInstance *aInstance, 305 const otIp6Address *aDestination, 306 const uint8_t aTlvTypes[], 307 uint8_t aCount); 308 309 /** 310 * Get the vendor name string. 311 * 312 * @param[in] aInstance A pointer to an OpenThread instance. 313 * 314 * @returns The vendor name string. 315 */ 316 const char *otThreadGetVendorName(otInstance *aInstance); 317 318 /** 319 * Get the vendor model string. 320 * 321 * @param[in] aInstance A pointer to an OpenThread instance. 322 * 323 * @returns The vendor model string. 324 */ 325 const char *otThreadGetVendorModel(otInstance *aInstance); 326 327 /** 328 * Get the vendor software version string. 329 * 330 * @param[in] aInstance A pointer to an OpenThread instance. 331 * 332 * @returns The vendor software version string. 333 */ 334 const char *otThreadGetVendorSwVersion(otInstance *aInstance); 335 336 /** 337 * Get the vendor app URL string. 338 * 339 * @param[in] aInstance A pointer to an OpenThread instance. 340 * 341 * @returns The vendor app URL string. 342 */ 343 const char *otThreadGetVendorAppUrl(otInstance *aInstance); 344 345 /** 346 * Set the vendor name string. 347 * 348 * Requires `OPENTHREAD_CONFIG_NET_DIAG_VENDOR_INFO_SET_API_ENABLE`. 349 * 350 * @p aVendorName should be UTF8 with max length of 32 chars (`MAX_VENDOR_NAME_TLV_LENGTH`). Maximum length does not 351 * include the null `\0` character. 352 * 353 * @param[in] aInstance A pointer to an OpenThread instance. 354 * @param[in] aVendorName The vendor name string. 355 * 356 * @retval OT_ERROR_NONE Successfully set the vendor name. 357 * @retval OT_ERROR_INVALID_ARGS @p aVendorName is not valid (too long or not UTF8). 358 */ 359 otError otThreadSetVendorName(otInstance *aInstance, const char *aVendorName); 360 361 /** 362 * Set the vendor model string. 363 * 364 * Requires `OPENTHREAD_CONFIG_NET_DIAG_VENDOR_INFO_SET_API_ENABLE`. 365 * 366 * @p aVendorModel should be UTF8 with max length of 32 chars (`MAX_VENDOR_MODEL_TLV_LENGTH`). Maximum length does not 367 * include the null `\0` character. 368 * 369 * @param[in] aInstance A pointer to an OpenThread instance. 370 * @param[in] aVendorModel The vendor model string. 371 * 372 * @retval OT_ERROR_NONE Successfully set the vendor model. 373 * @retval OT_ERROR_INVALID_ARGS @p aVendorModel is not valid (too long or not UTF8). 374 */ 375 otError otThreadSetVendorModel(otInstance *aInstance, const char *aVendorModel); 376 377 /** 378 * Set the vendor software version string. 379 * 380 * Requires `OPENTHREAD_CONFIG_NET_DIAG_VENDOR_INFO_SET_API_ENABLE`. 381 * 382 * @p aVendorSwVersion should be UTF8 with max length of 16 chars(`MAX_VENDOR_SW_VERSION_TLV_LENGTH`). Maximum length 383 * does not include the null `\0` character. 384 * 385 * @param[in] aInstance A pointer to an OpenThread instance. 386 * @param[in] aVendorSwVersion The vendor software version string. 387 * 388 * @retval OT_ERROR_NONE Successfully set the vendor software version. 389 * @retval OT_ERROR_INVALID_ARGS @p aVendorSwVersion is not valid (too long or not UTF8). 390 */ 391 otError otThreadSetVendorSwVersion(otInstance *aInstance, const char *aVendorSwVersion); 392 393 /** 394 * Set the vendor app URL string. 395 * 396 * Requires `OPENTHREAD_CONFIG_NET_DIAG_VENDOR_INFO_SET_API_ENABLE`. 397 * 398 * @p aVendorAppUrl should be UTF8 with max length of 64 chars (`MAX_VENDOR_APPL_URL_TLV_LENGTH`). Maximum length 399 * does not include the null `\0` character. 400 * 401 * @param[in] aInstance A pointer to an OpenThread instance. 402 * @param[in] aVendorAppUrl The vendor app URL string. 403 * 404 * @retval OT_ERROR_NONE Successfully set the vendor app URL string. 405 * @retval OT_ERROR_INVALID_ARGS @p aVendorAppUrl is not valid (too long or not UTF8). 406 */ 407 otError otThreadSetVendorAppUrl(otInstance *aInstance, const char *aVendorAppUrl); 408 409 /** 410 * @} 411 */ 412 413 #ifdef __cplusplus 414 } // extern "C" 415 #endif 416 417 #endif // OPENTHREAD_NETDIAG_H_ 418