Skip to content

Commit

Permalink
fix cancelling Border Agent discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
wgtdkp committed Jul 29, 2020
1 parent ca3abe3 commit 2d121ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/app/border_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "border_agent.hpp"

#include <atomic>
#include <chrono>
#include <thread>

Expand All @@ -42,6 +43,8 @@ namespace ot {

namespace commissioner {

static std::atomic<bool> gIsDiscoverCancelled;

struct BorderAgentOrErrorMsg
{
BorderAgent mBorderAgent;
Expand All @@ -59,6 +62,11 @@ static int HandleRecord(const struct sockaddr *from,
size_t length,
void * user_data);

void CancelDiscoverBorderAgent()
{
gIsDiscoverCancelled = true;
}

Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout)
{
static constexpr size_t kDefaultBufferSize = 1024 * 16;
Expand All @@ -67,18 +75,21 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou

Error error;
uint8_t buf[kDefaultBufferSize];
size_t borderAgentCount = 0;

auto begin = std::chrono::system_clock::now();

int socket = mdns_socket_open_ipv4();
VerifyOrExit(socket >= 0, error = ERROR_IO_ERROR("failed to open mDNS IPv4 socket"));

gIsDiscoverCancelled = false;

if (mdns_query_send(socket, kMdnsQueryType, kServiceName, strlen(kServiceName), buf, sizeof(buf)) != 0)
{
ExitNow(error = ERROR_IO_ERROR("failed to send mDNS query"));
}

while (begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now())
while (!gIsDiscoverCancelled && begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now())
{
BorderAgentOrErrorMsg curBorderAgentOrErrorMsg;

Expand All @@ -91,11 +102,15 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou
else if (curBorderAgentOrErrorMsg.mBorderAgent.mPresentFlags != 0)
{
aBorderAgentHandler(&curBorderAgentOrErrorMsg.mBorderAgent, ERROR_NONE);
++borderAgentCount;
}

std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

VerifyOrExit(!gIsDiscoverCancelled, error = ERROR_CANCELLED("Border Agent discovery was cancelled"));
VerifyOrExit(borderAgentCount != 0, error = ERROR_TIMEOUT("Found no Border Agent"));

exit:
if (socket >= 0)
{
Expand Down
8 changes: 8 additions & 0 deletions src/app/border_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ using BorderAgentHandler = std::function<void(const BorderAgent *aBorderAgent, c
*/
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout);

/**
* Cancel the call to DiscoverBorderAgent.
*
* This function is thread-safe.
*
*/
void CancelDiscoverBorderAgent();

} // namespace commissioner

} // namespace ot
Expand Down
2 changes: 2 additions & 0 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ void Interpreter::CancelCommand()
{
mCommissioner->Stop();
}

CancelDiscoverBorderAgent();
}

Interpreter::Expression Interpreter::Read()
Expand Down

0 comments on commit 2d121ae

Please sign in to comment.