Skip to content

Commit

Permalink
[api] enable DIAG_GET reset command into commissioner
Browse files Browse the repository at this point in the history
the DIAG_GET reset command is used to notify the peer device to clear
the value of the diagnostic TLV such as `MAC counters TLV` which
contains several stats of network transmission.
  • Loading branch information
ZhangLe2016 committed May 23, 2024
1 parent e72fcdd commit 5be94e9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/commissioner/commissioner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,33 @@ class Commissioner
* The GIT_REVISION is included only when the commissioner is built in a
* git repository.
*/

/**
* @brief Asynchronously reset a set of available diagnostic TLV(s) value.
*
* This method notifies a Thread device to reset a set of diagnostic value by sending DIAG_GET.rst message.
* It always returns immediately without waiting for the completion. //TBC by real test
*
* @param[in, out] aHandler A handler of all response and errors; Guaranteed to be called.
* @param[in] aRloc The RLOC of dest Thread device.
* @param[in] aDiagTlvFlags Diagnostic TLVs flags indicate which TLVs are wanted.
*/

virtual void CommandDiagRest(ErrorHandler aHandler, uint16_t aRloc, uint64_t aaDiagTlvFlags) = 0;

/**
* @brief Synchronously reset a set of available diagnostic TLV(s) value.
*
* This method notifies a Thread device to reset a set of diagnostic value by sending DIAG_GET.rst message.
* It will not return until errors happened, timeouted or succeed.
* @param[out] aRawTlvData A diagnostic TLVs data returned by the leader or other Thread Device.
* @param[in] aRloc The RLOC of dest Thread device.
* @param[in] aDiagTlvFlags Diagnostic TLVs flags indicate which TLVs are wanted.
*
*/

virtual Error CommandDiagRest(uint16_t aRloc, uint64_t aDiagTlvFlags) = 0;

static std::string GetVersion(void);
};

Expand Down
35 changes: 35 additions & 0 deletions src/library/commissioner_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,41 @@ void CommissionerImpl::SetPendingDataset(ErrorHandler aHandler, const PendingOpe
}
}

void CommissionerImpl::CommandDiagReset(ErrorHandler aHandler, uint16_t aRloc, uint64_t aDiagTlvFlags)
{
Error error;
coap::Request request{coap::Type::kConfirmable, coap::Code::kPost};
auto onResponse = [aHandler](const coap::Response *aResponse, Error aError) {
aHandler(HandleStateResponse(aResponse, aError));
};

VerifyOrExit(IsActive(), error = ERROR_INVALID_STATE("commissioner is not active"));
SuccessOrExit(error = request.SetUriPath(uri::kDiagRst));
SuccessOrExit(error = AppendTlv(request, {tlv::Type::kNetworkDiagTypeList, GetDiagTypeListTlvs(aDiagTlvFlags),
tlv::Scope::kNetworkDiag}));

#if OT_COMM_CONFIG_CCM_ENABLE
if (IsCcmMode())
{
SuccessOrExit(error = SignRequest(request));
}
#endif
if (aRloc == 0)
{
aRloc = kLeaderAloc16;
}

LOG_DEBUG(LOG_REGION_DIAG, "sending DIAG_GET.rst");
mProxyClient.SendRequest(request, onResponse, aRloc, kDefaultMmPort);
LOG_DEBUG(LOG_REGION_DIAG, "sent DIAG_GET.rst");

exit:
if (error != ErrorCode::kNone)
{
aHandler(error);
}
}

#if OT_COMM_CONFIG_CCM_ENABLE
void CommissionerImpl::SetBbrDataset(ErrorHandler aHandler, const BbrDataset &aDataset)
{
Expand Down
3 changes: 3 additions & 0 deletions src/library/commissioner_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ class CommissionerImpl : public Commissioner

struct event_base *GetEventBase() { return mEventBase; }

void CommandDiagReset(ErrorHandler aHandler, uint16_t aRloc, uint64_t aaDiagTlvFlags) override;
Error CommandDiagReset(uint16_t, uint64_t) override { return ERROR_UNIMPLEMENTED(""); }

private:
using AsyncRequest = std::function<void()>;

Expand Down
20 changes: 20 additions & 0 deletions src/library/commissioner_safe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,26 @@ void CommissionerSafe::Invoke(evutil_socket_t, short, void *aContext)
}
}

void CommissionerSafe::CommandDiagReset(ErrorHandler aHandler, uint16_t aRloc, uint64_t aDiagTlvFlags)
{
PushAsyncRequest([=]() { mImpl->CommandDiagReset(aHandler, aRloc, aDiagTlvFlags); });
}

Error CommissionerSafe::CommandDiagReset(uint16_t aRloc, uint64_t aDiagTlvFlags)
{
std::promise<Error> pro;
auto wait = [&pro](Error error) { pro.set_value(error); };

CommandDiagReset(wait, aRloc, aDiagTlvFlags);
std::future<Error> fut = pro.get_future();

if (fut.wait_for(std::chrono::seconds(10)) == std::future_status::timeout)
{
return ERROR_TIMEOUT("request to 10 timeout");
}
return fut.get();
}

void CommissionerSafe::PushAsyncRequest(AsyncRequest &&aAsyncRequest)
{
std::lock_guard<std::mutex> _(mInvokeMutex);
Expand Down
3 changes: 3 additions & 0 deletions src/library/commissioner_safe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ class CommissionerSafe : public Commissioner

Error SetToken(const ByteArray &aSignedToken) override;

void CommandDiagReset(ErrorHandler aHandler, uint16_t aRloc, uint64_t aaDiagTlvFlags) override;
Error CommandDiagReset(uint16_t aRloc, uint64_t aDiagTlvFlags) override;

private:
using AsyncRequest = std::function<void()>;

Expand Down

0 comments on commit 5be94e9

Please sign in to comment.