Skip to content

Commit 88ee104

Browse files
committed
fix cancelling Border Agent discovery
1 parent 2808299 commit 88ee104

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/app/border_agent.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "border_agent.hpp"
3030

31+
#include <atomic>
3132
#include <chrono>
3233
#include <thread>
3334

@@ -42,6 +43,8 @@ namespace ot {
4243

4344
namespace commissioner {
4445

46+
static std::atomic<bool> gIsDiscoverCancelled;
47+
4548
struct BorderAgentOrErrorMsg
4649
{
4750
BorderAgent mBorderAgent;
@@ -59,6 +62,11 @@ static int HandleRecord(const struct sockaddr *from,
5962
size_t length,
6063
void * user_data);
6164

65+
void CancelDiscoverBorderAgent()
66+
{
67+
gIsDiscoverCancelled = true;
68+
}
69+
6270
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout)
6371
{
6472
static constexpr size_t kDefaultBufferSize = 1024 * 16;
@@ -67,18 +75,21 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou
6775

6876
Error error;
6977
uint8_t buf[kDefaultBufferSize];
78+
size_t borderAgentCount = 0;
7079

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

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

85+
gIsDiscoverCancelled = false;
86+
7687
if (mdns_query_send(socket, kMdnsQueryType, kServiceName, strlen(kServiceName), buf, sizeof(buf)) != 0)
7788
{
7889
ExitNow(error = ERROR_IO_ERROR("failed to send mDNS query"));
7990
}
8091

81-
while (begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now())
92+
while (!gIsDiscoverCancelled && begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now())
8293
{
8394
BorderAgentOrErrorMsg curBorderAgentOrErrorMsg;
8495

@@ -91,11 +102,15 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou
91102
else if (curBorderAgentOrErrorMsg.mBorderAgent.mPresentFlags != 0)
92103
{
93104
aBorderAgentHandler(&curBorderAgentOrErrorMsg.mBorderAgent, ERROR_NONE);
105+
++borderAgentCount;
94106
}
95107

96108
std::this_thread::sleep_for(std::chrono::milliseconds(100));
97109
}
98110

111+
VerifyOrExit(!gIsDiscoverCancelled, error = ERROR_CANCELLED("Border Agent discovery was cancelled"));
112+
VerifyOrExit(borderAgentCount != 0, error = ERROR_TIMEOUT("Found no Border Agent"));
113+
99114
exit:
100115
if (socket >= 0)
101116
{

src/app/border_agent.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ using BorderAgentHandler = std::function<void(const BorderAgent *aBorderAgent, c
177177
*/
178178
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout);
179179

180+
/**
181+
* Cancel the call to DiscoverBorderAgent.
182+
*
183+
* This function is thread-safe.
184+
*
185+
*/
186+
void CancelDiscoverBorderAgent();
187+
180188
} // namespace commissioner
181189

182190
} // namespace ot

src/app/cli/interpreter.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ void Interpreter::AbortCommand()
207207
{
208208
mCommissioner->Stop();
209209
}
210+
211+
CancelDiscoverBorderAgent();
210212
}
211213

212214
Interpreter::Expression Interpreter::Read()

0 commit comments

Comments
 (0)