diff --git a/etc/cmake/options.cmake b/etc/cmake/options.cmake index 7bbe4ebc8..812295720 100644 --- a/etc/cmake/options.cmake +++ b/etc/cmake/options.cmake @@ -122,12 +122,12 @@ ot_option(OT_MESH_DIAG OPENTHREAD_CONFIG_MESH_DIAG_ENABLE "mesh diag") ot_option(OT_MESSAGE_USE_HEAP OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE "heap allocator for message buffers") ot_option(OT_MLE_LONG_ROUTES OPENTHREAD_CONFIG_MLE_LONG_ROUTES_ENABLE "MLE long routes extension (experimental)") ot_option(OT_MLR OPENTHREAD_CONFIG_MLR_ENABLE "Multicast Listener Registration (MLR)") -ot_option(OT_MTD_NETDIAG OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE "TMF network diagnostics on MTDs") ot_option(OT_MULTIPLE_INSTANCE OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE "multiple instances") ot_option(OT_NAT64_BORDER_ROUTING OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE "border routing NAT64") ot_option(OT_NAT64_TRANSLATOR OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE "NAT64 translator support") ot_option(OT_NEIGHBOR_DISCOVERY_AGENT OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE "neighbor discovery agent") ot_option(OT_NETDATA_PUBLISHER OPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE "Network Data publisher") +ot_option(OT_NETDIAG_CLIENT OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE "Network Diagnostic client") ot_option(OT_OTNS OPENTHREAD_CONFIG_OTNS_ENABLE "OTNS") ot_option(OT_PING_SENDER OPENTHREAD_CONFIG_PING_SENDER_ENABLE "ping sender" ${OT_APP_CLI}) ot_option(OT_PLATFORM_NETIF OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE "platform netif") @@ -210,3 +210,17 @@ endif() if(OT_POSIX_SETTINGS_PATH) target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH=${OT_POSIX_SETTINGS_PATH}") endif() + +#----------------------------------------------------------------------------------------------------------------------- +# Check removed/replaced options + +macro(ot_removed_option name error) + # This macro checks for a remove option and emits an error + # if the option is set. + get_property(is_set CACHE ${name} PROPERTY VALUE SET) + if (is_set) + message(FATAL_ERROR "Removed option ${name} is set - ${error}") + endif() +endmacro() + +ot_removed_option(OT_MTD_NETDIAG "- Use OT_NETDIAG_CLIENT instead - note that server function is always supported") diff --git a/etc/gn/openthread.gni b/etc/gn/openthread.gni index fe494b08e..9bf75fb0f 100644 --- a/etc/gn/openthread.gni +++ b/etc/gn/openthread.gni @@ -174,8 +174,8 @@ if (openthread_enable_core_config_args) { # Enable MLE long routes extension (experimental, breaks Thread conformance] openthread_config_mle_long_routes_enable = false - # Enable TMF network diagnostics on MTDs - openthread_config_tmf_network_diag_mtd_enable = false + # Enable TMF network diagnostics client + openthread_config_tmf_netdiag_client_enable = false # Enable multiple instances openthread_config_multiple_instance_enable = false diff --git a/examples/Makefile-simulation b/examples/Makefile-simulation index f7577f723..72894fb47 100644 --- a/examples/Makefile-simulation +++ b/examples/Makefile-simulation @@ -59,9 +59,9 @@ JAM_DETECTION ?= 1 JOINER ?= 1 LINK_RAW ?= 1 MAC_FILTER ?= 1 -MTD_NETDIAG ?= 1 NEIGHBOR_DISCOVERY_AGENT ?= 1 NETDATA_PUBLISHER ?= 1 +NETDIAG_CLIENT ?= 1 PING_SENDER ?= 1 REFERENCE_DEVICE ?= 1 SERVICE ?= 1 diff --git a/examples/README.md b/examples/README.md index 45d4815f3..0c2a59de7 100644 --- a/examples/README.md +++ b/examples/README.md @@ -51,11 +51,11 @@ This page lists the available common switches with description. Unless stated ot | MAC_FILTER | OT_MAC_FILTER | Enables support for the MAC filter. | | MLE_LONG_ROUTES | OT_MLE_LONG_ROUTES | Enables the MLE long routes extension. **Note: Enabling this feature breaks conformance to the Thread Specification.** | | MLR | OT_MLR | Enables Multicast Listener Registration feature for Thread 1.2. | -| MTD_NETDIAG | OT_MTD_NETDIAG | Enables the TMF network diagnostics on MTDs. | | MULTIPLE_INSTANCE | OT_MULTIPLE_INSTANCE | Enables multiple OpenThread instances. | | NAT64_BORDER_ROUTING | OT_NAT64_BORDER_ROUTING | Enables NAT64 border routing support for Border Router. | | NAT64_TRANSLATOR | OT_NAT64_TRANSLATOR | Enables NAT64 translator for Border Router. | | NETDATA_PUBLISHER | OT_NETDATA_PUBLISHER | Enables support for Thread Network Data publisher. | +| NETDIAG_CLIENT | OT_NETDIAG_CLIENT | Enables Network Diagnostics client functionality. | | PING_SENDER | OT_PING_SENDER | Enables support for ping sender. | | OTNS | OT_OTNS | Enables support for [OpenThread Network Simulator](https://github.com/openthread/ot-ns). Enable this switch if you are building OpenThread for OpenThread Network Simulator. | | PLATFORM_UDP | OT_PLATFORM_UDP | Enables platform UDP support. | @@ -73,3 +73,9 @@ This page lists the available common switches with description. Unless stated ot | TREL | OT_TREL | Enables TREL radio link for Thread over Infrastructure feature. | | UDP_FORWARD | OT_UDP_FORWARD | Enables support for UDP forward. Enable this switch on the Border Router device (running on the NCP design) with External Commissioning support to service Thread Commissioner packets on the NCP side. | | UPTIME | OT_UPTIME | Enables support for tracking OpenThread instance's uptime. | + +Removed or replaced switches: + +| Makefile switch | CMake switch | Description | +| --- | --- | --- | +| MTD_NETDIAG | OT_MTD_NETDIAG | Use NEDIAG_CLIENT to enable client functionality. Server functionality is always supported. | diff --git a/examples/common-switches.mk b/examples/common-switches.mk index 0ae683133..36ff95726 100644 --- a/examples/common-switches.mk +++ b/examples/common-switches.mk @@ -76,6 +76,7 @@ NAT64_BORDER_ROUTING ?= 0 NAT64_TRANSLATOR ?= 0 NEIGHBOR_DISCOVERY_AGENT ?= 0 NETDATA_PUBLISHER ?= 0 +NETDIAG_CLIENT ?= 0 OTNS ?= 0 PING_SENDER ?= 1 PLATFORM_UDP ?= 0 @@ -286,6 +287,9 @@ ifeq ($(MLR),1) COMMONCFLAGS += -DOPENTHREAD_CONFIG_MLR_ENABLE=1 endif +# This config is removed but we still check and add the +# `OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE` so to +# get an error during build if `MTD_NETDIAG` is used. ifeq ($(MTD_NETDIAG),1) COMMONCFLAGS += -DOPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE=1 endif @@ -302,6 +306,10 @@ ifeq ($(NETDATA_PUBLISHER),1) COMMONCFLAGS += -DOPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE=1 endif +ifeq ($(NETDIAG_CLIENT),1) +COMMONCFLAGS += -DOPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE=1 +endif + ifeq ($(PING_SENDER),1) COMMONCFLAGS += -DOPENTHREAD_CONFIG_PING_SENDER_ENABLE=1 endif diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 367d8e975..8f523cbe1 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (307) +#define OPENTHREAD_API_VERSION (308) /** * @addtogroup api-instance diff --git a/include/openthread/netdiag.h b/include/openthread/netdiag.h index c4071e207..ce8ab36ac 100644 --- a/include/openthread/netdiag.h +++ b/include/openthread/netdiag.h @@ -47,6 +47,8 @@ extern "C" { * * @{ * + * + * Network Dianostics APIs require OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE. */ /** diff --git a/script/check-arm-build b/script/check-arm-build index a2dff828d..398dff766 100755 --- a/script/check-arm-build +++ b/script/check-arm-build @@ -65,8 +65,8 @@ build_nrf52840() "-DOT_MAC_FILTER=ON" "-DOT_MESSAGE_USE_HEAP=ON" "-DOT_MLR=ON" - "-DOT_MTD_NETDIAG=ON" "-DOT_NETDATA_PUBLISHER=ON" + "-DOT_NETDIAG_CLIENT=ON" "-DOT_PING_SENDER=ON" "-DOT_SERVICE=ON" "-DOT_SLAAC=ON" diff --git a/script/check-scan-build b/script/check-scan-build index b04b111b4..edd86d7ca 100755 --- a/script/check-scan-build +++ b/script/check-scan-build @@ -62,10 +62,10 @@ OT_BUILD_OPTIONS=( "-DOT_LOG_LEVEL_DYNAMIC=ON" "-DOT_MAC_FILTER=ON" "-DOT_MESH_DIAG=ON" - "-DOT_MTD_NETDIAG=ON" "-DOT_NAT64_BORDER_ROUTING=ON" "-DOT_NAT64_TRANSLATOR=ON" "-DOT_NEIGHBOR_DISCOVERY_AGENT=ON" + "-DOT_NETDIAG_CLIENT=ON" "-DOT_PING_SENDER=ON" "-DOT_PLATFORM=external" "-DOT_RCP_RESTORATION_MAX_COUNT=2" diff --git a/script/check-simulation-build-autotools b/script/check-simulation-build-autotools index 118a293a4..1e3ef250f 100755 --- a/script/check-simulation-build-autotools +++ b/script/check-simulation-build-autotools @@ -86,7 +86,7 @@ build_all_features() "-DOPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE=1" "-DOPENTHREAD_CONFIG_SRP_CLIENT_ENABLE=1" "-DOPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE=1" - "-DOPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE=1" + "-DOPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE=1" "-DOPENTHREAD_CONFIG_UDP_FORWARD_ENABLE=1" "-DOPENTHREAD_CONFIG_MAC_BEACON_PAYLOAD_PARSING_ENABLE=1" "-DOPENTHREAD_CONFIG_MAC_OUTGOING_BEACON_PAYLOAD_ENABLE=1" diff --git a/script/check-size b/script/check-size index 482dddf1a..907662698 100755 --- a/script/check-size +++ b/script/check-size @@ -142,7 +142,6 @@ size_nrf52840_version() "-DOT_LINK_RAW=ON" "-DOT_MAC_FILTER=ON" "-DOT_MESSAGE_USE_HEAP=ON" - "-DOT_MTD_NETDIAG=ON" "-DOT_NETDATA_PUBLISHER=ON" "-DOT_PING_SENDER=ON" "-DOT_SERVICE=ON" diff --git a/script/cmake-build b/script/cmake-build index 364721623..9d16d2474 100755 --- a/script/cmake-build +++ b/script/cmake-build @@ -92,9 +92,9 @@ OT_POSIX_SIM_COMMON_OPTIONS=( "-DOT_JAM_DETECTION=ON" "-DOT_JOINER=ON" "-DOT_MAC_FILTER=ON" - "-DOT_MTD_NETDIAG=ON" "-DOT_NEIGHBOR_DISCOVERY_AGENT=ON" "-DOT_NETDATA_PUBLISHER=ON" + "-DOT_NETDIAG_CLIENT=ON" "-DOT_PING_SENDER=ON" "-DOT_REFERENCE_DEVICE=ON" "-DOT_SERVICE=ON" diff --git a/script/make-pretty b/script/make-pretty index 443ac5321..28abdf014 100755 --- a/script/make-pretty +++ b/script/make-pretty @@ -121,10 +121,10 @@ OT_CLANG_TIDY_BUILD_OPTS=( '-DOT_LINK_METRICS_SUBJECT=ON' '-DOT_MAC_FILTER=ON' '-DOT_MESH_DIAG=ON' - '-DOT_MTD_NETDIAG=ON' '-DOT_NAT64_BORDER_ROUTING=ON' '-DOT_NAT64_TRANSLATOR=ON' '-DOT_NETDATA_PUBLISHER=ON' + '-DOT_NETDIAG_CLIENT=ON' '-DOT_PING_SENDER=ON' '-DOT_REFERENCE_DEVICE=ON' '-DOT_SERVICE=ON' diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 4b1780bce..b45c2c3c7 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -7950,7 +7950,7 @@ exit: } #endif -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE template <> otError Interpreter::Process(Arg aArgs[]) { otError error = OT_ERROR_NONE; @@ -8175,7 +8175,7 @@ void Interpreter::OutputChildTableEntry(uint8_t aIndentSize, const otNetworkDiag OutputLine(aIndentSize, "Mode:"); OutputMode(aIndentSize + kIndentSize, aChildEntry.mMode); } -#endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE +#endif // OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE void Interpreter::HandleDetachGracefullyResult(void *aContext) { @@ -8394,7 +8394,7 @@ otError Interpreter::ProcessCommand(Arg aArgs[]) #endif CmdEntry("netdata"), CmdEntry("netstat"), -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE CmdEntry("networkdiagnostic"), #endif #if OPENTHREAD_FTD diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 79faa376d..d9a59ce53 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -439,7 +439,7 @@ private: static void HandleEnergyScanResult(otEnergyScanResult *aResult, void *aContext); static void HandleLinkPcapReceive(const otRadioFrame *aFrame, bool aIsTx, void *aContext); -#if OPENTHREAD_FTD || (OPENTHREAD_MTD && OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE) +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE void HandleDiagnosticGetResponse(otError aError, const otMessage *aMessage, const Ip6::MessageInfo *aMessageInfo); static void HandleDiagnosticGetResponse(otError aError, otMessage *aMessage, diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn index f968aee6a..5212041b4 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -196,18 +196,19 @@ if (openthread_enable_core_config_args) { defines += [ "OPENTHREAD_CONFIG_MLE_LONG_ROUTES_ENABLE=1" ] } - if (openthread_config_tmf_network_diag_mtd_enable) { - defines += [ "OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE=1" ] - } - if (openthread_config_multiple_instance_enable) { defines += [ "OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE=1" ] } + if (openthread_config_tmf_netdiag_client_enable) { + defines += [ "OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE=1" ] + } + if (openthread_config_platform_netif_enable) { defines += [ "OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE=1" ] } + if (openthread_config_platform_udp_enable) { defines += [ "OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE=1" ] } diff --git a/src/core/api/netdiag_api.cpp b/src/core/api/netdiag_api.cpp index b6f2d6072..68cf91c62 100644 --- a/src/core/api/netdiag_api.cpp +++ b/src/core/api/netdiag_api.cpp @@ -33,7 +33,7 @@ #include "openthread-core-config.h" -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE #include @@ -49,7 +49,7 @@ otError otThreadGetNextDiagnosticTlv(const otMessage *aMessage, AssertPointerIsNotNull(aIterator); AssertPointerIsNotNull(aNetworkDiagTlv); - return NetworkDiagnostic::NetworkDiagnostic::GetNextDiagTlv(AsCoapMessage(aMessage), *aIterator, *aNetworkDiagTlv); + return NetworkDiagnostic::Client::GetNextDiagTlv(AsCoapMessage(aMessage), *aIterator, *aNetworkDiagTlv); } otError otThreadSendDiagnosticGet(otInstance *aInstance, @@ -59,7 +59,7 @@ otError otThreadSendDiagnosticGet(otInstance *aInstance, otReceiveDiagnosticGetCallback aCallback, void *aCallbackContext) { - return AsCoreType(aInstance).Get().SendDiagnosticGet( + return AsCoreType(aInstance).Get().SendDiagnosticGet( AsCoreType(aDestination), aTlvTypes, aCount, aCallback, aCallbackContext); } @@ -68,8 +68,8 @@ otError otThreadSendDiagnosticReset(otInstance *aInstance, const uint8_t aTlvTypes[], uint8_t aCount) { - return AsCoreType(aInstance).Get().SendDiagnosticReset( - AsCoreType(aDestination), aTlvTypes, aCount); + return AsCoreType(aInstance).Get().SendDiagnosticReset(AsCoreType(aDestination), + aTlvTypes, aCount); } -#endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE +#endif // OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE diff --git a/src/core/common/instance.cpp b/src/core/common/instance.cpp index 384843b68..294fc4a71 100644 --- a/src/core/common/instance.cpp +++ b/src/core/common/instance.cpp @@ -136,8 +136,9 @@ Instance::Instance(void) , mNetworkDataPublisher(*this) #endif , mNetworkDataServiceManager(*this) -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - , mNetworkDiagnostic(*this) + , mNetworkDiagnosticServer(*this) +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE + , mNetworkDiagnosticClient(*this) #endif #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE , mBorderAgent(*this) diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index 14d21ca6b..d7c171dcb 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -495,8 +495,9 @@ private: NetworkData::Service::Manager mNetworkDataServiceManager; -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - NetworkDiagnostic::NetworkDiagnostic mNetworkDiagnostic; + NetworkDiagnostic::Server mNetworkDiagnosticServer; +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE + NetworkDiagnostic::Client mNetworkDiagnosticClient; #endif #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE @@ -824,8 +825,10 @@ template <> inline Dns::ServiceDiscovery::Server &Instance::Get(void) { return m template <> inline Dns::Dso &Instance::Get(void) { return mDnsDso; } #endif -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE -template <> inline NetworkDiagnostic::NetworkDiagnostic &Instance::Get(void) { return mNetworkDiagnostic; } +template <> inline NetworkDiagnostic::Server &Instance::Get(void) { return mNetworkDiagnosticServer; } + +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE +template <> inline NetworkDiagnostic::Client &Instance::Get(void) { return mNetworkDiagnosticClient; } #endif #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE diff --git a/src/core/config/openthread-core-config-check.h b/src/core/config/openthread-core-config-check.h index 991214770..685fb9123 100644 --- a/src/core/config/openthread-core-config-check.h +++ b/src/core/config/openthread-core-config-check.h @@ -648,4 +648,10 @@ #error "OPENTHREAD_CONFIG_CHILD_SUPERVISION_MSG_NO_ACK_REQUEST is removed". #endif +#ifdef OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE +#error "OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE is removed. "\ + "Use OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE to enable client functionality."\ + "Netdiag server functionality is always supported." +#endif + #endif // OPENTHREAD_CORE_CONFIG_CHECK_H_ diff --git a/src/core/config/tmf.h b/src/core/config/tmf.h index 0bd1f3651..5632b8f64 100644 --- a/src/core/config/tmf.h +++ b/src/core/config/tmf.h @@ -173,13 +173,16 @@ #endif /** - * @def OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE + * @def OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE * - * Define to 1 to enable TMF network diagnostics on MTDs. + * Define to 1 to enable TMF network diagnostics client. + * + * The network diagnostic client add API to send diagnostic requests and queries to other node and process the response. + * It is enabled by default on Border Routers. * */ -#ifndef OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE -#define OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE 0 +#ifndef OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE +#define OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE #endif /** diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 293426feb..2511047e6 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -33,8 +33,6 @@ #include "network_diagnostic.hpp" -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - #include "coap/coap_message.hpp" #include "common/array.hpp" #include "common/as_core_type.hpp" @@ -58,82 +56,15 @@ RegisterLogModule("NetDiag"); namespace NetworkDiagnostic { -NetworkDiagnostic::NetworkDiagnostic(Instance &aInstance) +//--------------------------------------------------------------------------------------------------------------------- +// Server + +Server::Server(Instance &aInstance) : InstanceLocator(aInstance) { } -Error NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, - const uint8_t aTlvTypes[], - uint8_t aCount, - GetCallback aCallback, - void *aContext) -{ - Error error; - - if (aDestination.IsMulticast()) - { - error = SendCommand(kUriDiagnosticGetQuery, aDestination, aTlvTypes, aCount); - } - else - { - error = SendCommand(kUriDiagnosticGetRequest, aDestination, aTlvTypes, aCount, &HandleGetResponse, this); - } - - SuccessOrExit(error); - - mGetCallback.Set(aCallback, aContext); - -exit: - return error; -} - -Error NetworkDiagnostic::SendCommand(Uri aUri, - const Ip6::Address &aDestination, - const uint8_t aTlvTypes[], - uint8_t aCount, - Coap::ResponseHandler aHandler, - void *aContext) -{ - Error error; - Coap::Message *message = nullptr; - Tmf::MessageInfo messageInfo(GetInstance()); - - switch (aUri) - { - case kUriDiagnosticGetQuery: - message = Get().NewNonConfirmablePostMessage(aUri); - break; - - case kUriDiagnosticGetRequest: - case kUriDiagnosticReset: - message = Get().NewConfirmablePostMessage(aUri); - break; - - default: - OT_ASSERT(false); - } - - VerifyOrExit(message != nullptr, error = kErrorNoBufs); - - if (aCount > 0) - { - SuccessOrExit(error = Tlv::Append(*message, aTlvTypes, aCount)); - } - - PrepareMessageInfoForDest(aDestination, messageInfo); - - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, aHandler, aContext)); - - Log(kMessageSend, aUri, aDestination); - -exit: - FreeMessageOnError(message, error); - return error; -} - -void NetworkDiagnostic::PrepareMessageInfoForDest(const Ip6::Address &aDestination, - Tmf::MessageInfo &aMessageInfo) const +void Server::PrepareMessageInfoForDest(const Ip6::Address &aDestination, Tmf::MessageInfo &aMessageInfo) const { if (aDestination.IsMulticast()) { @@ -152,41 +83,7 @@ void NetworkDiagnostic::PrepareMessageInfoForDest(const Ip6::Address &aDestinati aMessageInfo.SetPeerAddr(aDestination); } -void NetworkDiagnostic::HandleGetResponse(void *aContext, - otMessage *aMessage, - const otMessageInfo *aMessageInfo, - Error aResult) -{ - static_cast(aContext)->HandleGetResponse(AsCoapMessagePtr(aMessage), - AsCoreTypePtr(aMessageInfo), aResult); -} - -void NetworkDiagnostic::HandleGetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult) -{ - SuccessOrExit(aResult); - VerifyOrExit(aMessage->GetCode() == Coap::kCodeChanged, aResult = kErrorFailed); - -exit: - mGetCallback.InvokeIfSet(aResult, aMessage, aMessageInfo); -} - -template <> -void NetworkDiagnostic::HandleTmf(Coap::Message &aMessage, - const Ip6::MessageInfo &aMessageInfo) -{ - VerifyOrExit(aMessage.IsConfirmablePostRequest()); - - Log(kMessageReceive, kUriDiagnosticGetAnswer, aMessageInfo.GetPeerAddr()); - - mGetCallback.InvokeIfSet(kErrorNone, &aMessage, &aMessageInfo); - - IgnoreError(Get().SendEmptyAck(aMessage, aMessageInfo)); - -exit: - return; -} - -Error NetworkDiagnostic::AppendIp6AddressList(Message &aMessage) +Error Server::AppendIp6AddressList(Message &aMessage) { Error error = kErrorNone; uint16_t count = 0; @@ -224,7 +121,7 @@ exit: } #if OPENTHREAD_FTD -Error NetworkDiagnostic::AppendChildTable(Message &aMessage) +Error Server::AppendChildTable(Message &aMessage) { Error error = kErrorNone; uint16_t count; @@ -276,7 +173,7 @@ exit: } #endif // OPENTHREAD_FTD -Error NetworkDiagnostic::AppendMacCounters(Message &aMessage) +Error Server::AppendMacCounters(Message &aMessage) { MacCountersTlv tlv; const otMacCounters &counters = Get().GetCounters(); @@ -298,7 +195,7 @@ Error NetworkDiagnostic::AppendMacCounters(Message &aMessage) return tlv.AppendTo(aMessage); } -Error NetworkDiagnostic::AppendRequestedTlvs(const Message &aRequest, Message &aResponse) +Error Server::AppendRequestedTlvs(const Message &aRequest, Message &aResponse) { Error error; uint16_t offset; @@ -320,7 +217,7 @@ exit: return error; } -Error NetworkDiagnostic::AppendDiagTlv(uint8_t aTlvType, Message &aMessage) +Error Server::AppendDiagTlv(uint8_t aTlvType, Message &aMessage) { Error error = kErrorNone; @@ -437,14 +334,16 @@ exit: } template <> -void NetworkDiagnostic::HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) +void Server::HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) { Error error = kErrorNone; Coap::Message *response = nullptr; Tmf::MessageInfo responseInfo(GetInstance()); VerifyOrExit(aMessage.IsPostRequest(), error = kErrorDrop); - Log(kMessageReceive, kUriDiagnosticGetQuery, aMessageInfo.GetPeerAddr()); + + LogInfo("Received %s from %s", UriToString(), + aMessageInfo.GetPeerAddr().ToString().AsCString()); // DIAG_GET.qry may be sent as a confirmable request. if (aMessage.IsConfirmable()) @@ -465,14 +364,15 @@ exit: } template <> -void NetworkDiagnostic::HandleTmf(Coap::Message &aMessage, - const Ip6::MessageInfo &aMessageInfo) +void Server::HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) { Error error = kErrorNone; Coap::Message *response = nullptr; VerifyOrExit(aMessage.IsConfirmablePostRequest(), error = kErrorDrop); - Log(kMessageReceive, kUriDiagnosticGetRequest, aMessageInfo.GetPeerAddr()); + + LogInfo("Received %s from %s", UriToString(), + aMessageInfo.GetPeerAddr().ToString().AsCString()); response = Get().NewResponseMessage(aMessage); VerifyOrExit(response != nullptr, error = kErrorNoBufs); @@ -484,22 +384,16 @@ exit: FreeMessageOnError(response, error); } -Error NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination, - const uint8_t aTlvTypes[], - uint8_t aCount) -{ - return SendCommand(kUriDiagnosticReset, aDestination, aTlvTypes, aCount); -} - -template <> -void NetworkDiagnostic::HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) +template <> void Server::HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) { uint16_t offset = 0; uint8_t type; Tlv tlv; VerifyOrExit(aMessage.IsConfirmablePostRequest()); - Log(kMessageReceive, kUriDiagnosticReset, aMessageInfo.GetPeerAddr()); + + LogInfo("Received %s from %s", UriToString(), + aMessageInfo.GetPeerAddr().ToString().AsCString()); SuccessOrExit(aMessage.Read(aMessage.GetOffset(), tlv)); @@ -528,6 +422,121 @@ exit: return; } +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE + +//--------------------------------------------------------------------------------------------------------------------- +// Client + +Client::Client(Instance &aInstance) + : InstanceLocator(aInstance) +{ +} + +Error Client::SendDiagnosticGet(const Ip6::Address &aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount, + GetCallback aCallback, + void *aContext) +{ + Error error; + + if (aDestination.IsMulticast()) + { + error = SendCommand(kUriDiagnosticGetQuery, aDestination, aTlvTypes, aCount); + } + else + { + error = SendCommand(kUriDiagnosticGetRequest, aDestination, aTlvTypes, aCount, &HandleGetResponse, this); + } + + SuccessOrExit(error); + + mGetCallback.Set(aCallback, aContext); + +exit: + return error; +} + +Error Client::SendCommand(Uri aUri, + const Ip6::Address &aDestination, + const uint8_t aTlvTypes[], + uint8_t aCount, + Coap::ResponseHandler aHandler, + void *aContext) +{ + Error error; + Coap::Message *message = nullptr; + Tmf::MessageInfo messageInfo(GetInstance()); + + switch (aUri) + { + case kUriDiagnosticGetQuery: + message = Get().NewNonConfirmablePostMessage(aUri); + break; + + case kUriDiagnosticGetRequest: + case kUriDiagnosticReset: + message = Get().NewConfirmablePostMessage(aUri); + break; + + default: + OT_ASSERT(false); + } + + VerifyOrExit(message != nullptr, error = kErrorNoBufs); + + if (aCount > 0) + { + SuccessOrExit(error = Tlv::Append(*message, aTlvTypes, aCount)); + } + + Get().PrepareMessageInfoForDest(aDestination, messageInfo); + + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, aHandler, aContext)); + + LogInfo("Sent %s to %s", UriToString(aUri), aDestination.ToString().AsCString()); + +exit: + FreeMessageOnError(message, error); + return error; +} + +void Client::HandleGetResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, Error aResult) +{ + static_cast(aContext)->HandleGetResponse(AsCoapMessagePtr(aMessage), AsCoreTypePtr(aMessageInfo), + aResult); +} + +void Client::HandleGetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult) +{ + SuccessOrExit(aResult); + VerifyOrExit(aMessage->GetCode() == Coap::kCodeChanged, aResult = kErrorFailed); + +exit: + mGetCallback.InvokeIfSet(aResult, aMessage, aMessageInfo); +} + +template <> +void Client::HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) +{ + VerifyOrExit(aMessage.IsConfirmablePostRequest()); + + LogInfo("Received %s from %s", ot::UriToString(), + aMessageInfo.GetPeerAddr().ToString().AsCString()); + + mGetCallback.InvokeIfSet(kErrorNone, &aMessage, &aMessageInfo); + + IgnoreError(Get().SendEmptyAck(aMessage, aMessageInfo)); + +exit: + return; +} + +Error Client::SendDiagnosticReset(const Ip6::Address &aDestination, const uint8_t aTlvTypes[], uint8_t aCount) +{ + return SendCommand(kUriDiagnosticReset, aDestination, aTlvTypes, aCount); +} + static void ParseRoute(const RouteTlv &aRouteTlv, otNetworkDiagRoute &aNetworkDiagRoute) { uint8_t routeCount = 0; @@ -561,7 +570,7 @@ static inline void ParseMacCounters(const MacCountersTlv &aMacCountersTlv, otNet aMacCounters.mIfOutDiscards = aMacCountersTlv.GetIfOutDiscards(); } -Error NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator, TlvInfo &aTlvInfo) +Error Client::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator, TlvInfo &aTlvInfo) { Error error = kErrorNotFound; uint16_t offset = (aIterator == 0) ? aMessage.GetOffset() : aIterator; @@ -775,7 +784,7 @@ exit: #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) -const char *NetworkDiagnostic::UriToString(Uri aUri) +const char *Client::UriToString(Uri aUri) { const char *str = ""; @@ -790,9 +799,6 @@ const char *NetworkDiagnostic::UriToString(Uri aUri) case kUriDiagnosticReset: str = ot::UriToString(); break; - case kUriDiagnosticGetAnswer: - str = ot::UriToString(); - break; default: break; } @@ -800,29 +806,10 @@ const char *NetworkDiagnostic::UriToString(Uri aUri) return str; } -void NetworkDiagnostic::Log(Action aAction, Uri aUri, const Ip6::Address &aIp6Address) const -{ - static const char *const kActionStrings[] = { - "Sent", // (0) kMessageSend - "Received", // (1) kMessageReceive - }; - - static const char *const kActionPrepositionStrings[] = { - "to", // (0) kMessageSend - "from", // (1) kMessageReceive - }; - - static_assert(kMessageSend == 0, "kMessageSend value is incorrect"); - static_assert(kMessageReceive == 1, "kMessageReceive value is incorrect"); - - LogInfo("%s %s %s %s", kActionStrings[aAction], UriToString(aUri), kActionPrepositionStrings[aAction], - aIp6Address.ToString().AsCString()); -} - #endif // #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) +#endif // OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE + } // namespace NetworkDiagnostic } // namespace ot - -#endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE diff --git a/src/core/thread/network_diagnostic.hpp b/src/core/thread/network_diagnostic.hpp index 40259443d..2521418e4 100644 --- a/src/core/thread/network_diagnostic.hpp +++ b/src/core/thread/network_diagnostic.hpp @@ -36,8 +36,6 @@ #include "openthread-core-config.h" -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - #include #include "common/callback.hpp" @@ -61,11 +59,50 @@ namespace NetworkDiagnostic { * @{ */ +class Client; + /** - * This class implements the Network Diagnostic processing. + * This class implements the Network Diagnostic server responding to requests. * */ -class NetworkDiagnostic : public InstanceLocator, private NonCopyable +class Server : public InstanceLocator, private NonCopyable +{ + friend class Tmf::Agent; + friend class Client; + +public: + /** + * This constructor initializes the Server. + * + * @param[in] aInstance The OpenThread instance. + * + */ + explicit Server(Instance &aInstance); + +private: + static constexpr uint16_t kMaxChildEntries = 398; + + Error AppendDiagTlv(uint8_t aTlvType, Message &aMessage); + Error AppendIp6AddressList(Message &aMessage); + Error AppendMacCounters(Message &aMessage); + Error AppendChildTable(Message &aMessage); + Error AppendRequestedTlvs(const Message &aRequest, Message &aResponse); + void PrepareMessageInfoForDest(const Ip6::Address &aDestination, Tmf::MessageInfo &aMessageInfo) const; + + template void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); +}; + +DeclareTmfHandler(Server, kUriDiagnosticGetRequest); +DeclareTmfHandler(Server, kUriDiagnosticGetQuery); +DeclareTmfHandler(Server, kUriDiagnosticGetAnswer); + +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE + +/** + * This class implements the Network Diagnostic client sending requests and queries. + * + */ +class Client : public InstanceLocator, private NonCopyable { friend class Tmf::Agent; @@ -78,10 +115,12 @@ public: static constexpr Iterator kIteratorInit = OT_NETWORK_DIAGNOSTIC_ITERATOR_INIT; ///< Initializer for Iterator. /** - * This constructor initializes the object. + * This constructor initializes the Client. + * + * @param[in] aInstance The OpenThread instance. * */ - explicit NetworkDiagnostic(Instance &aInstance); + explicit Client(Instance &aInstance); /** * This method sends Diagnostic Get request. If the @p aDestination is of multicast type, the DIAG_GET.qry @@ -127,12 +166,6 @@ public: private: static constexpr uint16_t kMaxChildEntries = 398; - enum Action : uint8_t - { - kMessageSend, - kMessageReceive, - }; - Error SendCommand(Uri aUri, const Ip6::Address &aDestination, const uint8_t aTlvTypes[], @@ -140,13 +173,6 @@ private: Coap::ResponseHandler aHandler = nullptr, void *aContext = nullptr); - Error AppendDiagTlv(uint8_t aTlvType, Message &aMessage); - Error AppendIp6AddressList(Message &aMessage); - Error AppendMacCounters(Message &aMessage); - Error AppendChildTable(Message &aMessage); - Error AppendRequestedTlvs(const Message &aRequest, Message &aResponse); - void PrepareMessageInfoForDest(const Ip6::Address &aDestination, Tmf::MessageInfo &aMessageInfo) const; - static void HandleGetResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, @@ -157,18 +183,14 @@ private: #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) static const char *UriToString(Uri aUri); - void Log(Action aAction, Uri aUri, const Ip6::Address &aIp6Address) const; -#else - void Log(Action, Uri, const Ip6::Address &) const {} #endif Callback mGetCallback; }; -DeclareTmfHandler(NetworkDiagnostic, kUriDiagnosticGetRequest); -DeclareTmfHandler(NetworkDiagnostic, kUriDiagnosticGetQuery); -DeclareTmfHandler(NetworkDiagnostic, kUriDiagnosticGetAnswer); -DeclareTmfHandler(NetworkDiagnostic, kUriDiagnosticReset); +DeclareTmfHandler(Client, kUriDiagnosticReset); + +#endif // OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE /** * @} @@ -177,6 +199,4 @@ DeclareTmfHandler(NetworkDiagnostic, kUriDiagnosticReset); } // namespace ot -#endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - #endif // NETWORK_DIAGNOSTIC_HPP_ diff --git a/src/core/thread/tmf.cpp b/src/core/thread/tmf.cpp index fecd97853..583f6e29d 100644 --- a/src/core/thread/tmf.cpp +++ b/src/core/thread/tmf.cpp @@ -165,11 +165,12 @@ bool Agent::HandleResource(const char *aUriPath, Message &aMessage, const Ip6::M Case(kUriAnycastLocate, AnycastLocator); #endif -#if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - Case(kUriDiagnosticGetRequest, NetworkDiagnostic::NetworkDiagnostic); - Case(kUriDiagnosticGetQuery, NetworkDiagnostic::NetworkDiagnostic); - Case(kUriDiagnosticGetAnswer, NetworkDiagnostic::NetworkDiagnostic); - Case(kUriDiagnosticReset, NetworkDiagnostic::NetworkDiagnostic); + Case(kUriDiagnosticGetRequest, NetworkDiagnostic::Server); + Case(kUriDiagnosticGetQuery, NetworkDiagnostic::Server); + Case(kUriDiagnosticReset, NetworkDiagnostic::Server); + +#if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE + Case(kUriDiagnosticGetAnswer, NetworkDiagnostic::Client); #endif #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE diff --git a/src/posix/Makefile-posix b/src/posix/Makefile-posix index 5ddc67b4e..7fe9722de 100644 --- a/src/posix/Makefile-posix +++ b/src/posix/Makefile-posix @@ -61,9 +61,9 @@ LINK_RAW ?= 0 LOG_OUTPUT ?= PLATFORM_DEFINED MAC_FILTER ?= 1 MAX_POWER_TABLE ?= 1 -MTD_NETDIAG ?= 1 NEIGHBOR_DISCOVERY_AGENT ?= 1 NETDATA_PUBLISHER ?= 1 +NETDIAG_CLIENT ?= 1 PING_SENDER ?= 1 READLINE ?= readline REFERENCE_DEVICE ?= 1 diff --git a/tests/fuzz/oss-fuzz-build b/tests/fuzz/oss-fuzz-build index 60782f6c3..c8bb40789 100755 --- a/tests/fuzz/oss-fuzz-build +++ b/tests/fuzz/oss-fuzz-build @@ -62,8 +62,8 @@ set -euxo pipefail -DOT_LINK_RAW=ON \ -DOT_LOG_OUTPUT=APP \ -DOT_MAC_FILTER=ON \ - -DOT_MTD_NETDIAG=ON \ -DOT_NETDATA_PUBLISHER=ON \ + -DOT_NETDIAG_CLIENT=ON \ -DOT_PING_SENDER=ON \ -DOT_SERVICE=ON \ -DOT_SLAAC=ON \