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"