Skip to content

Commit

Permalink
[cli] Update command "br scan" to allow network interface selection for
Browse files Browse the repository at this point in the history
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
<network interface>. The chosen interface will be used for mDNS binding
through socket options.
  • Loading branch information
ZhangLe2016 committed Apr 12, 2024
1 parent 6a068ab commit f364c98
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit f364c98

Please sign in to comment.