Skip to content

Commit

Permalink
Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyirio committed Jun 26, 2024
1 parent 3873c6f commit 342f009
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/cli/cli_coap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <ctype.h>

#include "cli/cli.hpp"
#include "cli/cli_utils.hpp"
#include "common/timer.hpp"

namespace ot {
namespace Cli {
Expand All @@ -48,6 +50,7 @@ Coap::Coap(otInstance *aInstance, OutputImplementer &aOutputImplementer)
: Utils(aInstance, aOutputImplementer)
, mUseDefaultRequestTxParameters(true)
, mUseDefaultResponseTxParameters(true)
, mTimer(AsCoreType(aInstance), HandleTimer, this)
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
, mObserveSerial(0)
, mRequestTokenLength(0)
Expand Down Expand Up @@ -138,6 +141,12 @@ void Coap::PrintPayload(otMessage *aMessage)
}
}

if (mIsDemo)
{
mDemoIntervalMilli = 0;
mDemoIntervalMilli = buf[bytesPrinted - 1] | (buf[bytesPrinted - 2] << 8);
}

OutputNewLine();
}

Expand Down Expand Up @@ -496,6 +505,15 @@ template <> otError Coap::Process<Cmd("get")>(Arg aArgs[]) { return ProcessReque
*/
template <> otError Coap::Process<Cmd("post")>(Arg aArgs[]) { return ProcessRequest(aArgs, OT_COAP_CODE_POST); }

template <> otError Coap::Process<Cmd("demo")>(Arg aArgs[]) {
mIsDemo = true;
mDemoCount = 0;
for (uint16_t i = 0; i < 4; i++) {
mDemoArgs[i].Clear();
mDemoArgs[i].SetCString(aArgs[i].GetCString());
}
return ProcessRequest(aArgs, OT_COAP_CODE_POST); }

/**
* @cli coap put
* @code
Expand Down Expand Up @@ -584,6 +602,7 @@ otError Coap::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode)
otMessage *message = nullptr;
otMessageInfo messageInfo;
uint16_t payloadLength = 0;
bool nat64Synth;

// Default parameters
char coapUri[kMaxUriLength] = "test";
Expand All @@ -602,7 +621,13 @@ otError Coap::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode)
}
#endif

SuccessOrExit(error = aArgs[0].ParseAsIp6Address(coapDestinationIp));
SuccessOrExit(
error = ParseToIp6Address(GetInstancePtr(), aArgs[0], coapDestinationIp, nat64Synth));
if (nat64Synth)
{
OutputFormat("Sending request to synthesized IPv6 address: ");
OutputIp6AddressLine(coapDestinationIp);
}

VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(aArgs[1].GetLength() < sizeof(coapUri), error = OT_ERROR_INVALID_ARGS);
Expand Down Expand Up @@ -792,6 +817,7 @@ otError Coap::Process(Arg aArgs[])
CmdEntry("cancel"),
#endif
CmdEntry("delete"),
CmdEntry("demo"),
CmdEntry("get"),
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
CmdEntry("observe"),
Expand Down Expand Up @@ -1089,6 +1115,38 @@ void Coap::HandleResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo
}
#endif
PrintPayload(aMessage);
if (mIsDemo) { SendDemoRequest(); }
}
}

void Coap::SendDemoRequest()
{
if (mDemoCount < 10)
{
mDemoCount++;
mTimer.Start(mDemoIntervalMilli);
OutputLine("Waiting for %i milliseconds", mDemoIntervalMilli);
OutputNewLine();
}
else {
mIsDemo = false;
mDemoCount = 0;
}
}

void Coap::HandleTimer(Timer &aTimer)
{
static_cast<Coap *>(static_cast<TimerMilliContext &>(aTimer).GetContext())->HandleTimer();
}

void Coap::HandleTimer(void)
{
OutputLine("Time up! Resending request #%i ...", mDemoCount);
OutputNewLine();
otError error = ProcessRequest(mDemoArgs, OT_COAP_CODE_POST);
if (error != OT_ERROR_NONE) {
OutputLine("Something went wrong. Failed to send request");
SendDemoRequest();
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/cli/cli_coap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <openthread/coap.h>

#include "cli/cli_utils.hpp"
#include "common/timer.hpp"

namespace ot {
namespace Cli {
Expand Down Expand Up @@ -97,6 +98,10 @@ class Coap : private Utils

void PrintPayload(otMessage *aMessage);

static void HandleTimer(Timer &aTimer);
void HandleTimer(void);
void SendDemoRequest();

#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve = false);
#else
Expand Down Expand Up @@ -154,6 +159,12 @@ class Coap : private Utils
otCoapTxParameters mRequestTxParameters;
otCoapTxParameters mResponseTxParameters;

bool mIsDemo = false;
Arg mDemoArgs[4];
uint16_t mDemoIntervalMilli = 0;
uint16_t mDemoCount = 0;
TimerMilliContext mTimer;

#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapBlockwiseResource mResource;
#else
Expand Down

0 comments on commit 342f009

Please sign in to comment.