From 6bb5eb50d057f1b60bd851fbffab5c2401df2a71 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 3 Jan 2023 21:14:36 -0800 Subject: [PATCH] [cli] new command to enable/disable discovery request callback (#8579) This commit add `discover reqcallback {enable/disable}` CLI command which registers callback to output whenever device gets a "MLE Discovery Request". This command allows the callback to be explicitly set when needed and avoid having it be enabled by default (since the extra output from this callback can be treated as unexpected result by tests that parse the CLI output). --- src/cli/cli.cpp | 41 +++++++++++++++++++--- src/cli/cli.hpp | 9 +++-- tests/scripts/expect/cli-scan-discover.exp | 2 ++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 2524ad0ab..cbebd903b 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -145,10 +145,6 @@ Interpreter::Interpreter(Instance *aInstance, otCliOutputCallback aCallback, voi #endif #endif // OPENTHREAD_FTD || OPENTHREAD_MTD { -#if OPENTHREAD_FTD - otThreadSetDiscoveryRequestCallback(GetInstancePtr(), &Interpreter::HandleDiscoveryRequest, this); -#endif - OutputPrompt(); } @@ -3024,6 +3020,36 @@ template <> otError Interpreter::Process(Arg aArgs[]) otError error = OT_ERROR_NONE; uint32_t scanChannels = 0; +#if OPENTHREAD_FTD + /** + * @cli discover reqcallback (enable,disable) + * @code + * discover reqcallback enable + * Done + * @endcode + * @cparam discover reqcallback @ca{enable|disable} + * @par api_copy + * #otThreadSetDiscoveryRequestCallback + */ + if (aArgs[0] == "reqcallback") + { + bool enable; + otThreadDiscoveryRequestCallback callback = nullptr; + void *context = nullptr; + + SuccessOrExit(error = ParseEnableOrDisable(aArgs[1], enable)); + + if (enable) + { + callback = &Interpreter::HandleDiscoveryRequest; + context = this; + } + + otThreadSetDiscoveryRequestCallback(GetInstancePtr(), callback, context); + ExitNow(); + } +#endif // OPENTHREAD_FTD + if (!aArgs[0].IsEmpty()) { uint8_t channel; @@ -7155,12 +7181,19 @@ void Interpreter::HandleDetachGracefullyResult(void) OutputResult(OT_ERROR_NONE); } +#if OPENTHREAD_FTD +void Interpreter::HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo *aInfo, void *aContext) +{ + static_cast(aContext)->HandleDiscoveryRequest(*aInfo); +} + void Interpreter::HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo &aInfo) { OutputFormat("~ Discovery Request from "); OutputExtAddress(aInfo.mExtAddress); OutputLine(": version=%u,joiner=%d", aInfo.mVersion, aInfo.mIsJoiner); } +#endif #endif // OPENTHREAD_FTD || OPENTHREAD_MTD diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index d3b5a6ae2..478c3fa68 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -503,11 +503,10 @@ private: static void HandleDetachGracefullyResult(void *aContext); void HandleDetachGracefullyResult(void); - static void HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo *aInfo, void *aContext) - { - static_cast(aContext)->HandleDiscoveryRequest(*aInfo); - } - void HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo &aInfo); +#if OPENTHREAD_FTD + static void HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo *aInfo, void *aContext); + void HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo &aInfo); +#endif #endif // OPENTHREAD_FTD || OPENTHREAD_MTD diff --git a/tests/scripts/expect/cli-scan-discover.exp b/tests/scripts/expect/cli-scan-discover.exp index 78226c77d..52b6883f8 100755 --- a/tests/scripts/expect/cli-scan-discover.exp +++ b/tests/scripts/expect/cli-scan-discover.exp @@ -57,6 +57,8 @@ expect "channel" expect -re {(\d+)} set channel $expect_out(1,string) expect_line "Done" +send "discover reqcallback enable\n" +expect_line "Done" switch_node 3 send "scan $channel\n"