[cli] add support for CoAP URI queries in CLI requests (#10003)

New feature:

Added CoAP URI query processing functionality to CLI.

Solution:

URI string is checked for a '?' character. If found, the URI query
related part is moved to another string. Afterwards, the modified
`coapUri` string will contain only the path-related parts, and a
second string (`coapUriQuery`) will hold only the URI query related
parts. Then `coapUri` is passed to the original
`otCoapMessageAppendUriPathOptions()` method and `coapUriQuery` is
passed to a new method called `otCoapMessageAppendUriQueryOptions()`,
which - similarly to the other - splits the URI query part into more,
smaller pieces by the delimiter '&' and adds them as separate options
to the CoAP message making the request generated by this CLI function
RFC compliant.  If '?' is not found, everything is processed the old
way.
This commit is contained in:
Dávid Fehér
2024-07-11 09:34:02 -07:00
committed by GitHub
parent 78ecafb0d7
commit 77ca3c54d1
6 changed files with 65 additions and 3 deletions
+13
View File
@@ -608,6 +608,19 @@ otError otCoapMessageAppendObserveOption(otMessage *aMessage, uint32_t aObserve)
*/
otError otCoapMessageAppendUriPathOptions(otMessage *aMessage, const char *aUriPath);
/**
* Appends a Uri-Query option.
*
* @param[in,out] aMessage A pointer to the CoAP message.
* @param[in] aUriQuery A pointer to a NULL-terminated string.
*
* @retval OT_ERROR_NONE Successfully appended the option.
* @retval OT_ERROR_INVALID_ARGS The option type is not equal or greater than the last option type.
* @retval OT_ERROR_NO_BUFS The option length exceeds the buffer size.
*
*/
otError otCoapMessageAppendUriQueryOptions(otMessage *aMessage, const char *aUriQuery);
/**
* Converts a CoAP Block option SZX field to the actual block size
*
+1 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (427)
#define OPENTHREAD_API_VERSION (428)
/**
* @addtogroup api-instance
+16 -2
View File
@@ -583,7 +583,8 @@ otError Coap::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode)
otError error = OT_ERROR_NONE;
otMessage *message = nullptr;
otMessageInfo messageInfo;
uint16_t payloadLength = 0;
uint16_t payloadLength = 0;
char *uriQueryStartPtr = nullptr;
// Default parameters
char coapUri[kMaxUriLength] = "test";
@@ -682,7 +683,20 @@ otError Coap::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode)
}
#endif
SuccessOrExit(error = otCoapMessageAppendUriPathOptions(message, coapUri));
uriQueryStartPtr = const_cast<char *>(StringFind(coapUri, '?'));
if (uriQueryStartPtr == nullptr)
{
// "?" doesn't present in URI --> contains only URI path parts
SuccessOrExit(error = otCoapMessageAppendUriPathOptions(message, coapUri));
}
else
{
// "?" presents in URI --> contains URI path AND URI query parts
*uriQueryStartPtr++ = '\0';
SuccessOrExit(error = otCoapMessageAppendUriPathOptions(message, coapUri));
SuccessOrExit(error = otCoapMessageAppendUriQueryOptions(message, uriQueryStartPtr));
}
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
if (coapBlock)
+5
View File
@@ -99,6 +99,11 @@ otError otCoapMessageAppendUriPathOptions(otMessage *aMessage, const char *aUriP
return AsCoapMessage(aMessage).AppendUriPathOptions(aUriPath);
}
otError otCoapMessageAppendUriQueryOptions(otMessage *aMessage, const char *aUriQuery)
{
return AsCoapMessage(aMessage).AppendUriQueryOptions(aUriQuery);
}
uint16_t otCoapBlockSizeFromExponent(otCoapBlockSzx aSize)
{
return static_cast<uint16_t>(1 << (static_cast<uint8_t>(aSize) + Coap::Message::kBlockSzxBase));
+18
View File
@@ -275,6 +275,24 @@ exit:
return error;
}
Error Message::AppendUriQueryOptions(const char *aUriQuery)
{
Error error = kErrorNone;
const char *cur = aUriQuery;
const char *end;
while ((end = StringFind(cur, '&')) != nullptr)
{
SuccessOrExit(error = AppendOption(kOptionUriQuery, static_cast<uint16_t>(end - cur), cur));
cur = end + 1;
}
SuccessOrExit(error = AppendStringOption(kOptionUriQuery, cur));
exit:
return error;
}
Error Message::AppendBlockOption(Message::BlockType aType, uint32_t aNum, bool aMore, otCoapBlockSzx aSize)
{
Error error = kErrorNone;
+12
View File
@@ -479,6 +479,18 @@ public:
*/
Error ReadUriPathOptions(char (&aUriPath)[kMaxReceivedUriPath + 1]) const;
/**
* Appends a Uri-Query option.
*
* @param[in] aUriQuery A pointer to a null-terminated string.
*
* @retval kErrorNone Successfully appended the option.
* @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 AppendUriQueryOptions(const char *aUriQuery);
/**
* Appends a Block option
*