mirror of
https://github.com/espressif/openthread.git
synced 2026-10-17 21:18:13 +00:00
[cli] enhance Cli::Coap and CoapSecure (#6707)
This commit simplifies the handling of request commands (get, post, put, delete, and observe) in `Cli::Coap` and `Cli:CoapSecure` classes. It also updates parsing and processing of arguments for `CoapSecure` request command by removing the optional and actually unused dest IPv6 address argument. Finally, it removes the use of OT core headers/types (mainly the `ot:Coap::Message::BlockType` enumeration) in CLI CoAP modules.
This commit is contained in:
+53
-75
@@ -40,7 +40,6 @@
|
||||
#include <ctype.h>
|
||||
|
||||
#include "cli/cli.hpp"
|
||||
#include "coap/coap_message.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Cli {
|
||||
@@ -353,7 +352,38 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
otError Coap::ProcessGet(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_GET);
|
||||
}
|
||||
|
||||
otError Coap::ProcessPost(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_POST);
|
||||
}
|
||||
|
||||
otError Coap::ProcessPut(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_PUT);
|
||||
}
|
||||
|
||||
otError Coap::ProcessDelete(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_DELETE);
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
otError Coap::ProcessObserve(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_GET, /* aCoapObserve */ true);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve)
|
||||
#else
|
||||
otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode)
|
||||
#endif
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otMessage * message = nullptr;
|
||||
@@ -363,78 +393,26 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
// Default parameters
|
||||
char coapUri[kMaxUriLength] = "test";
|
||||
otCoapType coapType = OT_COAP_TYPE_NON_CONFIRMABLE;
|
||||
otCoapCode coapCode = OT_COAP_CODE_GET;
|
||||
otIp6Address coapDestinationIp;
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
bool coapObserve = false;
|
||||
#endif
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
bool coapBlock = false;
|
||||
otCoapBlockSzx coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_16;
|
||||
ot::Coap::Message::BlockType coapBlockType = ot::Coap::Message::kBlockType1;
|
||||
bool coapBlock = false;
|
||||
otCoapBlockSzx coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_16;
|
||||
BlockType coapBlockType = (aCoapCode == OT_COAP_CODE_GET) ? kBlockType2 : kBlockType1;
|
||||
#endif
|
||||
|
||||
VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS);
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE && OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
if (aCoapObserve)
|
||||
{
|
||||
coapBlockType = kBlockType1;
|
||||
}
|
||||
#endif
|
||||
|
||||
// CoAP-Code
|
||||
if (aArgs[0] == "get")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_GET;
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
coapBlockType = ot::Coap::Message::kBlockType2;
|
||||
#endif
|
||||
}
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
else if (aArgs[0] == "observe")
|
||||
{
|
||||
// Observe request. This is a GET with Observe=0
|
||||
coapCode = OT_COAP_CODE_GET;
|
||||
coapObserve = true;
|
||||
}
|
||||
#endif
|
||||
else if (aArgs[0] == "post")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_POST;
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
coapBlockType = ot::Coap::Message::kBlockType1;
|
||||
#endif
|
||||
}
|
||||
else if (aArgs[0] == "put")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_PUT;
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
coapBlockType = ot::Coap::Message::kBlockType1;
|
||||
#endif
|
||||
}
|
||||
else if (aArgs[0] == "delete")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_DELETE;
|
||||
}
|
||||
else
|
||||
{
|
||||
ExitNow(error = OT_ERROR_INVALID_ARGS);
|
||||
}
|
||||
VerifyOrExit(aArgsLength > 2, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
// Destination IPv6 address
|
||||
if (aArgsLength > 1)
|
||||
{
|
||||
SuccessOrExit(error = aArgs[1].ParseAsIp6Address(coapDestinationIp));
|
||||
}
|
||||
else
|
||||
{
|
||||
ExitNow(error = OT_ERROR_INVALID_ARGS);
|
||||
}
|
||||
SuccessOrExit(error = aArgs[1].ParseAsIp6Address(coapDestinationIp));
|
||||
|
||||
// CoAP-URI
|
||||
if (aArgsLength > 2)
|
||||
{
|
||||
VerifyOrExit(aArgs[2].GetLength() < kMaxUriLength, error = OT_ERROR_INVALID_ARGS);
|
||||
strncpy(coapUri, aArgs[2].GetCString(), sizeof(coapUri) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ExitNow(error = OT_ERROR_INVALID_ARGS);
|
||||
}
|
||||
VerifyOrExit(aArgs[2].GetLength() < kMaxUriLength, error = OT_ERROR_INVALID_ARGS);
|
||||
strncpy(coapUri, aArgs[2].GetCString(), sizeof(coapUri) - 1);
|
||||
|
||||
// CoAP-Type
|
||||
if (aArgsLength > 3)
|
||||
@@ -490,7 +468,7 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
if (coapObserve && mRequestTokenLength)
|
||||
if (aCoapObserve && mRequestTokenLength)
|
||||
{
|
||||
// New observe request, cancel any existing observation
|
||||
SuccessOrExit(error = CancelResourceSubscription());
|
||||
@@ -500,11 +478,11 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
message = otCoapNewMessage(mInterpreter.mInstance, nullptr);
|
||||
VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS);
|
||||
|
||||
otCoapMessageInit(message, coapType, coapCode);
|
||||
otCoapMessageInit(message, coapType, aCoapCode);
|
||||
otCoapMessageGenerateToken(message, OT_COAP_DEFAULT_TOKEN_LENGTH);
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
if (coapObserve)
|
||||
if (aCoapObserve)
|
||||
{
|
||||
SuccessOrExit(error = otCoapMessageAppendObserveOption(message, 0));
|
||||
}
|
||||
@@ -515,7 +493,7 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
if (coapBlock)
|
||||
{
|
||||
if (coapBlockType == ot::Coap::Message::kBlockType1)
|
||||
if (coapBlockType == kBlockType1)
|
||||
{
|
||||
SuccessOrExit(error = otCoapMessageAppendBlock1Option(message, 0, true, coapBlockSize));
|
||||
}
|
||||
@@ -558,7 +536,7 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
messageInfo.mPeerPort = OT_DEFAULT_COAP_PORT;
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
if (coapObserve)
|
||||
if (aCoapObserve)
|
||||
{
|
||||
// Make a note of the message details for later so we can cancel it later.
|
||||
memcpy(&mRequestAddr, &coapDestinationIp, sizeof(mRequestAddr));
|
||||
@@ -569,12 +547,12 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((coapType == OT_COAP_TYPE_CONFIRMABLE) || (coapCode == OT_COAP_CODE_GET))
|
||||
if ((coapType == OT_COAP_TYPE_CONFIRMABLE) || (aCoapCode == OT_COAP_CODE_GET))
|
||||
{
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
if (coapBlock)
|
||||
{
|
||||
if (coapCode == OT_COAP_CODE_PUT || coapCode == OT_COAP_CODE_POST)
|
||||
if (aCoapCode == OT_COAP_CODE_PUT || aCoapCode == OT_COAP_CODE_POST)
|
||||
{
|
||||
SuccessOrExit(error = otCoapMessageSetPayloadMarker(message));
|
||||
}
|
||||
@@ -657,7 +635,7 @@ void Coap::HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo)
|
||||
SuccessOrExit(error = otCoapOptionIteratorInit(&iterator, aMessage));
|
||||
#endif
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
if (otCoapOptionIteratorGetFirstOptionMatching(&iterator, OT_COAP_OPTION_OBSERVE) != NULL)
|
||||
if (otCoapOptionIteratorGetFirstOptionMatching(&iterator, OT_COAP_OPTION_OBSERVE) != nullptr)
|
||||
{
|
||||
SuccessOrExit(error = otCoapOptionIteratorGetOptionUintValue(&iterator, &observe));
|
||||
observePresent = true;
|
||||
|
||||
+28
-7
@@ -38,7 +38,8 @@
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_API_ENABLE
|
||||
|
||||
#include "coap/coap_message.hpp"
|
||||
#include <openthread/coap.h>
|
||||
|
||||
#include "utils/lookup_table.hpp"
|
||||
#include "utils/parse_cmdline.hpp"
|
||||
|
||||
@@ -86,6 +87,14 @@ private:
|
||||
otError (Coap::*mHandler)(uint8_t aArgsLength, Arg aArgs[]);
|
||||
};
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
enum BlockType : uint8_t
|
||||
{
|
||||
kBlockType1,
|
||||
kBlockType2,
|
||||
};
|
||||
#endif
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
otError CancelResourceSubscription(void);
|
||||
void CancelSubscriber(void);
|
||||
@@ -96,14 +105,26 @@ private:
|
||||
otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]);
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
otError ProcessCancel(uint8_t aArgsLength, Arg aArgs[]);
|
||||
#endif
|
||||
otError ProcessDelete(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessGet(uint8_t aArgsLength, Arg aArgs[]);
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
otError ProcessObserve(uint8_t aArgsLength, Arg aArgs[]);
|
||||
#endif
|
||||
otError ProcessParameters(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessPost(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessPut(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessResource(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessSet(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessStart(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessStop(uint8_t aArgsLength, Arg aArgs[]);
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve = false);
|
||||
#else
|
||||
otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode);
|
||||
#endif
|
||||
|
||||
static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
|
||||
void HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo);
|
||||
|
||||
@@ -153,15 +174,15 @@ private:
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
{"cancel", &Coap::ProcessCancel},
|
||||
#endif
|
||||
{"delete", &Coap::ProcessRequest},
|
||||
{"get", &Coap::ProcessRequest},
|
||||
{"delete", &Coap::ProcessDelete},
|
||||
{"get", &Coap::ProcessGet},
|
||||
{"help", &Coap::ProcessHelp},
|
||||
#if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
|
||||
{"observe", &Coap::ProcessRequest},
|
||||
{"observe", &Coap::ProcessObserve},
|
||||
#endif
|
||||
{"parameters", &Coap::ProcessParameters},
|
||||
{"post", &Coap::ProcessRequest},
|
||||
{"put", &Coap::ProcessRequest},
|
||||
{"post", &Coap::ProcessPost},
|
||||
{"put", &Coap::ProcessPut},
|
||||
{"resource", &Coap::ProcessResource},
|
||||
{"set", &Coap::ProcessSet},
|
||||
{"start", &Coap::ProcessStart},
|
||||
|
||||
+46
-92
@@ -36,7 +36,6 @@
|
||||
#if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
|
||||
|
||||
#include <mbedtls/debug.h>
|
||||
#include <openthread/ip6.h>
|
||||
#include <openthread/random_noncrypto.h>
|
||||
|
||||
#include "cli/cli.hpp"
|
||||
@@ -216,130 +215,90 @@ otError CoapSecure::ProcessStop(uint8_t aArgsLength, Arg aArgs[])
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
otError CoapSecure::ProcessGet(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otMessage * message = nullptr;
|
||||
otMessageInfo messageInfo;
|
||||
uint16_t payloadLength = 0;
|
||||
uint8_t indexShifter = 0;
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_GET);
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessPost(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_POST);
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessPut(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_PUT);
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessDelete(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_DELETE);
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otMessage *message = nullptr;
|
||||
uint16_t payloadLength = 0;
|
||||
|
||||
// Default parameters
|
||||
char coapUri[kMaxUriLength] = "test";
|
||||
otCoapType coapType = OT_COAP_TYPE_NON_CONFIRMABLE;
|
||||
otCoapCode coapCode = OT_COAP_CODE_GET;
|
||||
otIp6Address coapDestinationIp;
|
||||
char coapUri[kMaxUriLength] = "test";
|
||||
otCoapType coapType = OT_COAP_TYPE_NON_CONFIRMABLE;
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
bool coapBlock = false;
|
||||
otCoapBlockSzx coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_16;
|
||||
ot::Coap::Message::BlockType coapBlockType = ot::Coap::Message::kBlockType1;
|
||||
bool coapBlock = false;
|
||||
otCoapBlockSzx coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_16;
|
||||
BlockType coapBlockType = (aCoapCode == OT_COAP_CODE_GET) ? kBlockType2 : kBlockType1;
|
||||
#endif
|
||||
|
||||
VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
// CoAP-Code
|
||||
if (aArgs[0] == "get")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_GET;
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
coapBlockType = ot::Coap::Message::kBlockType2;
|
||||
#endif
|
||||
}
|
||||
else if (aArgs[0] == "post")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_POST;
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
coapBlockType = ot::Coap::Message::kBlockType1;
|
||||
#endif
|
||||
}
|
||||
else if (aArgs[0] == "put")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_PUT;
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
coapBlockType = ot::Coap::Message::kBlockType1;
|
||||
#endif
|
||||
}
|
||||
else if (aArgs[0] == "delete")
|
||||
{
|
||||
coapCode = OT_COAP_CODE_DELETE;
|
||||
}
|
||||
else
|
||||
{
|
||||
ExitNow(error = OT_ERROR_INVALID_ARGS);
|
||||
}
|
||||
|
||||
// Destination IPv6 address
|
||||
if (aArgsLength > 1)
|
||||
{
|
||||
error = aArgs[1].ParseAsIp6Address(coapDestinationIp);
|
||||
}
|
||||
else
|
||||
{
|
||||
ExitNow(error = OT_ERROR_INVALID_ARGS);
|
||||
strncpy(coapUri, aArgs[1].GetCString(), sizeof(coapUri) - 1);
|
||||
}
|
||||
|
||||
// Destination IPv6 address not need as client
|
||||
// if no IPv6 is entered, so ignore it and go away
|
||||
if (error == OT_ERROR_NONE)
|
||||
if (aArgsLength > 2)
|
||||
{
|
||||
indexShifter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
indexShifter = 1;
|
||||
}
|
||||
|
||||
// CoAP-URI
|
||||
if (aArgsLength > (2 - indexShifter))
|
||||
{
|
||||
strncpy(coapUri, aArgs[2 - indexShifter].GetCString(), sizeof(coapUri) - 1);
|
||||
}
|
||||
|
||||
// CoAP-Type
|
||||
if (aArgsLength > (3 - indexShifter))
|
||||
{
|
||||
if (aArgs[3 - indexShifter] == "con")
|
||||
if (aArgs[2] == "con")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
}
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
else if (aArgs[3 - indexShifter] == "block-16")
|
||||
else if (aArgs[2] == "block-16")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
coapBlock = true;
|
||||
coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_16;
|
||||
}
|
||||
else if (aArgs[3 - indexShifter] == "block-32")
|
||||
else if (aArgs[2] == "block-32")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
coapBlock = true;
|
||||
coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_32;
|
||||
}
|
||||
else if (aArgs[3 - indexShifter] == "block-64")
|
||||
else if (aArgs[2] == "block-64")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
coapBlock = true;
|
||||
coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_64;
|
||||
}
|
||||
else if (aArgs[3 - indexShifter] == "block-128")
|
||||
else if (aArgs[2] == "block-128")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
coapBlock = true;
|
||||
coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_128;
|
||||
}
|
||||
else if (aArgs[3 - indexShifter] == "block-256")
|
||||
else if (aArgs[2] == "block-256")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
coapBlock = true;
|
||||
coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_256;
|
||||
}
|
||||
else if (aArgs[3 - indexShifter] == "block-512")
|
||||
else if (aArgs[2] == "block-512")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
coapBlock = true;
|
||||
coapBlockSize = OT_COAP_OPTION_BLOCK_SZX_512;
|
||||
}
|
||||
else if (aArgs[3 - indexShifter] == "block-1024")
|
||||
else if (aArgs[2] == "block-1024")
|
||||
{
|
||||
coapType = OT_COAP_TYPE_CONFIRMABLE;
|
||||
coapBlock = true;
|
||||
@@ -351,14 +310,14 @@ otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
message = otCoapNewMessage(mInterpreter.mInstance, nullptr);
|
||||
VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS);
|
||||
|
||||
otCoapMessageInit(message, coapType, coapCode);
|
||||
otCoapMessageInit(message, coapType, aCoapCode);
|
||||
otCoapMessageGenerateToken(message, OT_COAP_DEFAULT_TOKEN_LENGTH);
|
||||
SuccessOrExit(error = otCoapMessageAppendUriPathOptions(message, coapUri));
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
if (coapBlock)
|
||||
{
|
||||
if (coapBlockType == ot::Coap::Message::kBlockType1)
|
||||
if (coapBlockType == kBlockType1)
|
||||
{
|
||||
SuccessOrExit(error = otCoapMessageAppendBlock1Option(message, 0, true, coapBlockSize));
|
||||
}
|
||||
@@ -369,17 +328,17 @@ otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if (aArgsLength > (4 - indexShifter))
|
||||
if (aArgsLength > 3)
|
||||
{
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
if (coapBlock)
|
||||
{
|
||||
SuccessOrExit(error = aArgs[4 - indexShifter].ParseAsUint32(mBlockCount));
|
||||
SuccessOrExit(error = aArgs[3].ParseAsUint32(mBlockCount));
|
||||
}
|
||||
else
|
||||
{
|
||||
#endif
|
||||
payloadLength = aArgs[4 - indexShifter].GetLength();
|
||||
payloadLength = aArgs[3].GetLength();
|
||||
|
||||
if (payloadLength > 0)
|
||||
{
|
||||
@@ -390,17 +349,12 @@ otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[])
|
||||
#endif
|
||||
}
|
||||
|
||||
// add payload
|
||||
if (payloadLength > 0)
|
||||
{
|
||||
SuccessOrExit(error = otMessageAppend(message, aArgs[4 - indexShifter].GetCString(), payloadLength));
|
||||
SuccessOrExit(error = otMessageAppend(message, aArgs[3].GetCString(), payloadLength));
|
||||
}
|
||||
|
||||
memset(&messageInfo, 0, sizeof(messageInfo));
|
||||
messageInfo.mPeerAddr = coapDestinationIp;
|
||||
messageInfo.mPeerPort = OT_DEFAULT_COAP_PORT;
|
||||
|
||||
if ((coapType == OT_COAP_TYPE_CONFIRMABLE) || (coapCode == OT_COAP_CODE_GET))
|
||||
if ((coapType == OT_COAP_TYPE_CONFIRMABLE) || (aCoapCode == OT_COAP_CODE_GET))
|
||||
{
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
if (coapBlock)
|
||||
|
||||
@@ -38,8 +38,10 @@
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
|
||||
|
||||
#include "coap/coap_message.hpp"
|
||||
#include "coap/coap_secure.hpp"
|
||||
#include <mbedtls/ssl.h>
|
||||
|
||||
#include <openthread/coap_secure.h>
|
||||
|
||||
#include "utils/lookup_table.hpp"
|
||||
#include "utils/parse_cmdline.hpp"
|
||||
|
||||
@@ -93,19 +95,32 @@ private:
|
||||
otError (CoapSecure::*mHandler)(uint8_t aArgsLength, Arg aArgs[]);
|
||||
};
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
enum BlockType : uint8_t
|
||||
{
|
||||
kBlockType1,
|
||||
kBlockType2,
|
||||
};
|
||||
#endif
|
||||
|
||||
void PrintPayload(otMessage *aMessage) const;
|
||||
|
||||
otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessConnect(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessDelete(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessDisconnect(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessGet(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessPost(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessPsk(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessPut(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessResource(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessSet(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessStart(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessStop(uint8_t aArgsLength, Arg aArgs[]);
|
||||
otError ProcessX509(uint8_t aArgsLength, Arg aArgs[]);
|
||||
|
||||
otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode);
|
||||
|
||||
void Stop(void);
|
||||
|
||||
static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
|
||||
@@ -145,15 +160,15 @@ private:
|
||||
|
||||
static constexpr Command sCommands[] = {
|
||||
{"connect", &CoapSecure::ProcessConnect},
|
||||
{"delete", &CoapSecure::ProcessRequest},
|
||||
{"delete", &CoapSecure::ProcessDelete},
|
||||
{"disconnect", &CoapSecure::ProcessDisconnect},
|
||||
{"get", &CoapSecure::ProcessRequest},
|
||||
{"get", &CoapSecure::ProcessGet},
|
||||
{"help", &CoapSecure::ProcessHelp},
|
||||
{"post", &CoapSecure::ProcessRequest},
|
||||
{"post", &CoapSecure::ProcessPost},
|
||||
#ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
|
||||
{"psk", &CoapSecure::ProcessPsk},
|
||||
#endif
|
||||
{"put", &CoapSecure::ProcessRequest},
|
||||
{"put", &CoapSecure::ProcessPut},
|
||||
{"resource", &CoapSecure::ProcessResource},
|
||||
{"set", &CoapSecure::ProcessSet},
|
||||
{"start", &CoapSecure::ProcessStart},
|
||||
|
||||
@@ -52,21 +52,21 @@ expect_line "Done"
|
||||
send "coaps connect $addr_1 5684\n"
|
||||
expect_line "Done"
|
||||
expect "coaps connected"
|
||||
send "coaps get $addr_1 test/resource\n"
|
||||
send "coaps get test/resource\n"
|
||||
expect_line "Done"
|
||||
expect "coaps response from $addr_1 with payload: 54657374696e67313233" # ASCII of "Testing123"
|
||||
send "coaps post $addr_1 test/resource con Testing123\n"
|
||||
send "coaps post test/resource con Testing123\n"
|
||||
expect_line "Done"
|
||||
expect "coaps response from $addr_1"
|
||||
send "coaps put $addr_1 test/resource con Testing123\n"
|
||||
send "coaps put test/resource con Testing123\n"
|
||||
expect_line "Done"
|
||||
expect "coaps response from $addr_1"
|
||||
send "coaps delete $addr_1 test/resource con\n"
|
||||
send "coaps delete test/resource con\n"
|
||||
expect_line "Done"
|
||||
expect "coaps response from $addr_1"
|
||||
send "coaps post $addr_1 test/resource none Testing123\n"
|
||||
send "coaps post test/resource none Testing123\n"
|
||||
expect_line "Done"
|
||||
send "coaps get $addr_1 default\n"
|
||||
send "coaps get default\n"
|
||||
expect_line "Done"
|
||||
expect "coaps response from $addr_1"
|
||||
set addr_2 [get_ipaddr mleid]
|
||||
|
||||
Reference in New Issue
Block a user