mirror of
https://github.com/espressif/openthread.git
synced 2026-08-20 01:19:51 +00:00
[coap] enhance block-wise transfer BlockSzx handling (#12237)
This change improves the internal implementation of CoAP block-wise transfers. Introduces an internal `Coap::BlockSzx` enum to mirror the public `otCoapBlockSzx` enum, improving the separation between the API and the implementation. All internal functions are updated to use the new `BlockSzx` enum. The logic from `otCoapBlockSizeFromExponent()` is moved into a new core `Coap::BlockSizeFromExponent()` function. The public function becomes a simple wrapper. A new helper function, `CoapBase::DetermineBlockSzxFromSize()`, is added to replace a `switch` statement, simplifying the logic for determining block size from a given buffer length. `ResourceBlockWise` is updated to inherit from `LinkedListEntry`, aligning it with the common pattern used for managing resource lists.
This commit is contained in:
@@ -100,19 +100,16 @@ otError otCoapMessageAppendUriQueryOptions(otMessage *aMessage, const char *aUri
|
||||
return AsCoapMessage(aMessage).AppendUriQueryOptions(aUriQuery);
|
||||
}
|
||||
|
||||
uint16_t otCoapBlockSizeFromExponent(otCoapBlockSzx aSize)
|
||||
{
|
||||
return static_cast<uint16_t>(1 << (static_cast<uint8_t>(aSize) + Coap::Message::kBlockSzxBase));
|
||||
}
|
||||
uint16_t otCoapBlockSizeFromExponent(otCoapBlockSzx aSize) { return Coap::BlockSizeFromExponent(MapEnum(aSize)); }
|
||||
|
||||
otError otCoapMessageAppendBlock2Option(otMessage *aMessage, uint32_t aNum, bool aMore, otCoapBlockSzx aSize)
|
||||
{
|
||||
return AsCoapMessage(aMessage).AppendBlockOption(Coap::Message::kBlockType2, aNum, aMore, aSize);
|
||||
return AsCoapMessage(aMessage).AppendBlockOption(Coap::Message::kBlockType2, aNum, aMore, MapEnum(aSize));
|
||||
}
|
||||
|
||||
otError otCoapMessageAppendBlock1Option(otMessage *aMessage, uint32_t aNum, bool aMore, otCoapBlockSzx aSize)
|
||||
{
|
||||
return AsCoapMessage(aMessage).AppendBlockOption(Coap::Message::kBlockType1, aNum, aMore, aSize);
|
||||
return AsCoapMessage(aMessage).AppendBlockOption(Coap::Message::kBlockType1, aNum, aMore, MapEnum(aSize));
|
||||
}
|
||||
|
||||
otError otCoapMessageAppendProxyUriOption(otMessage *aMessage, const char *aUriPath)
|
||||
|
||||
+61
-69
@@ -178,13 +178,13 @@ Error CoapBase::Send(ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
Error CoapBase::SendMessage(Message &aMessage,
|
||||
const Ip6::MessageInfo &aMessageInfo,
|
||||
const TxParameters *aTxParameters,
|
||||
ResponseHandler aHandler,
|
||||
void *aContext,
|
||||
otCoapBlockwiseTransmitHook aTransmitHook,
|
||||
otCoapBlockwiseReceiveHook aReceiveHook)
|
||||
Error CoapBase::SendMessage(Message &aMessage,
|
||||
const Ip6::MessageInfo &aMessageInfo,
|
||||
const TxParameters *aTxParameters,
|
||||
ResponseHandler aHandler,
|
||||
void *aContext,
|
||||
BlockwiseTransmitHook aTransmitHook,
|
||||
BlockwiseReceiveHook aReceiveHook)
|
||||
#else
|
||||
Error CoapBase::SendMessage(Message &aMessage,
|
||||
const Ip6::MessageInfo &aMessageInfo,
|
||||
@@ -220,7 +220,7 @@ Error CoapBase::SendMessage(Message &aMessage,
|
||||
(aMessage.GetBlockWiseBlockNumber() == 0))
|
||||
{
|
||||
// Set payload for first block of the transfer
|
||||
VerifyOrExit((bufLen = otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
VerifyOrExit((bufLen = BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
error = kErrorNoBufs);
|
||||
SuccessOrExit(error = aTransmitHook(aContext, buf, aMessage.GetBlockWiseBlockNumber() * bufLen, &bufLen,
|
||||
&moreBlocks));
|
||||
@@ -242,7 +242,7 @@ Error CoapBase::SendMessage(Message &aMessage,
|
||||
(aMessage.GetBlockWiseBlockNumber() == 0))
|
||||
{
|
||||
// Set payload for first block of the transfer
|
||||
VerifyOrExit((bufLen = otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
VerifyOrExit((bufLen = BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
error = kErrorNoBufs);
|
||||
SuccessOrExit(error = aTransmitHook(aContext, buf, aMessage.GetBlockWiseBlockNumber() * bufLen, &bufLen,
|
||||
&moreBlocks));
|
||||
@@ -1250,17 +1250,16 @@ Error CoapBase::SendNextBlock1Request(Message &aRequest,
|
||||
}
|
||||
|
||||
// Get next block
|
||||
VerifyOrExit((bufLen = otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
VerifyOrExit((bufLen = BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
error = kErrorNoBufs);
|
||||
|
||||
SuccessOrExit(
|
||||
error = aCoapMetadata.mBlockwiseTransmitHook(aCoapMetadata.mResponseContext, buf,
|
||||
otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) *
|
||||
(aMessage.GetBlockWiseBlockNumber() + 1),
|
||||
&bufLen, &moreBlocks));
|
||||
SuccessOrExit(error = aCoapMetadata.mBlockwiseTransmitHook(aCoapMetadata.mResponseContext, buf,
|
||||
BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) *
|
||||
(aMessage.GetBlockWiseBlockNumber() + 1),
|
||||
&bufLen, &moreBlocks));
|
||||
|
||||
// Check if block length is valid
|
||||
VerifyOrExit(bufLen <= otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()), error = kErrorInvalidArgs);
|
||||
VerifyOrExit(bufLen <= BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()), error = kErrorInvalidArgs);
|
||||
|
||||
// Init request for next block
|
||||
VerifyOrExit((request = NewMessage()) != nullptr, error = kErrorNoBufs);
|
||||
@@ -1273,7 +1272,7 @@ Error CoapBase::SendNextBlock1Request(Message &aRequest,
|
||||
DequeueMessage(aRequest);
|
||||
|
||||
LogInfo("Send Block1 Nr. %d, Size: %d bytes, More Blocks Flag: %d", request->GetBlockWiseBlockNumber(),
|
||||
otCoapBlockSizeFromExponent(request->GetBlockWiseBlockSize()), request->IsMoreBlocksFlagSet());
|
||||
BlockSizeFromExponent(request->GetBlockWiseBlockSize()), request->IsMoreBlocksFlagSet());
|
||||
|
||||
SuccessOrExit(error = SendMessage(*request, aMessageInfo, /* aTxParamters */ nullptr,
|
||||
aCoapMetadata.mResponseHandler, aCoapMetadata.mResponseContext,
|
||||
@@ -1301,21 +1300,20 @@ Error CoapBase::SendNextBlock2Request(Message &aRequest,
|
||||
|
||||
// Check payload and block length
|
||||
VerifyOrExit((aMessage.GetLength() - aMessage.GetOffset()) <=
|
||||
otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) &&
|
||||
BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) &&
|
||||
(aMessage.GetLength() - aMessage.GetOffset()) <= kMaxBlockLength,
|
||||
error = kErrorNoBufs);
|
||||
|
||||
// Read and then forward payload to receive hook function
|
||||
bufLen = aMessage.ReadBytes(aMessage.GetOffset(), buf, aMessage.GetLength() - aMessage.GetOffset());
|
||||
SuccessOrExit(
|
||||
error = aCoapMetadata.mBlockwiseReceiveHook(aCoapMetadata.mResponseContext, buf,
|
||||
otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) *
|
||||
aMessage.GetBlockWiseBlockNumber(),
|
||||
bufLen, aMessage.IsMoreBlocksFlagSet(), aTotalLength));
|
||||
SuccessOrExit(error = aCoapMetadata.mBlockwiseReceiveHook(aCoapMetadata.mResponseContext, buf,
|
||||
BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) *
|
||||
aMessage.GetBlockWiseBlockNumber(),
|
||||
bufLen, aMessage.IsMoreBlocksFlagSet(), aTotalLength));
|
||||
|
||||
// CoAP Block-Wise Transfer continues
|
||||
LogInfo("Received Block2 Nr. %d , Size: %d bytes, More Blocks Flag: %d", aMessage.GetBlockWiseBlockNumber(),
|
||||
otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()), aMessage.IsMoreBlocksFlagSet());
|
||||
BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()), aMessage.IsMoreBlocksFlagSet());
|
||||
|
||||
// Conclude block-wise transfer if last block has been received
|
||||
if (!aMessage.IsMoreBlocksFlagSet())
|
||||
@@ -1335,7 +1333,7 @@ Error CoapBase::SendNextBlock2Request(Message &aRequest,
|
||||
}
|
||||
|
||||
LogInfo("Request Block2 Nr. %d, Size: %d bytes", request->GetBlockWiseBlockNumber(),
|
||||
otCoapBlockSizeFromExponent(request->GetBlockWiseBlockSize()));
|
||||
BlockSizeFromExponent(request->GetBlockWiseBlockSize()));
|
||||
|
||||
SuccessOrExit(error =
|
||||
SendMessage(*request, aMessageInfo, /* aTxParameters */ nullptr, aCoapMetadata.mResponseHandler,
|
||||
@@ -1362,10 +1360,9 @@ Error CoapBase::ProcessBlock1Request(Message &aMessage,
|
||||
// Read and then forward payload to receive hook function
|
||||
VerifyOrExit((aMessage.GetLength() - aMessage.GetOffset()) <= kMaxBlockLength, error = kErrorNoBufs);
|
||||
bufLen = aMessage.ReadBytes(aMessage.GetOffset(), buf, aMessage.GetLength() - aMessage.GetOffset());
|
||||
SuccessOrExit(error = aResource.HandleBlockReceive(buf,
|
||||
otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) *
|
||||
aMessage.GetBlockWiseBlockNumber(),
|
||||
bufLen, aMessage.IsMoreBlocksFlagSet(), aTotalLength));
|
||||
SuccessOrExit(error = aResource.HandleBlockReceive(
|
||||
buf, BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) * aMessage.GetBlockWiseBlockNumber(),
|
||||
bufLen, aMessage.IsMoreBlocksFlagSet(), aTotalLength));
|
||||
|
||||
if (aMessage.IsMoreBlocksFlagSet())
|
||||
{
|
||||
@@ -1386,7 +1383,7 @@ Error CoapBase::ProcessBlock1Request(Message &aMessage,
|
||||
SuccessOrExit(error = CacheLastBlockResponse(response));
|
||||
|
||||
LogInfo("Acknowledge Block1 Nr. %d, Size: %d bytes", response->GetBlockWiseBlockNumber(),
|
||||
otCoapBlockSizeFromExponent(response->GetBlockWiseBlockSize()));
|
||||
BlockSizeFromExponent(response->GetBlockWiseBlockSize()));
|
||||
|
||||
SuccessOrExit(error = SendMessage(*response, aMessageInfo));
|
||||
|
||||
@@ -1423,7 +1420,7 @@ Error CoapBase::ProcessBlock2Request(Message &aMessage,
|
||||
SuccessOrExit(error = aMessage.ReadBlockOptionValues(kOptionBlock2));
|
||||
|
||||
LogInfo("Request for Block2 Nr. %d, Size: %d bytes received", aMessage.GetBlockWiseBlockNumber(),
|
||||
otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()));
|
||||
BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()));
|
||||
|
||||
if (aMessage.GetBlockWiseBlockNumber() == 0)
|
||||
{
|
||||
@@ -1438,56 +1435,31 @@ Error CoapBase::ProcessBlock2Request(Message &aMessage,
|
||||
|
||||
SuccessOrExit(error = response->SetTokenFromMessage(aMessage));
|
||||
|
||||
VerifyOrExit((bufLen = otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
VerifyOrExit((bufLen = BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize())) <= kMaxBlockLength,
|
||||
error = kErrorNoBufs);
|
||||
SuccessOrExit(error = aResource.HandleBlockTransmit(buf,
|
||||
otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) *
|
||||
aMessage.GetBlockWiseBlockNumber(),
|
||||
&bufLen, &moreBlocks));
|
||||
SuccessOrExit(error = aResource.HandleBlockTransmit(
|
||||
buf, BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) * aMessage.GetBlockWiseBlockNumber(),
|
||||
&bufLen, &moreBlocks));
|
||||
|
||||
response->SetMoreBlocksFlag(moreBlocks);
|
||||
|
||||
if (moreBlocks)
|
||||
{
|
||||
switch (bufLen)
|
||||
{
|
||||
case 1024:
|
||||
response->SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_1024);
|
||||
break;
|
||||
case 512:
|
||||
response->SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_512);
|
||||
break;
|
||||
case 256:
|
||||
response->SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_256);
|
||||
break;
|
||||
case 128:
|
||||
response->SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_128);
|
||||
break;
|
||||
case 64:
|
||||
response->SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_64);
|
||||
break;
|
||||
case 32:
|
||||
response->SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_32);
|
||||
break;
|
||||
case 16:
|
||||
response->SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_16);
|
||||
break;
|
||||
default:
|
||||
error = kErrorInvalidArgs;
|
||||
ExitNow();
|
||||
break;
|
||||
}
|
||||
BlockSzx blockSzx;
|
||||
|
||||
SuccessOrExit(error = DetermineBlockSzxFromSize(bufLen, blockSzx));
|
||||
response->SetBlockWiseBlockSize(blockSzx);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Verify that buffer length is not larger than requested block size
|
||||
VerifyOrExit(bufLen <= otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()),
|
||||
error = kErrorInvalidArgs);
|
||||
VerifyOrExit(bufLen <= BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()), error = kErrorInvalidArgs);
|
||||
response->SetBlockWiseBlockSize(aMessage.GetBlockWiseBlockSize());
|
||||
}
|
||||
|
||||
response->SetBlockWiseBlockNumber(
|
||||
(otCoapBlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) * aMessage.GetBlockWiseBlockNumber()) /
|
||||
(otCoapBlockSizeFromExponent(response->GetBlockWiseBlockSize())));
|
||||
(BlockSizeFromExponent(aMessage.GetBlockWiseBlockSize()) * aMessage.GetBlockWiseBlockNumber()) /
|
||||
(BlockSizeFromExponent(response->GetBlockWiseBlockSize())));
|
||||
|
||||
// Copy options from last response
|
||||
SuccessOrExit(error = iterator.Init(*mLastResponse));
|
||||
@@ -1525,7 +1497,7 @@ Error CoapBase::ProcessBlock2Request(Message &aMessage,
|
||||
}
|
||||
|
||||
LogInfo("Send Block2 Nr. %d, Size: %d bytes, More Blocks Flag %d", response->GetBlockWiseBlockNumber(),
|
||||
otCoapBlockSizeFromExponent(response->GetBlockWiseBlockSize()), response->IsMoreBlocksFlagSet());
|
||||
BlockSizeFromExponent(response->GetBlockWiseBlockSize()), response->IsMoreBlocksFlagSet());
|
||||
|
||||
SuccessOrExit(error = SendMessage(*response, aMessageInfo));
|
||||
|
||||
@@ -1535,6 +1507,26 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
Error CoapBase::DetermineBlockSzxFromSize(uint16_t aSize, BlockSzx &aBlockSzx)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
|
||||
for (uint8_t szx = kBlockSzx16; szx <= kBlockSzx1024; szx++)
|
||||
{
|
||||
aBlockSzx = static_cast<BlockSzx>(szx);
|
||||
|
||||
if (BlockSizeFromExponent(aBlockSzx) == aSize)
|
||||
{
|
||||
ExitNow();
|
||||
}
|
||||
}
|
||||
|
||||
error = kErrorInvalidArgs;
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
+43
-50
@@ -160,10 +160,25 @@ protected:
|
||||
};
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
|
||||
/**
|
||||
* Represents a function pointer which is called when a CoAP message with a block-wise transfer option is received.
|
||||
*
|
||||
* Please see `otCoapBlockwiseReceiveHook` for details.
|
||||
*/
|
||||
typedef otCoapBlockwiseReceiveHook BlockwiseReceiveHook;
|
||||
|
||||
/**
|
||||
* Represents a function pointer which is called before the next block in a block-wise transfer is sent.
|
||||
*
|
||||
* Please see `otCoapBlockwiseTransmitHook` for details.
|
||||
*/
|
||||
typedef otCoapBlockwiseTransmitHook BlockwiseTransmitHook;
|
||||
|
||||
/**
|
||||
* Implements CoAP block-wise resource handling.
|
||||
*/
|
||||
class ResourceBlockWise : public otCoapBlockwiseResource
|
||||
class ResourceBlockWise : public otCoapBlockwiseResource, public LinkedListEntry<ResourceBlockWise>
|
||||
{
|
||||
friend class CoapBase;
|
||||
|
||||
@@ -179,11 +194,11 @@ public:
|
||||
* @param[in] aTransmitHook A function pointer that is called when transmitting a CoAP block message from @p
|
||||
* aUriPath.
|
||||
*/
|
||||
ResourceBlockWise(const char *aUriPath,
|
||||
otCoapRequestHandler aHandler,
|
||||
void *aContext,
|
||||
otCoapBlockwiseReceiveHook aReceiveHook,
|
||||
otCoapBlockwiseTransmitHook aTransmitHook)
|
||||
ResourceBlockWise(const char *aUriPath,
|
||||
RequestHandler aHandler,
|
||||
void *aContext,
|
||||
BlockwiseReceiveHook aReceiveHook,
|
||||
BlockwiseTransmitHook aTransmitHook)
|
||||
{
|
||||
mUriPath = aUriPath;
|
||||
mHandler = aHandler;
|
||||
@@ -193,47 +208,6 @@ public:
|
||||
mNext = nullptr;
|
||||
}
|
||||
|
||||
Error HandleBlockReceive(const uint8_t *aBlock,
|
||||
uint32_t aPosition,
|
||||
uint16_t aBlockLength,
|
||||
bool aMore,
|
||||
uint32_t aTotalLength) const
|
||||
{
|
||||
return mReceiveHook(otCoapBlockwiseResource::mContext, aBlock, aPosition, aBlockLength, aMore, aTotalLength);
|
||||
}
|
||||
|
||||
Error HandleBlockTransmit(uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore) const
|
||||
{
|
||||
return mTransmitHook(otCoapBlockwiseResource::mContext, aBlock, aPosition, aBlockLength, aMore);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next entry in the linked list.
|
||||
*
|
||||
* @returns A pointer to the next entry in the linked list or `nullptr` if at the end of the list.
|
||||
*/
|
||||
const ResourceBlockWise *GetNext(void) const
|
||||
{
|
||||
return static_cast<const ResourceBlockWise *>(static_cast<const ResourceBlockWise *>(this)->mNext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next entry in the linked list.
|
||||
*
|
||||
* @returns A pointer to the next entry in the linked list or `nullptr` if at the end of the list.
|
||||
*/
|
||||
ResourceBlockWise *GetNext(void)
|
||||
{
|
||||
return static_cast<ResourceBlockWise *>(static_cast<ResourceBlockWise *>(this)->mNext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the next pointer on the entry.
|
||||
*
|
||||
* @param[in] aNext A pointer to the next entry.
|
||||
*/
|
||||
void SetNext(ResourceBlockWise *aNext) { static_cast<ResourceBlockWise *>(this)->mNext = aNext; }
|
||||
|
||||
/**
|
||||
* Returns a pointer to the URI path.
|
||||
*
|
||||
@@ -246,7 +220,22 @@ protected:
|
||||
{
|
||||
mHandler(mContext, &aMessage, &aMessageInfo);
|
||||
}
|
||||
|
||||
Error HandleBlockReceive(const uint8_t *aBlock,
|
||||
uint32_t aPosition,
|
||||
uint16_t aBlockLength,
|
||||
bool aMore,
|
||||
uint32_t aTotalLength) const
|
||||
{
|
||||
return mReceiveHook(mContext, aBlock, aPosition, aBlockLength, aMore, aTotalLength);
|
||||
}
|
||||
|
||||
Error HandleBlockTransmit(uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore) const
|
||||
{
|
||||
return mTransmitHook(mContext, aBlock, aPosition, aBlockLength, aMore);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
|
||||
/**
|
||||
@@ -770,8 +759,8 @@ private:
|
||||
bool mObserve : 1; // Information that this request involves Observations.
|
||||
#endif
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
otCoapBlockwiseReceiveHook mBlockwiseReceiveHook; // Function pointer called on Block2 response reception.
|
||||
otCoapBlockwiseTransmitHook mBlockwiseTransmitHook; // Function pointer called on Block1 response reception.
|
||||
BlockwiseReceiveHook mBlockwiseReceiveHook; // Function pointer called on Block2 response reception.
|
||||
BlockwiseTransmitHook mBlockwiseTransmitHook; // Function pointer called on Block1 response reception.
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -827,6 +816,7 @@ private:
|
||||
Error Send(ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
|
||||
void FreeLastBlockResponse(void);
|
||||
Error CacheLastBlockResponse(Message *aResponse);
|
||||
Error PrepareNextBlockRequest(Message::BlockType aType,
|
||||
@@ -851,7 +841,10 @@ private:
|
||||
const Metadata &aCoapMetadata,
|
||||
uint32_t aTotalLength,
|
||||
bool aBeginBlock1Transfer);
|
||||
#endif
|
||||
|
||||
static Error DetermineBlockSzxFromSize(uint16_t aSize, BlockSzx &aBlockSzx);
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
|
||||
MessageQueue mPendingRequests;
|
||||
uint16_t mMessageId;
|
||||
|
||||
@@ -38,6 +38,16 @@
|
||||
namespace ot {
|
||||
namespace Coap {
|
||||
|
||||
uint16_t BlockSizeFromExponent(BlockSzx aBlockSzxq)
|
||||
{
|
||||
static constexpr uint8_t kBlockSzxBase = 4;
|
||||
|
||||
return static_cast<uint16_t>(1 << (static_cast<uint8_t>(aBlockSzxq) + kBlockSzxBase));
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
// `Message`
|
||||
|
||||
void Message::Init(void)
|
||||
{
|
||||
GetHelpData().Clear();
|
||||
@@ -49,7 +59,7 @@ void Message::Init(void)
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
SetBlockWiseBlockNumber(0);
|
||||
SetMoreBlocksFlag(false);
|
||||
SetBlockWiseBlockSize(OT_COAP_OPTION_BLOCK_SZX_16);
|
||||
SetBlockWiseBlockSize(kBlockSzx16);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -285,13 +295,13 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
Error Message::AppendBlockOption(Message::BlockType aType, uint32_t aNum, bool aMore, otCoapBlockSzx aSize)
|
||||
Error Message::AppendBlockOption(Message::BlockType aType, uint32_t aNum, bool aMore, BlockSzx aSize)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
uint32_t encoded = aSize;
|
||||
|
||||
VerifyOrExit(aType == kBlockType1 || aType == kBlockType2, error = kErrorInvalidArgs);
|
||||
VerifyOrExit(aSize <= OT_COAP_OPTION_BLOCK_SZX_1024, error = kErrorInvalidArgs);
|
||||
VerifyOrExit(aSize <= kBlockSzx1024, error = kErrorInvalidArgs);
|
||||
VerifyOrExit(aNum < kBlockNumMax, error = kErrorInvalidArgs);
|
||||
|
||||
encoded |= static_cast<uint32_t>(aMore << kBlockMOffset);
|
||||
@@ -324,17 +334,17 @@ Error Message::ReadBlockOptionValues(uint16_t aBlockType)
|
||||
case 1:
|
||||
SetBlockWiseBlockNumber(static_cast<uint32_t>((buf[0] & 0xf0) >> 4));
|
||||
SetMoreBlocksFlag(static_cast<bool>((buf[0] & 0x08) >> 3 == 1));
|
||||
SetBlockWiseBlockSize(static_cast<otCoapBlockSzx>(buf[0] & 0x07));
|
||||
SetBlockWiseBlockSize(static_cast<BlockSzx>(buf[0] & 0x07));
|
||||
break;
|
||||
case 2:
|
||||
SetBlockWiseBlockNumber(static_cast<uint32_t>((buf[0] << 4) + ((buf[1] & 0xf0) >> 4)));
|
||||
SetMoreBlocksFlag(static_cast<bool>((buf[1] & 0x08) >> 3 == 1));
|
||||
SetBlockWiseBlockSize(static_cast<otCoapBlockSzx>(buf[1] & 0x07));
|
||||
SetBlockWiseBlockSize(static_cast<BlockSzx>(buf[1] & 0x07));
|
||||
break;
|
||||
case 3:
|
||||
SetBlockWiseBlockNumber(static_cast<uint32_t>((buf[0] << 12) + (buf[1] << 4) + ((buf[2] & 0xf0) >> 4)));
|
||||
SetMoreBlocksFlag(static_cast<bool>((buf[2] & 0x08) >> 3 == 1));
|
||||
SetBlockWiseBlockSize(static_cast<otCoapBlockSzx>(buf[2] & 0x07));
|
||||
SetBlockWiseBlockSize(static_cast<BlockSzx>(buf[2] & 0x07));
|
||||
break;
|
||||
default:
|
||||
error = kErrorInvalidArgs;
|
||||
@@ -491,10 +501,16 @@ const char *Message::CodeToString(void) const
|
||||
}
|
||||
#endif // OPENTHREAD_CONFIG_COAP_API_ENABLE
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
// `Message::Iterator`
|
||||
|
||||
Message::Iterator MessageQueue::begin(void) { return Message::Iterator(GetHead()); }
|
||||
|
||||
Message::ConstIterator MessageQueue::begin(void) const { return Message::ConstIterator(GetHead()); }
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
// `Option::Iterator`
|
||||
|
||||
Error Option::Iterator::Init(const Message &aMessage)
|
||||
{
|
||||
Error error = kErrorParse;
|
||||
|
||||
@@ -153,6 +153,29 @@ enum OptionNumber : uint16_t
|
||||
kOptionSize1 = OT_COAP_OPTION_SIZE1, ///< Size1
|
||||
};
|
||||
|
||||
/**
|
||||
* CoAP Block Size Exponents
|
||||
*/
|
||||
enum BlockSzx : uint8_t
|
||||
{
|
||||
kBlockSzx16 = OT_COAP_OPTION_BLOCK_SZX_16, ///< 16 bytes.
|
||||
kBlockSzx32 = OT_COAP_OPTION_BLOCK_SZX_32, ///< 32 bytes.
|
||||
kBlockSzx64 = OT_COAP_OPTION_BLOCK_SZX_64, ///< 64 bytes.
|
||||
kBlockSzx128 = OT_COAP_OPTION_BLOCK_SZX_128, ///< 128 bytes.
|
||||
kBlockSzx256 = OT_COAP_OPTION_BLOCK_SZX_256, ///< 256 bytes.
|
||||
kBlockSzx512 = OT_COAP_OPTION_BLOCK_SZX_512, ///< 512 bytes.
|
||||
kBlockSzx1024 = OT_COAP_OPTION_BLOCK_SZX_1024, ///< 1024 bytes.
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a CoAP Block Size Exponent (SZX) to the actual block size (in bytes).
|
||||
*
|
||||
* @param[in] aBlockSzx Block size exponent.
|
||||
*
|
||||
* @returns The actual size corresponding to @o aBlockSzx.
|
||||
*/
|
||||
uint16_t BlockSizeFromExponent(BlockSzx aBlockSzx);
|
||||
|
||||
/**
|
||||
* Implements CoAP message generation and parsing.
|
||||
*/
|
||||
@@ -178,8 +201,6 @@ public:
|
||||
kBlockType2 = 2,
|
||||
};
|
||||
|
||||
static constexpr uint8_t kBlockSzxBase = 4;
|
||||
|
||||
/**
|
||||
* Initializes the CoAP header.
|
||||
*/
|
||||
@@ -469,7 +490,7 @@ public:
|
||||
* @retval kErrorInvalidArgs The option type is not equal or greater than the last option type.
|
||||
* @retval kErrorNoBufs The option length exceeds the buffer size.
|
||||
*/
|
||||
Error AppendBlockOption(BlockType aType, uint32_t aNum, bool aMore, otCoapBlockSzx aSize);
|
||||
Error AppendBlockOption(BlockType aType, uint32_t aNum, bool aMore, BlockSzx aSize);
|
||||
|
||||
/**
|
||||
* Appends a Proxy-Uri option.
|
||||
@@ -558,7 +579,7 @@ public:
|
||||
*
|
||||
* @returns The block size.
|
||||
*/
|
||||
otCoapBlockSzx GetBlockWiseBlockSize(void) const { return GetHelpData().mBlockWiseData.mBlockSize; }
|
||||
BlockSzx GetBlockWiseBlockSize(void) const { return GetHelpData().mBlockWiseData.mBlockSize; }
|
||||
#endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
|
||||
/**
|
||||
@@ -625,7 +646,7 @@ public:
|
||||
*
|
||||
* @param[in] aBlockSize Block size value to set.
|
||||
*/
|
||||
void SetBlockWiseBlockSize(otCoapBlockSzx aBlockSize) { GetHelpData().mBlockWiseData.mBlockSize = aBlockSize; }
|
||||
void SetBlockWiseBlockSize(BlockSzx aBlockSize) { GetHelpData().mBlockWiseData.mBlockSize = aBlockSize; }
|
||||
#endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
|
||||
/**
|
||||
@@ -859,9 +880,9 @@ private:
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
struct BlockWiseData
|
||||
{
|
||||
uint32_t mBlockNumber;
|
||||
bool mMoreBlocks;
|
||||
otCoapBlockSzx mBlockSize;
|
||||
uint32_t mBlockNumber;
|
||||
bool mMoreBlocks;
|
||||
BlockSzx mBlockSize;
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -1187,6 +1208,7 @@ DefineCoreType(otCoapOption, Coap::Option);
|
||||
DefineCoreType(otCoapOptionIterator, Coap::Option::Iterator);
|
||||
DefineMapEnum(otCoapType, Coap::Type);
|
||||
DefineMapEnum(otCoapCode, Coap::Code);
|
||||
DefineMapEnum(otCoapBlockSzx, Coap::BlockSzx);
|
||||
|
||||
/**
|
||||
* Casts an `otMessage` pointer to a `Coap::Message` reference.
|
||||
|
||||
Reference in New Issue
Block a user