[coap] avoid using default parameter values for common methods (#8932)

This commit adds overloads of commonly used methods in `Coap` and
`MessagePool` replacing default parameter values. This helps reduce
code size.
This commit is contained in:
Abtin Keshavarzian
2023-04-06 09:52:33 -07:00
committed by GitHub
parent 356b4a6a58
commit 6865b83d7f
4 changed files with 110 additions and 18 deletions
+22
View File
@@ -115,6 +115,13 @@ exit:
return message;
}
Message *CoapBase::NewMessage(void) { return NewMessage(Message::Settings::GetDefault()); }
Message *CoapBase::NewPriorityMessage(void)
{
return NewMessage(Message::Settings(Message::kWithLinkSecurity, Message::kPriorityNet));
}
Message *CoapBase::NewPriorityConfirmablePostMessage(Uri aUri)
{
return InitMessage(NewPriorityMessage(), kTypeConfirmable, aUri);
@@ -351,6 +358,11 @@ exit:
return error;
}
Error CoapBase::SendMessage(Message &aMessage, const Ip6::MessageInfo &aMessageInfo, const TxParameters &aTxParameters)
{
return SendMessage(aMessage, aMessageInfo, aTxParameters, nullptr, nullptr);
}
Error CoapBase::SendMessage(Message &aMessage,
const Ip6::MessageInfo &aMessageInfo,
ResponseHandler aHandler,
@@ -363,6 +375,11 @@ Error CoapBase::SendMessage(Message &aMessage,
#endif
}
Error CoapBase::SendMessage(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
return SendMessage(aMessage, aMessageInfo, nullptr, nullptr);
}
Error CoapBase::SendReset(Message &aRequest, const Ip6::MessageInfo &aMessageInfo)
{
return SendEmptyMessage(kTypeReset, aRequest, aMessageInfo);
@@ -378,6 +395,11 @@ Error CoapBase::SendEmptyAck(const Message &aRequest, const Ip6::MessageInfo &aM
return (aRequest.IsConfirmable() ? SendHeaderResponse(aCode, aRequest, aMessageInfo) : kErrorInvalidArgs);
}
Error CoapBase::SendEmptyAck(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo)
{
return SendEmptyAck(aRequest, aMessageInfo, kCodeChanged);
}
Error CoapBase::SendNotFound(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo)
{
return SendHeaderResponse(kCodeNotFound, aRequest, aMessageInfo);
+59 -15
View File
@@ -449,7 +449,15 @@ public:
* @returns A pointer to the message or `nullptr` if failed to allocate message.
*
*/
Message *NewMessage(const Message::Settings &aSettings = Message::Settings::GetDefault());
Message *NewMessage(const Message::Settings &aSettings);
/**
* This method allocates a new message with a CoAP header with default settings.
*
* @returns A pointer to the message or `nullptr` if failed to allocate message.
*
*/
Message *NewMessage(void);
/**
* This method allocates a new message with a CoAP header that has Network Control priority level.
@@ -457,10 +465,7 @@ public:
* @returns A pointer to the message or `nullptr` if failed to allocate message.
*
*/
Message *NewPriorityMessage(void)
{
return NewMessage(Message::Settings(Message::kWithLinkSecurity, Message::kPriorityNet));
}
Message *NewPriorityMessage(void);
/**
* This method allocates and initializes a new CoAP Confirmable Post message with Network Control priority level.
@@ -557,7 +562,7 @@ public:
*
* If a response for a request is expected, respective function and context information should be provided.
* If no response is expected, these arguments should be NULL pointers.
* If Message Id was not set in the header (equal to 0), this function will assign unique Message Id to the message.
* If Message ID was not set in the header (equal to 0), this method will assign unique Message ID to the message.
*
* @param[in] aMessage A reference to the message to send.
* @param[in] aMessageInfo A reference to the message info associated with @p aMessage.
@@ -585,7 +590,7 @@ public:
*
* If a response for a request is expected, respective function and context information should be provided.
* If no response is expected, these arguments should be `nullptr` pointers.
* If Message Id was not set in the header (equal to 0), this function will assign unique Message Id to the message.
* If Message ID was not set in the header (equal to 0), this method will assign unique Message ID to the message.
*
* @param[in] aMessage A reference to the message to send.
* @param[in] aMessageInfo A reference to the message info associated with @p aMessage.
@@ -600,16 +605,28 @@ public:
Error SendMessage(Message &aMessage,
const Ip6::MessageInfo &aMessageInfo,
const TxParameters &aTxParameters,
ResponseHandler aHandler = nullptr,
void *aContext = nullptr);
ResponseHandler aHandler,
void *aContext);
#endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
/**
* This method sends a CoAP message with custom transmission parameters.
*
* If Message ID was not set in the header (equal to 0), this method will assign unique Message ID to the message.
*
* @param[in] aMessage A reference to the message to send.
* @param[in] aMessageInfo A reference to the message info associated with @p aMessage.
* @param[in] aTxParameters A reference to transmission parameters for this message.
*
* @retval kErrorNone Successfully sent CoAP message.
* @retval kErrorNoBufs Insufficient buffers available to send the CoAP message.
*
*/
Error SendMessage(Message &aMessage, const Ip6::MessageInfo &aMessageInfo, const TxParameters &aTxParameters);
/**
* This method sends a CoAP message with default transmission parameters.
*
* If a response for a request is expected, respective function and context information should be provided.
* If no response is expected, these arguments should be `nullptr` pointers.
* If Message Id was not set in the header (equal to 0), this function will assign unique Message Id to the message.
* If Message ID was not set in the header (equal to 0), this method will assign unique Message ID to the message.
*
* @param[in] aMessage A reference to the message to send.
* @param[in] aMessageInfo A reference to the message info associated with @p aMessage.
@@ -622,8 +639,22 @@ public:
*/
Error SendMessage(Message &aMessage,
const Ip6::MessageInfo &aMessageInfo,
ResponseHandler aHandler = nullptr,
void *aContext = nullptr);
ResponseHandler aHandler,
void *aContext);
/**
* This method sends a CoAP message with default transmission parameters.
*
* If Message ID was not set in the header (equal to 0), this method will assign unique Message ID to the message.
*
* @param[in] aMessage A reference to the message to send.
* @param[in] aMessageInfo A reference to the message info associated with @p aMessage.
*
* @retval kErrorNone Successfully sent CoAP message.
* @retval kErrorNoBufs Insufficient buffers available to send the CoAP response.
*
*/
Error SendMessage(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
/**
* This method sends a CoAP reset message.
@@ -677,7 +708,20 @@ public:
* @retval kErrorInvalidArgs The @p aRequest header is not of confirmable type.
*
*/
Error SendEmptyAck(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo, Code aCode = kCodeChanged);
Error SendEmptyAck(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo, Code aCode);
/**
* This method sends a CoAP ACK message on which a dummy CoAP response is piggybacked.
*
* @param[in] aRequest A reference to the CoAP Message that was used in CoAP request.
* @param[in] aMessageInfo The message info corresponding to the CoAP request.
*
* @retval kErrorNone Successfully enqueued the CoAP response message.
* @retval kErrorNoBufs Insufficient buffers available to send the CoAP response.
* @retval kErrorInvalidArgs The @p aRequest header is not of confirmable type.
*
*/
Error SendEmptyAck(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo);
/**
* This method sends a header-only CoAP message to indicate no resource matched for the request.
+7
View File
@@ -98,6 +98,13 @@ exit:
return message;
}
Message *MessagePool::Allocate(Message::Type aType) { return Allocate(aType, 0, Message::Settings::GetDefault()); }
Message *MessagePool::Allocate(Message::Type aType, uint16_t aReserveHeader)
{
return Allocate(aType, aReserveHeader, Message::Settings::GetDefault());
}
void MessagePool::Free(Message *aMessage)
{
OT_ASSERT(aMessage->Next() == nullptr && aMessage->Prev() == nullptr);
+22 -3
View File
@@ -1706,9 +1706,28 @@ public:
* @returns A pointer to the message or `nullptr` if no message buffers are available.
*
*/
Message *Allocate(Message::Type aType,
uint16_t aReserveHeader = 0,
const Message::Settings &aSettings = Message::Settings::GetDefault());
Message *Allocate(Message::Type aType, uint16_t aReserveHeader, const Message::Settings &aSettings);
/**
* This method allocates a new message of a given type using default settings.
*
* @param[in] aType The message type.
*
* @returns A pointer to the message or `nullptr` if no message buffers are available.
*
*/
Message *Allocate(Message::Type aType);
/**
* This method allocates a new message with a given type and reserved length using default settings.
*
* @param[in] aType The message type.
* @param[in] aReserveHeader The number of header bytes to reserve.
*
* @returns A pointer to the message or `nullptr` if no message buffers are available.
*
*/
Message *Allocate(Message::Type aType, uint16_t aReserveHeader);
/**
* This method is used to free a message and return all message buffers to the buffer pool.