From f364c9832d786e821bced2210bb64a23b4761bed Mon Sep 17 00:00:00 2001 From: lezhan Date: Fri, 12 Apr 2024 07:10:18 +0000 Subject: [PATCH] [cli] Update command "br scan" to allow network interface selection for mDNS binding. Within the OTBR practice application, the BR host will have several interfaces. When users want to discover the border router using mDNS, they will need to choose the specific interface. This change introduces a new option for the CLI command `br scan`. User can now specify a network interface using the syntax `br scan --netif . The chosen interface will be used for mDNS binding through socket options. --- src/app/cli/interpreter.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/app/cli/interpreter.cpp b/src/app/cli/interpreter.cpp index 750046ad..cc869825 100644 --- a/src/app/cli/interpreter.cpp +++ b/src/app/cli/interpreter.cpp @@ -1433,26 +1433,33 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr) nlohmann::json baJson; char mdnsSendBuffer[kMdnsBufferSize]; - auto it = std::find(mContext.mCommandKeys.begin(), mContext.mCommandKeys.end(), "--timeout"); - - if (it != mContext.mCommandKeys.end()) + for (auto it = mContext.mCommandKeys.begin(); it != mContext.mCommandKeys.end(); ++it) { - try + if (*it == "--timeout") { - scanTimeout = stol(mContext.mCommandKeys[std::distance(mContext.mCommandKeys.begin(), it) + 1]); - } catch (...) + if (++it != mContext.mCommandKeys.end()) + { + try + { + scanTimeout = stol(*it); + } catch (...) + { + ExitNow(value = ERROR_INVALID_ARGS("Imparsable timeout value '{}'", *it)); + } + } else { + ExitNow(value = ERROR_INVALID_ARGS("Missing {} value", *--it)); + } + } else if (*it == "--netif") { - ExitNow(value = ERROR_INVALID_ARGS("Imparsable timeout value '{}'", mContext.mCommandKeys[std::distance(mContext.mCommandKeys.begin(), it)])); + if (++it != mContext.mCommandKeys.end()) + { + netIf = *it; + } else { + ExitNow(value = ERROR_INVALID_ARGS("Missing {} value", *--it)); + } } } - it = std::find(mContext.mCommandKeys.begin(), mContext.mCommandKeys.end(), "--netif"); - - if (it != mContext.mCommandKeys.end()) - { - netIf = mContext.mCommandKeys[std::distance(mContext.mCommandKeys.begin(), it) + 1]; - } - // Open IPv4 mDNS socket mdnsSocket = mdns_socket_open_ipv4(); VerifyOrExit(mdnsSocket >= 0, value = ERROR_IO_ERROR("failed to open mDNS IPv4 socket"));