From ece6ac61e6a54a0778cd4d226ee2185e40e143a1 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 2 Sep 2020 19:47:50 -0700 Subject: [PATCH] [coap] misc enhancements (#5482) This commit contains a group of smaller changes/renames in `Coap` modules: - Add enumeration (c++ style) constants for CoAP `Type` and `Code` and `OptionType`. - Add helpers `Message::Is{Method}Request()` e.g., `IsPostRequest()` - Define Thread URI Paths (in `uri_paths.hpp/cpp`) as char array. - Add helper method in `Coap::Message` for common initializations e.g., `InitAsConfirmablePost()`. - Add helper methods for common `Coap:Message` type and code checks e.g., `IsConfirmablePost()`. - Re-define `OT_COAP_TYPE` values to use 2-bit unsigned int values (per RFC-7252). --- Android.mk | 1 + include/openthread/coap.h | 12 +- include/openthread/instance.h | 2 +- src/cli/cli_coap.cpp | 2 +- src/cli/cli_coap_secure.cpp | 2 +- src/core/BUILD.gn | 3 +- src/core/CMakeLists.txt | 1 + src/core/Makefile.am | 3 +- src/core/api/coap_api.cpp | 8 +- src/core/backbone_router/bbr_manager.cpp | 10 +- .../multicast_listeners_table.cpp | 2 +- src/core/coap/coap.cpp | 50 +-- src/core/coap/coap.hpp | 2 +- src/core/coap/coap_message.cpp | 179 ++++++---- src/core/coap/coap_message.hpp | 315 +++++++++++++--- src/core/meshcop/announce_begin_client.cpp | 6 +- src/core/meshcop/border_agent.cpp | 80 +++-- src/core/meshcop/commissioner.cpp | 32 +- src/core/meshcop/dataset_manager.cpp | 28 +- src/core/meshcop/dataset_manager_ftd.cpp | 4 +- src/core/meshcop/energy_scan_client.cpp | 10 +- src/core/meshcop/joiner.cpp | 14 +- src/core/meshcop/joiner_router.cpp | 14 +- src/core/meshcop/meshcop_leader.cpp | 8 +- src/core/meshcop/panid_query_client.cpp | 10 +- src/core/thread/address_resolver.cpp | 26 +- src/core/thread/announce_begin_server.cpp | 6 +- src/core/thread/dua_manager.cpp | 14 +- src/core/thread/energy_scan_server.cpp | 8 +- src/core/thread/mle_router.cpp | 16 +- src/core/thread/mlr_manager.cpp | 8 +- src/core/thread/network_data.cpp | 4 +- src/core/thread/network_data_leader.cpp | 2 +- src/core/thread/network_data_leader_ftd.cpp | 8 +- src/core/thread/network_diagnostic.cpp | 31 +- src/core/thread/panid_query_server.cpp | 8 +- src/core/thread/thread_netif.cpp | 2 +- src/core/thread/thread_uri_paths.hpp | 338 ------------------ src/core/thread/uri_paths.cpp | 76 ++++ src/core/thread/uri_paths.hpp | 89 +++++ 40 files changed, 752 insertions(+), 682 deletions(-) delete mode 100644 src/core/thread/thread_uri_paths.hpp create mode 100644 src/core/thread/uri_paths.cpp create mode 100644 src/core/thread/uri_paths.hpp diff --git a/Android.mk b/Android.mk index c764b100a..ce380c334 100644 --- a/Android.mk +++ b/Android.mk @@ -275,6 +275,7 @@ LOCAL_SRC_FILES := \ src/core/thread/thread_netif.cpp \ src/core/thread/tmf.cpp \ src/core/thread/topology.cpp \ + src/core/thread/uri_paths.cpp \ src/core/utils/channel_manager.cpp \ src/core/utils/channel_monitor.cpp \ src/core/utils/child_supervision.cpp \ diff --git a/include/openthread/coap.h b/include/openthread/coap.h index 99facfd64..2899c1b20 100644 --- a/include/openthread/coap.h +++ b/include/openthread/coap.h @@ -58,6 +58,8 @@ extern "C" { #define OT_DEFAULT_COAP_PORT 5683 ///< Default CoAP port, as specified in RFC 7252 +#define OT_COAP_DEFAULT_TOKEN_LENGTH 2 ///< Default token length. + #define OT_COAP_MAX_TOKEN_LENGTH 8 ///< Max token length as specified (RFC 7252). #define OT_COAP_MAX_RETRANSMIT 20 ///< Max retransmit supported by OpenThread. @@ -65,15 +67,15 @@ extern "C" { #define OT_COAP_MIN_ACK_TIMEOUT 1000 ///< Minimal ACK timeout in milliseconds supported by OpenThread. /** - * CoAP Type values. + * CoAP Type values (2 bit unsigned integer). * */ typedef enum otCoapType { - OT_COAP_TYPE_CONFIRMABLE = 0x00, ///< Confirmable - OT_COAP_TYPE_NON_CONFIRMABLE = 0x10, ///< Non-confirmable - OT_COAP_TYPE_ACKNOWLEDGMENT = 0x20, ///< Acknowledgment - OT_COAP_TYPE_RESET = 0x30, ///< Reset + OT_COAP_TYPE_CONFIRMABLE = 0, ///< Confirmable + OT_COAP_TYPE_NON_CONFIRMABLE = 1, ///< Non-confirmable + OT_COAP_TYPE_ACKNOWLEDGMENT = 2, ///< Acknowledgment + OT_COAP_TYPE_RESET = 3, ///< Reset } otCoapType; /** diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 36ea89d36..0b3b829cb 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (26) +#define OPENTHREAD_API_VERSION (27) /** * @addtogroup api-instance diff --git a/src/cli/cli_coap.cpp b/src/cli/cli_coap.cpp index f79c2b3af..f3f351917 100644 --- a/src/cli/cli_coap.cpp +++ b/src/cli/cli_coap.cpp @@ -447,7 +447,7 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, char *aArgs[]) VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); otCoapMessageInit(message, coapType, coapCode); - otCoapMessageGenerateToken(message, ot::Coap::Message::kDefaultTokenLength); + otCoapMessageGenerateToken(message, OT_COAP_DEFAULT_TOKEN_LENGTH); #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE if (coapObserve) diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index 754be2c8a..f696082f3 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -284,7 +284,7 @@ otError CoapSecure::ProcessRequest(uint8_t aArgsLength, char *aArgs[]) VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); otCoapMessageInit(message, coapType, coapCode); - otCoapMessageGenerateToken(message, ot::Coap::Message::kDefaultTokenLength); + otCoapMessageGenerateToken(message, OT_COAP_DEFAULT_TOKEN_LENGTH); SuccessOrExit(error = otCoapMessageAppendUriPathOptions(message, coapUri)); if (aArgsLength > (4 - indexShifter)) diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn index 3e745cdef..14250536f 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -537,13 +537,14 @@ openthread_core_files = [ "thread/thread_netif.cpp", "thread/thread_netif.hpp", "thread/thread_tlvs.hpp", - "thread/thread_uri_paths.hpp", "thread/time_sync_service.cpp", "thread/time_sync_service.hpp", "thread/tmf.cpp", "thread/tmf.hpp", "thread/topology.cpp", "thread/topology.hpp", + "thread/uri_paths.cpp", + "thread/uri_paths.hpp", "utils/channel_manager.cpp", "utils/channel_manager.hpp", "utils/channel_monitor.cpp", diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index fa63f38f2..a35f59a72 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -205,6 +205,7 @@ set(COMMON_SOURCES thread/time_sync_service.cpp thread/tmf.cpp thread/topology.cpp + thread/uri_paths.cpp utils/channel_manager.cpp utils/channel_monitor.cpp utils/child_supervision.cpp diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 04b3c0c7d..45695b631 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -247,6 +247,7 @@ SOURCES_COMMON = \ thread/time_sync_service.cpp \ thread/tmf.cpp \ thread/topology.cpp \ + thread/uri_paths.cpp \ utils/channel_manager.cpp \ utils/channel_monitor.cpp \ utils/child_supervision.cpp \ @@ -469,10 +470,10 @@ HEADERS_COMMON = \ thread/src_match_controller.hpp \ thread/thread_netif.hpp \ thread/thread_tlvs.hpp \ - thread/thread_uri_paths.hpp \ thread/time_sync_service.hpp \ thread/tmf.hpp \ thread/topology.hpp \ + thread/uri_paths.hpp \ utils/channel_manager.hpp \ utils/channel_monitor.hpp \ utils/child_supervision.hpp \ diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index a817319ba..8ab8347ce 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -52,7 +52,7 @@ otMessage *otCoapNewMessage(otInstance *aInstance, const otMessageSettings *aSet void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode) { - static_cast(aMessage)->Init(aType, aCode); + static_cast(aMessage)->Init(static_cast(aType), static_cast(aCode)); } otError otCoapMessageInitResponse(otMessage *aResponse, const otMessage *aRequest, otCoapType aType, otCoapCode aCode) @@ -60,7 +60,7 @@ otError otCoapMessageInitResponse(otMessage *aResponse, const otMessage *aReques Coap::Message & response = *static_cast(aResponse); const Coap::Message &request = *static_cast(aRequest); - response.Init(aType, aCode); + response.Init(static_cast(aType), static_cast(aCode)); response.SetMessageId(request.GetMessageId()); return response.SetToken(request.GetToken(), request.GetTokenLength()); @@ -139,12 +139,12 @@ otError otCoapMessageSetPayloadMarker(otMessage *aMessage) otCoapType otCoapMessageGetType(const otMessage *aMessage) { - return static_cast(aMessage)->GetType(); + return static_cast(static_cast(aMessage)->GetType()); } otCoapCode otCoapMessageGetCode(const otMessage *aMessage) { - return static_cast(aMessage)->GetCode(); + return static_cast(static_cast(aMessage)->GetCode()); } const char *otCoapMessageCodeToString(const otMessage *aMessage) diff --git a/src/core/backbone_router/bbr_manager.cpp b/src/core/backbone_router/bbr_manager.cpp index b7532abbe..0c352b965 100644 --- a/src/core/backbone_router/bbr_manager.cpp +++ b/src/core/backbone_router/bbr_manager.cpp @@ -45,7 +45,7 @@ #include "thread/mle_types.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { @@ -53,8 +53,8 @@ namespace BackboneRouter { Manager::Manager(Instance &aInstance) : InstanceLocator(aInstance) - , mMulticastListenerRegistration(OT_URI_PATH_MLR, Manager::HandleMulticastListenerRegistration, this) - , mDuaRegistration(OT_URI_PATH_DUA_REGISTRATION_REQUEST, Manager::HandleDuaRegistration, this) + , mMulticastListenerRegistration(UriPath::kMlr, Manager::HandleMulticastListenerRegistration, this) + , mDuaRegistration(UriPath::kDuaRegistrationRequest, Manager::HandleDuaRegistration, this) , mMulticastListenersTable(aInstance) , mTimer(aInstance, Manager::HandleTimer, this) #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE @@ -114,7 +114,7 @@ void Manager::HandleMulticastListenerRegistration(const Coap::Message &aMessage, bool hasCommissionerSessionIdTlv = false; bool processTimeoutTlv = false; - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_PARSE); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), error = OT_ERROR_PARSE); #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE // Required by Test Specification 5.10.22 DUA-TC-26, only for certification purpose @@ -276,7 +276,7 @@ void Manager::HandleDuaRegistration(const Coap::Message &aMessage, const Ip6::Me Ip6::Address target; Ip6::InterfaceIdentifier meshLocalIid; - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_PARSE); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); SuccessOrExit(error = Tlv::FindTlv(aMessage, ThreadTlv::kMeshLocalEid, &meshLocalIid, sizeof(meshLocalIid))); diff --git a/src/core/backbone_router/multicast_listeners_table.cpp b/src/core/backbone_router/multicast_listeners_table.cpp index bcb58760b..6450a8219 100644 --- a/src/core/backbone_router/multicast_listeners_table.cpp +++ b/src/core/backbone_router/multicast_listeners_table.cpp @@ -42,7 +42,7 @@ #include "common/random.hpp" #include "thread/mle_types.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 2d8bfa006..fcefd6694 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -141,11 +141,11 @@ otError CoapBase::SendMessage(Message & aMessage, switch (aMessage.GetType()) { - case OT_COAP_TYPE_ACKNOWLEDGMENT: + case kTypeAck: mResponsesQueue.EnqueueResponse(aMessage, aMessageInfo, aTxParameters); break; - case OT_COAP_TYPE_RESET: - OT_ASSERT(aMessage.GetCode() == OT_COAP_CODE_EMPTY); + case kTypeReset: + OT_ASSERT(aMessage.GetCode() == kCodeEmpty); break; default: aMessage.SetMessageId(mMessageId++); @@ -175,10 +175,10 @@ otError CoapBase::SendMessage(Message & aMessage, bool observe; SuccessOrExit(error = iterator.Init(&aMessage)); - observe = (iterator.GetFirstOptionMatching(OT_COAP_OPTION_OBSERVE) != nullptr); + observe = (iterator.GetFirstOptionMatching(kOptionObserve) != nullptr); // Special case, if we're sending a GET with Observe=1, that is a cancellation. - if (observe && (aMessage.GetCode() == OT_COAP_CODE_GET)) + if (observe && aMessage.IsGetRequest()) { uint64_t observeVal = 0; @@ -244,26 +244,26 @@ otError CoapBase::SendMessage(Message & aMessage, otError CoapBase::SendReset(Message &aRequest, const Ip6::MessageInfo &aMessageInfo) { - return SendEmptyMessage(OT_COAP_TYPE_RESET, aRequest, aMessageInfo); + return SendEmptyMessage(kTypeReset, aRequest, aMessageInfo); } otError CoapBase::SendAck(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo) { - return SendEmptyMessage(OT_COAP_TYPE_ACKNOWLEDGMENT, aRequest, aMessageInfo); + return SendEmptyMessage(kTypeAck, aRequest, aMessageInfo); } otError CoapBase::SendEmptyAck(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo) { - return (aRequest.IsConfirmable() ? SendHeaderResponse(OT_COAP_CODE_CHANGED, aRequest, aMessageInfo) + return (aRequest.IsConfirmable() ? SendHeaderResponse(kCodeChanged, aRequest, aMessageInfo) : OT_ERROR_INVALID_ARGS); } otError CoapBase::SendNotFound(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo) { - return SendHeaderResponse(OT_COAP_CODE_NOT_FOUND, aRequest, aMessageInfo); + return SendHeaderResponse(kCodeNotFound, aRequest, aMessageInfo); } -otError CoapBase::SendEmptyMessage(Message::Type aType, const Message &aRequest, const Ip6::MessageInfo &aMessageInfo) +otError CoapBase::SendEmptyMessage(Type aType, const Message &aRequest, const Ip6::MessageInfo &aMessageInfo) { otError error = OT_ERROR_NONE; Message *message = nullptr; @@ -272,7 +272,7 @@ otError CoapBase::SendEmptyMessage(Message::Type aType, const Message &aRequest, VerifyOrExit((message = NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(aType, OT_COAP_CODE_EMPTY); + message->Init(aType, kCodeEmpty); message->SetMessageId(aRequest.GetMessageId()); message->Finish(); @@ -298,13 +298,13 @@ otError CoapBase::SendHeaderResponse(Message::Code aCode, const Message &aReques switch (aRequest.GetType()) { - case OT_COAP_TYPE_CONFIRMABLE: - message->Init(OT_COAP_TYPE_ACKNOWLEDGMENT, aCode); + case kTypeConfirmable: + message->Init(kTypeAck, aCode); message->SetMessageId(aRequest.GetMessageId()); break; - case OT_COAP_TYPE_NON_CONFIRMABLE: - message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, aCode); + case kTypeNonConfirmable: + message->Init(kTypeNonConfirmable, aCode); break; default: @@ -506,8 +506,8 @@ Message *CoapBase::FindRelatedRequest(const Message & aResponse, { switch (aResponse.GetType()) { - case OT_COAP_TYPE_RESET: - case OT_COAP_TYPE_ACKNOWLEDGMENT: + case kTypeReset: + case kTypeAck: if (aResponse.GetMessageId() == message->GetMessageId()) { ExitNow(); @@ -515,8 +515,8 @@ Message *CoapBase::FindRelatedRequest(const Message & aResponse, break; - case OT_COAP_TYPE_CONFIRMABLE: - case OT_COAP_TYPE_NON_CONFIRMABLE: + case kTypeConfirmable: + case kTypeNonConfirmable: if (aResponse.IsTokenEqual(*message)) { ExitNow(); @@ -573,13 +573,13 @@ void CoapBase::ProcessReceivedResponse(Message &aMessage, const Ip6::MessageInfo OptionIterator iterator; SuccessOrExit(error = iterator.Init(&aMessage)); - responseObserve = (iterator.GetFirstOptionMatching(OT_COAP_OPTION_OBSERVE) != nullptr); + responseObserve = (iterator.GetFirstOptionMatching(kOptionObserve) != nullptr); } #endif switch (aMessage.GetType()) { - case OT_COAP_TYPE_RESET: + case kTypeReset: if (aMessage.IsEmpty()) { FinalizeCoapTransaction(*request, metadata, nullptr, nullptr, OT_ERROR_ABORT); @@ -588,7 +588,7 @@ void CoapBase::ProcessReceivedResponse(Message &aMessage, const Ip6::MessageInfo // Silently ignore non-empty reset messages (RFC 7252, p. 4.2). break; - case OT_COAP_TYPE_ACKNOWLEDGMENT: + case kTypeAck: if (aMessage.IsEmpty()) { // Empty acknowledgment. @@ -646,12 +646,12 @@ void CoapBase::ProcessReceivedResponse(Message &aMessage, const Ip6::MessageInfo // or with no token match (RFC 7252, p. 5.3.2) break; - case OT_COAP_TYPE_CONFIRMABLE: + case kTypeConfirmable: // Send empty ACK if it is a CON message. IgnoreError(SendAck(aMessage, aMessageInfo)); // Fall through // Handling of RFC7641 and multicast is below. - case OT_COAP_TYPE_NON_CONFIRMABLE: + case kTypeNonConfirmable: // Separate response or observation notification. If the request was to a multicast // address, OR both the request and response carry Observe options, then this is NOT // the final message, we may see multiples. @@ -719,7 +719,7 @@ void CoapBase::ProcessReceivedRequest(Message &aMessage, const Ip6::MessageInfo { switch (option->mNumber) { - case OT_COAP_OPTION_URI_PATH: + case kOptionUriPath: if (curUriPath != uriPath) { *curUriPath++ = '/'; diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 5e9387879..c2d7b15d7 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -568,7 +568,7 @@ private: void ProcessReceivedResponse(Message &aMessage, const Ip6::MessageInfo &aMessageInfo); void SendCopy(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo); - otError SendEmptyMessage(Message::Type aType, const Message &aRequest, const Ip6::MessageInfo &aMessageInfo); + otError SendEmptyMessage(Type aType, const Message &aRequest, const Ip6::MessageInfo &aMessageInfo); otError Send(ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index ba5fc572a..6f11371a5 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -72,6 +72,41 @@ exit: return error; } +void Message::InitAsConfirmablePost(void) +{ + Init(kTypeConfirmable, kCodePost); +} + +void Message::InitAsNonConfirmablePost(void) +{ + Init(kTypeNonConfirmable, kCodePost); +} + +otError Message::InitAsConfirmablePost(const char *aUriPath) +{ + return Init(kTypeConfirmable, kCodePost, aUriPath); +} + +otError Message::InitAsNonConfirmablePost(const char *aUriPath) +{ + return Init(kTypeNonConfirmable, kCodePost, aUriPath); +} + +otError Message::InitAsPost(const Ip6::Address &aDestination, const char *aUriPath) +{ + return Init(aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable, kCodePost, aUriPath); +} + +bool Message::IsConfirmablePostRequest(void) const +{ + return IsConfirmable() && IsPostRequest(); +} + +bool Message::IsNonConfirmablePostRequest(void) const +{ + return IsNonConfirmable() && IsPostRequest(); +} + void Message::Finish(void) { Write(0, GetOptionStart(), &GetHelpData().mHeader); @@ -166,7 +201,7 @@ otError Message::AppendStringOption(uint16_t aNumber, const char *aValue) otError Message::AppendObserveOption(uint32_t aObserve) { - return AppendUintOption(OT_COAP_OPTION_OBSERVE, aObserve & 0xFFFFFF); + return AppendUintOption(kOptionObserve, aObserve & 0xFFFFFF); } otError Message::AppendUriPathOptions(const char *aUriPath) @@ -177,11 +212,11 @@ otError Message::AppendUriPathOptions(const char *aUriPath) while ((end = strchr(cur, '/')) != nullptr) { - SuccessOrExit(error = AppendOption(OT_COAP_OPTION_URI_PATH, static_cast(end - cur), cur)); + SuccessOrExit(error = AppendOption(kOptionUriPath, static_cast(end - cur), cur)); cur = end + 1; } - SuccessOrExit(error = AppendStringOption(OT_COAP_OPTION_URI_PATH, cur)); + SuccessOrExit(error = AppendStringOption(kOptionUriPath, cur)); exit: return error; @@ -199,7 +234,7 @@ otError Message::AppendBlockOption(Message::BlockType aType, uint32_t aNum, bool encoded |= static_cast(aMore << kBlockMOffset); encoded |= aNum << kBlockNumOffset; - error = AppendUintOption((aType == kBlockType1) ? OT_COAP_OPTION_BLOCK1 : OT_COAP_OPTION_BLOCK2, encoded); + error = AppendUintOption((aType == kBlockType1) ? kOptionBlock1 : kOptionBlock2, encoded); exit: return error; @@ -207,22 +242,22 @@ exit: otError Message::AppendProxyUriOption(const char *aProxyUri) { - return AppendStringOption(OT_COAP_OPTION_PROXY_URI, aProxyUri); + return AppendStringOption(kOptionProxyUri, aProxyUri); } otError Message::AppendContentFormatOption(otCoapOptionContentFormat aContentFormat) { - return AppendUintOption(OT_COAP_OPTION_CONTENT_FORMAT, static_cast(aContentFormat)); + return AppendUintOption(kOptionContentFormat, static_cast(aContentFormat)); } otError Message::AppendMaxAgeOption(uint32_t aMaxAge) { - return AppendUintOption(OT_COAP_OPTION_MAX_AGE, aMaxAge); + return AppendUintOption(kOptionMaxAge, aMaxAge); } otError Message::AppendUriQueryOption(const char *aUriQuery) { - return AppendStringOption(OT_COAP_OPTION_URI_QUERY, aUriQuery); + return AppendStringOption(kOptionUriQuery, aUriQuery); } otError Message::SetPayloadMarker(void) @@ -272,9 +307,10 @@ exit: otError Message::SetToken(const uint8_t *aToken, uint8_t aTokenLength) { - GetHelpData().mHeader.mVersionTypeToken = (GetHelpData().mHeader.mVersionTypeToken & ~kTokenLengthMask) | - ((aTokenLength << kTokenLengthOffset) & kTokenLengthMask); - memcpy(GetHelpData().mHeader.mToken, aToken, aTokenLength); + OT_ASSERT(aTokenLength <= kMaxTokenLength); + + SetTokenLength(aTokenLength); + memcpy(GetToken(), aToken, aTokenLength); GetHelpData().mHeaderLength += aTokenLength; return SetLength(GetHelpData().mHeaderLength); @@ -282,7 +318,7 @@ otError Message::SetToken(const uint8_t *aToken, uint8_t aTokenLength) otError Message::SetToken(uint8_t aTokenLength) { - uint8_t token[kMaxTokenLength] = {0}; + uint8_t token[kMaxTokenLength]; OT_ASSERT(aTokenLength <= sizeof(token)); @@ -293,7 +329,7 @@ otError Message::SetToken(uint8_t aTokenLength) otError Message::SetDefaultResponseHeader(const Message &aRequest) { - Init(OT_COAP_TYPE_ACKNOWLEDGMENT, OT_COAP_CODE_CHANGED); + Init(kTypeAck, kCodeChanged); SetMessageId(aRequest.GetMessageId()); @@ -315,97 +351,100 @@ exit: #if OPENTHREAD_CONFIG_COAP_API_ENABLE const char *Message::CodeToString(void) const { - const char *codeString; + const char *string; switch (GetCode()) { - case OT_COAP_CODE_INTERNAL_ERROR: - codeString = "InternalError"; + case kCodeEmpty: + string = "Empty"; break; - case OT_COAP_CODE_METHOD_NOT_ALLOWED: - codeString = "MethodNotAllowed"; + case kCodeGet: + string = "Get"; break; - case OT_COAP_CODE_CONTENT: - codeString = "Content"; + case kCodePost: + string = "Post"; break; - case OT_COAP_CODE_EMPTY: - codeString = "Empty"; + case kCodePut: + string = "Put"; break; - case OT_COAP_CODE_GET: - codeString = "Get"; + case kCodeDelete: + string = "Delete"; break; - case OT_COAP_CODE_POST: - codeString = "Post"; + case kCodeCreated: + string = "Created"; break; - case OT_COAP_CODE_PUT: - codeString = "Put"; + case kCodeDeleted: + string = "Deleted"; break; - case OT_COAP_CODE_DELETE: - codeString = "Delete"; + case kCodeValid: + string = "Valid"; break; - case OT_COAP_CODE_NOT_FOUND: - codeString = "NotFound"; + case kCodeChanged: + string = "Changed"; break; - case OT_COAP_CODE_UNSUPPORTED_FORMAT: - codeString = "UnsupportedFormat"; + case kCodeContent: + string = "Content"; break; - case OT_COAP_CODE_RESPONSE_MIN: - codeString = "ResponseMin"; + case kCodeContinue: + string = "Continue"; break; - case OT_COAP_CODE_CREATED: - codeString = "Created"; + case kCodeBadRequest: + string = "BadRequest"; break; - case OT_COAP_CODE_DELETED: - codeString = "Deleted"; + case kCodeUnauthorized: + string = "Unauthorized"; break; - case OT_COAP_CODE_VALID: - codeString = "Valid"; + case kCodeBadOption: + string = "BadOption"; break; - case OT_COAP_CODE_CHANGED: - codeString = "Changed"; + case kCodeForbidden: + string = "Forbidden"; break; - case OT_COAP_CODE_BAD_REQUEST: - codeString = "BadRequest"; + case kCodeNotFound: + string = "NotFound"; break; - case OT_COAP_CODE_UNAUTHORIZED: - codeString = "Unauthorized"; + case kCodeMethodNotAllowed: + string = "MethodNotAllowed"; break; - case OT_COAP_CODE_BAD_OPTION: - codeString = "BadOption"; + case kCodeNotAcceptable: + string = "NotAcceptable"; break; - case OT_COAP_CODE_FORBIDDEN: - codeString = "Forbidden"; + case kCodeRequestIncomplete: + string = "RequestIncomplete"; break; - case OT_COAP_CODE_NOT_ACCEPTABLE: - codeString = "NotAcceptable"; + case kCodePreconditionFailed: + string = "PreconditionFailed"; break; - case OT_COAP_CODE_PRECONDITION_FAILED: - codeString = "PreconditionFailed"; + case kCodeRequestTooLarge: + string = "RequestTooLarge"; break; - case OT_COAP_CODE_REQUEST_TOO_LARGE: - codeString = "RequestTooLarge"; + case kCodeUnsupportedFormat: + string = "UnsupportedFormat"; break; - case OT_COAP_CODE_NOT_IMPLEMENTED: - codeString = "NotImplemented"; + case kCodeInternalError: + string = "InternalError"; break; - case OT_COAP_CODE_BAD_GATEWAY: - codeString = "BadGateway"; + case kCodeNotImplemented: + string = "NotImplemented"; break; - case OT_COAP_CODE_SERVICE_UNAVAILABLE: - codeString = "ServiceUnavailable"; + case kCodeBadGateway: + string = "BadGateway"; break; - case OT_COAP_CODE_GATEWAY_TIMEOUT: - codeString = "GatewayTimeout"; + case kCodeServiceUnavailable: + string = "ServiceUnavailable"; break; - case OT_COAP_CODE_PROXY_NOT_SUPPORTED: - codeString = "ProxyNotSupported"; + case kCodeGatewayTimeout: + string = "GatewayTimeout"; + break; + case kCodeProxyNotSupported: + string = "ProxyNotSupported"; break; default: - codeString = "Unknown"; + string = "Unknown"; break; } - return codeString; + return string; } #endif // OPENTHREAD_CONFIG_COAP_API_ENABLE diff --git a/src/core/coap/coap_message.hpp b/src/core/coap/coap_message.hpp index 912d0a61a..28129999c 100644 --- a/src/core/coap/coap_message.hpp +++ b/src/core/coap/coap_message.hpp @@ -42,6 +42,7 @@ #include "common/code_utils.hpp" #include "common/encoding.hpp" #include "common/message.hpp" +#include "net/ip6_address.hpp" namespace ot { @@ -67,6 +68,91 @@ using ot::Encoding::BigEndian::HostSwap16; class OptionIterator; +/** + * CoAP Type values. + * + */ +enum Type : uint8_t +{ + kTypeConfirmable = OT_COAP_TYPE_CONFIRMABLE, ///< Confirmable type. + kTypeNonConfirmable = OT_COAP_TYPE_NON_CONFIRMABLE, ///< Non-confirmable type. + kTypeAck = OT_COAP_TYPE_ACKNOWLEDGMENT, ///< Acknowledgment type. + kTypeReset = OT_COAP_TYPE_RESET, ///< Reset type. +}; + +/** + * CoAP Code values. + * + */ +enum Code : uint8_t +{ + // Request Codes: + + kCodeEmpty = OT_COAP_CODE_EMPTY, ///< Empty message code + kCodeGet = OT_COAP_CODE_GET, ///< Get + kCodePost = OT_COAP_CODE_POST, ///< Post + kCodePut = OT_COAP_CODE_PUT, ///< Put + kCodeDelete = OT_COAP_CODE_DELETE, ///< Delete + + // Response Codes: + + kCodeCreated = OT_COAP_CODE_CREATED, ///< Created + kCodeDeleted = OT_COAP_CODE_DELETED, ///< Deleted + kCodeValid = OT_COAP_CODE_VALID, ///< Valid + kCodeChanged = OT_COAP_CODE_CHANGED, ///< Changed + kCodeContent = OT_COAP_CODE_CONTENT, ///< Content + kCodeContinue = OT_COAP_CODE_CONTINUE, ///< RFC7959 Continue + + // Client Error Codes: + + kCodeBadRequest = OT_COAP_CODE_BAD_REQUEST, ///< Bad Request + kCodeUnauthorized = OT_COAP_CODE_UNAUTHORIZED, ///< Unauthorized + kCodeBadOption = OT_COAP_CODE_BAD_OPTION, ///< Bad Option + kCodeForbidden = OT_COAP_CODE_FORBIDDEN, ///< Forbidden + kCodeNotFound = OT_COAP_CODE_NOT_FOUND, ///< Not Found + kCodeMethodNotAllowed = OT_COAP_CODE_METHOD_NOT_ALLOWED, ///< Method Not Allowed + kCodeNotAcceptable = OT_COAP_CODE_NOT_ACCEPTABLE, ///< Not Acceptable + kCodeRequestIncomplete = OT_COAP_CODE_REQUEST_INCOMPLETE, ///< RFC7959 Request Entity Incomplete + kCodePreconditionFailed = OT_COAP_CODE_PRECONDITION_FAILED, ///< Precondition Failed + kCodeRequestTooLarge = OT_COAP_CODE_REQUEST_TOO_LARGE, ///< Request Entity Too Large + kCodeUnsupportedFormat = OT_COAP_CODE_UNSUPPORTED_FORMAT, ///< Unsupported Content-Format + + // Server Error Codes: + + kCodeInternalError = OT_COAP_CODE_INTERNAL_ERROR, ///< Internal Server Error + kCodeNotImplemented = OT_COAP_CODE_NOT_IMPLEMENTED, ///< Not Implemented + kCodeBadGateway = OT_COAP_CODE_BAD_GATEWAY, ///< Bad Gateway + kCodeServiceUnavailable = OT_COAP_CODE_SERVICE_UNAVAILABLE, ///< Service Unavailable + kCodeGatewayTimeout = OT_COAP_CODE_GATEWAY_TIMEOUT, ///< Gateway Timeout + kCodeProxyNotSupported = OT_COAP_CODE_PROXY_NOT_SUPPORTED, ///< Proxying Not Supported +}; + +/** + * CoAP Option Numbers. + * + */ +enum : uint16_t +{ + kOptionIfMatch = OT_COAP_OPTION_IF_MATCH, ///< If-Match + kOptionUriHost = OT_COAP_OPTION_URI_HOST, ///< Uri-Host + kOptionETag = OT_COAP_OPTION_E_TAG, ///< ETag + kOptionIfNoneMatch = OT_COAP_OPTION_IF_NONE_MATCH, ///< If-None-Match + kOptionObserve = OT_COAP_OPTION_OBSERVE, ///< Observe [RFC7641] + kOptionUriPort = OT_COAP_OPTION_URI_PORT, ///< Uri-Port + kOptionLocationPath = OT_COAP_OPTION_LOCATION_PATH, ///< Location-Path + kOptionUriPath = OT_COAP_OPTION_URI_PATH, ///< Uri-Path + kOptionContentFormat = OT_COAP_OPTION_CONTENT_FORMAT, ///< Content-Format + kOptionMaxAge = OT_COAP_OPTION_MAX_AGE, ///< Max-Age + kOptionUriQuery = OT_COAP_OPTION_URI_QUERY, ///< Uri-Query + kOptionAccept = OT_COAP_OPTION_ACCEPT, ///< Accept + kOptionLocationQuery = OT_COAP_OPTION_LOCATION_QUERY, ///< Location-Query + kOptionBlock2 = OT_COAP_OPTION_BLOCK2, ///< Block2 (RFC7959) + kOptionBlock1 = OT_COAP_OPTION_BLOCK1, ///< Block1 (RFC7959) + kOptionProxyUri = OT_COAP_OPTION_PROXY_URI, ///< Proxy-Uri + kOptionProxyScheme = OT_COAP_OPTION_PROXY_SCHEME, ///< Proxy-Scheme + kOptionSize1 = OT_COAP_OPTION_SIZE1, ///< Size1 +}; + /** * This class implements CoAP message generation and parsing. * @@ -76,26 +162,14 @@ class Message : public ot::Message friend class OptionIterator; public: - enum + enum : uint8_t { - kVersion1 = 1, ///< Version 1 - kMinHeaderLength = 4, ///< Minimum header length - kMaxHeaderLength = 512, ///< Maximum header length - kDefaultTokenLength = 2, ///< Default token length - kTypeOffset = 4, ///< The type offset in the first byte of a CoAP header + kDefaultTokenLength = OT_COAP_DEFAULT_TOKEN_LENGTH, ///< Default token length + kMaxTokenLength = OT_COAP_MAX_TOKEN_LENGTH, ///< Maximum token length. }; - /** - * CoAP Type values. - * - */ - typedef otCoapType Type; - - /** - * CoAP Code values. - * - */ - typedef otCoapCode Code; + typedef ot::Coap::Type Type; ///< CoAP Type. + typedef ot::Coap::Code Code; ///< CoAP Code. /** * CoAP Block1/Block2 Types @@ -127,6 +201,18 @@ public: */ void Init(Type aType, Code aCode); + /** + * This method initializes the CoAP header as `kTypeConfirmable` and `kCodePost`. + * + */ + void InitAsConfirmablePost(void); + + /** + * This method initializes the CoAP header as `kTypeNonConfirmable` and `kCodePost`. + * + */ + void InitAsNonConfirmablePost(void); + /** * This method initializes the CoAP header with specific Type and Code. * @@ -140,6 +226,42 @@ public: */ otError Init(Type aType, Code aCode, const char *aUriPath); + /** + * This method initializes the CoAP header as `kTypeConfirmable` and `kCodePost` with a given URI Path. + * + * @param[in] aUriPath A pointer to a null-terminated string. + * + * @retval OT_ERROR_NONE Successfully appended the option. + * @retval OT_ERROR_NO_BUFS The option length exceeds the buffer size. + * + */ + otError InitAsConfirmablePost(const char *aUriPath); + + /** + * This method initializes the CoAP header as `kTypeNonConfirmable` and `kCodePost` with a given URI Path. + * + * @param[in] aUriPath A pointer to a null-terminated string. + * + * @retval OT_ERROR_NONE Successfully appended the option. + * @retval OT_ERROR_NO_BUFS The option length exceeds the buffer size. + * + */ + otError InitAsNonConfirmablePost(const char *aUriPath); + + /** + * This method initializes the CoAP header as `kCodePost` with a given URI Path with its type determined from a + * given destination IPv6 address. + * + * @param[in] aDestination The message destination IPv6 address used to determine the CoAP type, + * `kTypeNonConfirmable` if multicast address, `kTypeConfirmable` otherwise. + * @param[in] aUriPath A pointer to a null-terminated string. + * + * @retval OT_ERROR_NONE Successfully appended the option. + * @retval OT_ERROR_NO_BUFS The option length exceeds the buffer size. + * + */ + otError InitAsPost(const Ip6::Address &aDestination, const char *aUriPath); + /** * This method writes header to the message. This must be called before sending the message. * @@ -175,7 +297,7 @@ public: * @returns The Type value. * */ - Type GetType(void) const { return static_cast(GetHelpData().mHeader.mVersionTypeToken & kTypeMask); } + uint8_t GetType(void) const { return (GetHelpData().mHeader.mVersionTypeToken & kTypeMask) >> kTypeOffset; } /** * This method sets the Type value. @@ -186,7 +308,7 @@ public: void SetType(Type aType) { GetHelpData().mHeader.mVersionTypeToken &= ~kTypeMask; - GetHelpData().mHeader.mVersionTypeToken |= aType; + GetHelpData().mHeader.mVersionTypeToken |= (static_cast(aType) << kTypeOffset); } /** @@ -195,7 +317,7 @@ public: * @returns The Code value. * */ - Code GetCode(void) const { return static_cast(GetHelpData().mHeader.mCode); } + uint8_t GetCode(void) const { return static_cast(GetHelpData().mHeader.mCode); } /** * This method sets the Code value. @@ -457,7 +579,7 @@ public: * @retval FALSE Message is not an empty message header. * */ - bool IsEmpty(void) const { return (GetCode() == 0); } + bool IsEmpty(void) const { return (GetCode() == kCodeEmpty); } /** * This method checks if a header is a request header. @@ -466,7 +588,43 @@ public: * @retval FALSE Message is not a request header. * */ - bool IsRequest(void) const { return (GetCode() >= OT_COAP_CODE_GET && GetCode() <= OT_COAP_CODE_DELETE); } + bool IsRequest(void) const { return (GetCode() >= kCodeGet) && (GetCode() <= kCodeDelete); } + + /** + * This method indicates whether or not the CoAP code in header is "Get" request. + * + * @retval TRUE Message is a Get request. + * @retval FALSE Message is not a Get request. + * + */ + bool IsGetRequest(void) const { return GetCode() == kCodeGet; } + + /** + * This method indicates whether or not the CoAP code in header is "Post" request. + * + * @retval TRUE Message is a Post request. + * @retval FALSE Message is not a Post request. + * + */ + bool IsPostRequest(void) const { return GetCode() == kCodePost; } + + /** + * This method indicates whether or not the CoAP code in header is "Put" request. + * + * @retval TRUE Message is a Put request. + * @retval FALSE Message is not a Put request. + * + */ + bool IsPutRequest(void) const { return GetCode() == kCodePut; } + + /** + * This method indicates whether or not the CoAP code in header is "Delete" request. + * + * @retval TRUE Message is a Delete request. + * @retval FALSE Message is not a Delete request. + * + */ + bool IsDeleteRequest(void) const { return GetCode() == kCodeDelete; } /** * This method checks if a header is a response header. @@ -475,7 +633,7 @@ public: * @retval FALSE Message is not a response header. * */ - bool IsResponse(void) const { return (GetCode() >= OT_COAP_CODE_RESPONSE_MIN); } + bool IsResponse(void) const { return GetCode() >= OT_COAP_CODE_RESPONSE_MIN; } /** * This method checks if a header is a CON message header. @@ -484,7 +642,7 @@ public: * @retval FALSE Message is not is a CON message header. * */ - bool IsConfirmable(void) const { return (GetType() == OT_COAP_TYPE_CONFIRMABLE); } + bool IsConfirmable(void) const { return (GetType() == kTypeConfirmable); } /** * This method checks if a header is a NON message header. @@ -493,7 +651,7 @@ public: * @retval FALSE Message is not is a NON message header. * */ - bool IsNonConfirmable(void) const { return (GetType() == OT_COAP_TYPE_NON_CONFIRMABLE); } + bool IsNonConfirmable(void) const { return (GetType() == kTypeNonConfirmable); } /** * This method checks if a header is a ACK message header. @@ -502,7 +660,7 @@ public: * @retval FALSE Message is not is a ACK message header. * */ - bool IsAck(void) const { return (GetType() == OT_COAP_TYPE_ACKNOWLEDGMENT); } + bool IsAck(void) const { return (GetType() == kTypeAck); } /** * This method checks if a header is a RST message header. @@ -511,7 +669,27 @@ public: * @retval FALSE Message is not is a RST message header. * */ - bool IsReset(void) const { return (GetType() == OT_COAP_TYPE_RESET); } + bool IsReset(void) const { return (GetType() == kTypeReset); } + + /** + * This method indicates whether or not the header is a confirmable Put request (i.e, `kTypeConfirmable` with + * `kCodePost`). + * + * @retval TRUE Message is a confirmable Post request. + * @retval FALSE Message is not a confirmable Post request. + * + */ + bool IsConfirmablePostRequest(void) const; + + /** + * This method indicates whether or not the header is a non-confirmable Put request (i.e, `kTypeNonConfirmable` with + * `kCodePost`). + * + * @retval TRUE Message is a non-confirmable Post request. + * @retval FALSE Message is not a non-confirmable Post request. + * + */ + bool IsNonConfirmablePostRequest(void) const; /** * This method creates a copy of this CoAP message. @@ -568,35 +746,62 @@ public: const Message *GetNextCoapMessage(void) const { return static_cast(GetNext()); } private: - /** - * Protocol Constants (RFC 7252). - * - */ - enum + enum : uint8_t { - kOptionDeltaOffset = 4, ///< Delta Offset - kOptionDeltaMask = 0xf << kOptionDeltaOffset, ///< Delta Mask + /* + * Header field first byte (RFC 7252). + * + * 7 6 5 4 3 2 1 0 + * +-+-+-+-+-+-+-+-+ + * |Ver| T | TKL | (Version, Type and Token Length). + * +-+-+-+-+-+-+-+-+ + */ + kVersionOffset = 6, + kVersionMask = 0x3 << kVersionOffset, + kVersion1 = 1, + kTypeOffset = 4, + kTypeMask = 0x3 << kTypeOffset, + kTokenLengthOffset = 0, + kTokenLengthMask = 0xf << kTokenLengthOffset, - kMaxTokenLength = OT_COAP_MAX_TOKEN_LENGTH, + /* + * + * Option Format (RFC 7252). + * + * 7 6 5 4 3 2 1 0 + * +---------------+---------------+ + * | Option Delta | Option Length | 1 byte + * +---------------+---------------+ + * / Option Delta / 0-2 bytes + * \ (extended) \ + * +-------------------------------+ + * / Option Length / 0-2 bytes + * \ (extended) \ + * +-------------------------------+ + * / Option Value / 0 or more bytes + * +-------------------------------+ + * + */ - kVersionMask = 0xc0, ///< Version mask as specified (RFC 7252). - kVersionOffset = 6, ///< Version offset as specified (RFC 7252). + kOptionDeltaOffset = 4, + kOptionDeltaMask = 0xf << kOptionDeltaOffset, + kOptionLengthOffset = 0, + kOptionLengthMask = 0xf << kOptionLengthOffset, - kTypeMask = 0x30, ///< Type mask as specified (RFC 7252). + kMaxOptionHeaderSize = 5, + kOption1ByteExtension = 13, // Indicates a 1 byte extension (RFC 7252). + kOption2ByteExtension = 14, // Indicates a 2 byte extension (RFC 7252). - kTokenLengthMask = 0x0f, ///< Token Length mask as specified (RFC 7252). - kTokenLengthOffset = 0, ///< Token Length offset as specified (RFC 7252). - kTokenOffset = 4, ///< Token offset as specified (RFC 7252). + kHelpDataAlignment = sizeof(uint16_t), ///< Alignment of help data. + }; - kMaxOptionHeaderSize = 5, ///< Maximum size of an Option header - - kOption1ByteExtension = 13, ///< Indicates a 1 byte extension (RFC 7252). - kOption2ByteExtension = 14, ///< Indicates a 1 byte extension (RFC 7252). + enum : uint16_t + { + kMinHeaderLength = 4, + kMaxHeaderLength = 512, kOption1ByteExtensionOffset = 13, ///< Delta/Length offset as specified (RFC 7252). kOption2ByteExtensionOffset = 269, ///< Delta/Length offset as specified (RFC 7252). - - kHelpDataAlignment = sizeof(uint16_t), ///< Alignment of help data. }; enum @@ -618,10 +823,10 @@ private: OT_TOOL_PACKED_BEGIN struct Header { - uint8_t mVersionTypeToken; ///< The CoAP Version, Type, and Token Length - uint8_t mCode; ///< The CoAP Code - uint16_t mMessageId; ///< The CoAP Message ID - uint8_t mToken[OT_COAP_MAX_TOKEN_LENGTH]; ///< The CoAP Token + uint8_t mVersionTypeToken; ///< The CoAP Version, Type, and Token Length + uint8_t mCode; ///< The CoAP Code + uint16_t mMessageId; ///< The CoAP Message ID + uint8_t mToken[kMaxTokenLength]; ///< The CoAP Token } OT_TOOL_PACKED_END; /** @@ -645,6 +850,14 @@ private: } HelpData &GetHelpData(void) { return const_cast(static_cast(this)->GetHelpData()); } + + uint8_t *GetToken(void) { return GetHelpData().mHeader.mToken; } + + void SetTokenLength(uint8_t aTokenLength) + { + GetHelpData().mHeader.mVersionTypeToken &= ~kTokenLengthMask; + GetHelpData().mHeader.mVersionTypeToken |= ((aTokenLength << kTokenLengthOffset) & kTokenLengthMask); + } }; /** diff --git a/src/core/meshcop/announce_begin_client.cpp b/src/core/meshcop/announce_begin_client.cpp index 6ce0a601e..489a7c062 100644 --- a/src/core/meshcop/announce_begin_client.cpp +++ b/src/core/meshcop/announce_begin_client.cpp @@ -42,7 +42,7 @@ #include "meshcop/meshcop.hpp" #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD @@ -66,9 +66,7 @@ otError AnnounceBeginClient::SendRequest(uint32_t aChannelMask, VerifyOrExit(Get().IsActive(), error = OT_ERROR_INVALID_STATE); VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = - message->Init(aAddress.IsMulticast() ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, - OT_COAP_CODE_POST, OT_URI_PATH_ANNOUNCE_BEGIN)); + SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kAnnounceBegin)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerSessionId, diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index b1bc33e0f..a43c84ad4 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -42,7 +42,7 @@ #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE @@ -67,7 +67,7 @@ public: , mPetition(aPetition) , mSeparate(aSeparate) , mTokenLength(aMessage.GetTokenLength()) - , mType(aMessage.GetType() >> Coap::Message::kTypeOffset) + , mType(aMessage.GetType()) { memcpy(mToken, aMessage.GetToken(), mTokenLength); } @@ -107,15 +107,15 @@ public: * @retval OT_ERROR_NO_BUFS Insufficient message buffers available to generate the response header. * */ - otError ToHeader(Coap::Message &aMessage, Coap::Message::Code aCode) + otError ToHeader(Coap::Message &aMessage, uint8_t aCode) { - if (mType == (OT_COAP_TYPE_NON_CONFIRMABLE >> Coap::Message::kTypeOffset) || mSeparate) + if ((mType == Coap::kTypeNonConfirmable) || mSeparate) { - aMessage.Init(OT_COAP_TYPE_NON_CONFIRMABLE, aCode); + aMessage.Init(Coap::kTypeNonConfirmable, static_cast(aCode)); } else { - aMessage.Init(OT_COAP_TYPE_ACKNOWLEDGMENT, aCode); + aMessage.Init(Coap::kTypeAck, static_cast(aCode)); } if (!mSeparate) @@ -129,8 +129,9 @@ public: private: enum { - kMaxTokenLength = OT_COAP_MAX_TOKEN_LENGTH, ///< The max token size + kMaxTokenLength = Coap::Message::kMaxTokenLength, ///< The max token size }; + BorderAgent &mBorderAgent; uint16_t mMessageId; ///< The CoAP Message ID of the original request. bool mPetition : 1; ///< Whether the forwarding request is leader petition. @@ -147,15 +148,16 @@ static Coap::Message::Code CoapCodeFromError(otError aError) switch (aError) { case OT_ERROR_NONE: - code = OT_COAP_CODE_CHANGED; + code = Coap::kCodeChanged; break; case OT_ERROR_PARSE: - code = OT_COAP_CODE_BAD_REQUEST; + code = Coap::kCodeBadRequest; + ; break; default: - code = OT_COAP_CODE_INTERNAL_ERROR; + code = Coap::kCodeInternalError; break; } @@ -195,11 +197,11 @@ static void SendErrorMessage(Coap::CoapSecure & aCoapSecure, if (aRequest.IsNonConfirmable() || aSeparate) { - message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, CoapCodeFromError(aError)); + message->Init(Coap::kTypeNonConfirmable, CoapCodeFromError(aError)); } else { - message->Init(OT_COAP_TYPE_ACKNOWLEDGMENT, CoapCodeFromError(aError)); + message->Init(Coap::kTypeAck, CoapCodeFromError(aError)); } if (!aSeparate) @@ -240,7 +242,7 @@ void BorderAgent::HandleCoapResponse(void * aContext, SuccessOrExit(error = aResult); VerifyOrExit((message = NewMeshCoPMessage(instance.Get())) != nullptr, error = OT_ERROR_NO_BUFS); - if (forwardContext.IsPetition() && response->GetCode() == OT_COAP_CODE_CHANGED) + if (forwardContext.IsPetition() && response->GetCode() == Coap::kCodeChanged) { uint8_t state; @@ -292,7 +294,7 @@ void BorderAgent::HandleRequest<&BorderAgent::mCommissionerPetition>(void * { IgnoreError(static_cast(aContext)->ForwardToLeader( *static_cast(aMessage), *static_cast(aMessageInfo), - OT_URI_PATH_LEADER_PETITION, true, true)); + UriPath::kLeaderPetition, true, true)); } template <> @@ -333,21 +335,21 @@ void BorderAgent::HandleRequest<&BorderAgent::mProxyTransmit>(void * BorderAgent::BorderAgent(Instance &aInstance) : InstanceLocator(aInstance) - , mCommissionerPetition(OT_URI_PATH_COMMISSIONER_PETITION, + , mCommissionerPetition(UriPath::kCommissionerPetition, BorderAgent::HandleRequest<&BorderAgent::mCommissionerPetition>, this) - , mCommissionerKeepAlive(OT_URI_PATH_COMMISSIONER_KEEP_ALIVE, + , mCommissionerKeepAlive(UriPath::kCommissionerKeepAlive, BorderAgent::HandleRequest<&BorderAgent::mCommissionerKeepAlive>, this) - , mRelayTransmit(OT_URI_PATH_RELAY_TX, BorderAgent::HandleRequest<&BorderAgent::mRelayTransmit>, this) - , mRelayReceive(OT_URI_PATH_RELAY_RX, BorderAgent::HandleRequest<&BorderAgent::mRelayReceive>, this) - , mCommissionerGet(OT_URI_PATH_COMMISSIONER_GET, BorderAgent::HandleRequest<&BorderAgent::mCommissionerGet>, this) - , mCommissionerSet(OT_URI_PATH_COMMISSIONER_SET, BorderAgent::HandleRequest<&BorderAgent::mCommissionerSet>, this) - , mActiveGet(OT_URI_PATH_ACTIVE_GET, BorderAgent::HandleRequest<&BorderAgent::mActiveGet>, this) - , mActiveSet(OT_URI_PATH_ACTIVE_SET, BorderAgent::HandleRequest<&BorderAgent::mActiveSet>, this) - , mPendingGet(OT_URI_PATH_PENDING_GET, BorderAgent::HandleRequest<&BorderAgent::mPendingGet>, this) - , mPendingSet(OT_URI_PATH_PENDING_SET, BorderAgent::HandleRequest<&BorderAgent::mPendingSet>, this) - , mProxyTransmit(OT_URI_PATH_PROXY_TX, BorderAgent::HandleRequest<&BorderAgent::mProxyTransmit>, this) + , mRelayTransmit(UriPath::kRelayTx, BorderAgent::HandleRequest<&BorderAgent::mRelayTransmit>, this) + , mRelayReceive(UriPath::kRelayRx, BorderAgent::HandleRequest<&BorderAgent::mRelayReceive>, this) + , mCommissionerGet(UriPath::kCommissionerGet, BorderAgent::HandleRequest<&BorderAgent::mCommissionerGet>, this) + , mCommissionerSet(UriPath::kCommissionerSet, BorderAgent::HandleRequest<&BorderAgent::mCommissionerSet>, this) + , mActiveGet(UriPath::kActiveGet, BorderAgent::HandleRequest<&BorderAgent::mActiveGet>, this) + , mActiveSet(UriPath::kActiveSet, BorderAgent::HandleRequest<&BorderAgent::mActiveSet>, this) + , mPendingGet(UriPath::kPendingGet, BorderAgent::HandleRequest<&BorderAgent::mPendingGet>, this) + , mPendingSet(UriPath::kPendingSet, BorderAgent::HandleRequest<&BorderAgent::mPendingSet>, this) + , mProxyTransmit(UriPath::kProxyTx, BorderAgent::HandleRequest<&BorderAgent::mProxyTransmit>, this) , mUdpReceiver(BorderAgent::HandleUdpReceive, this) , mTimer(aInstance, HandleTimeout, this) , mState(OT_BORDER_AGENT_STATE_STOPPED) @@ -428,8 +430,8 @@ bool BorderAgent::HandleUdpReceive(const Message &aMessage, const Ip6::MessageIn VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_PROXY_RX)); + message->InitAsNonConfirmablePost(); + SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kProxyRx)); SuccessOrExit(error = message->SetPayloadMarker()); { @@ -453,12 +455,12 @@ bool BorderAgent::HandleUdpReceive(const Message &aMessage, const Ip6::MessageIn SuccessOrExit(error = Get().SendMessage(*message, Get().GetMessageInfo())); - otLogInfoMeshCoP("Sent to commissioner on %s", OT_URI_PATH_PROXY_RX); + otLogInfoMeshCoP("Sent to commissioner on %s", UriPath::kProxyRx); exit: if (message != nullptr && error != OT_ERROR_NONE) { - otLogWarnMeshCoP("Failed notify commissioner on %s", OT_URI_PATH_PROXY_RX); + otLogWarnMeshCoP("Failed notify commissioner on %s", UriPath::kProxyRx); message->Free(); } @@ -470,11 +472,11 @@ void BorderAgent::HandleRelayReceive(const Coap::Message &aMessage) Coap::Message *message = nullptr; otError error; - VerifyOrExit(aMessage.IsNonConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); + VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), error = OT_ERROR_DROP); VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_RELAY_RX)); + message->InitAsNonConfirmablePost(); + SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kRelayRx)); if (aMessage.GetLength() > aMessage.GetOffset()) { @@ -482,7 +484,7 @@ void BorderAgent::HandleRelayReceive(const Coap::Message &aMessage) } SuccessOrExit(error = ForwardToCommissioner(*message, aMessage)); - otLogInfoMeshCoP("Sent to commissioner on %s", OT_URI_PATH_RELAY_RX); + otLogInfoMeshCoP("Sent to commissioner on %s", UriPath::kRelayRx); exit: if (error != OT_ERROR_NONE && message != nullptr) @@ -518,7 +520,7 @@ void BorderAgent::HandleKeepAlive(const Coap::Message &aMessage, const Ip6::Mess { otError error; - error = ForwardToLeader(aMessage, aMessageInfo, OT_URI_PATH_LEADER_KEEP_ALIVE, false, true); + error = ForwardToLeader(aMessage, aMessageInfo, UriPath::kLeaderKeepAlive, false, true); if (error == OT_ERROR_NONE) { @@ -534,13 +536,13 @@ void BorderAgent::HandleRelayTransmit(const Coap::Message &aMessage) Ip6::MessageInfo messageInfo; uint16_t offset = 0; - VerifyOrExit(aMessage.IsNonConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), OT_NOOP); SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kJoinerRouterLocator, joinerRouterRloc)); VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_RELAY_TX)); + SuccessOrExit(error = message->InitAsNonConfirmablePost(UriPath::kRelayTx)); SuccessOrExit(error = message->SetPayloadMarker()); offset = message->GetLength(); @@ -555,12 +557,12 @@ void BorderAgent::HandleRelayTransmit(const Coap::Message &aMessage) SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); - otLogInfoMeshCoP("Sent to joiner router request on %s", OT_URI_PATH_RELAY_TX); + otLogInfoMeshCoP("Sent to joiner router request on %s", UriPath::kRelayTx); exit: if (error != OT_ERROR_NONE) { - otLogWarnMeshCoP("Failed to sent to joiner router request " OT_URI_PATH_RELAY_TX " %s", + otLogWarnMeshCoP("Failed to sent to joiner router request %s: %s", UriPath::kRelayTx, otThreadErrorToString(error)); if (message != nullptr) { @@ -593,7 +595,7 @@ otError BorderAgent::ForwardToLeader(const Coap::Message & aMessage, forwardContext = new (forwardContext) ForwardContext(*this, aMessage, aPetition, aSeparate); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, aPath)); + SuccessOrExit(error = message->InitAsConfirmablePost(aPath)); // Payload of c/cg may be empty if (aMessage.GetLength() - aMessage.GetOffset() > 0) diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index cd592d8af..c5d8af8e5 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -48,7 +48,7 @@ #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE @@ -64,9 +64,9 @@ Commissioner::Commissioner(Instance &aInstance) , mTransmitAttempts(0) , mJoinerExpirationTimer(aInstance, HandleJoinerExpirationTimer, this) , mTimer(aInstance, HandleTimer, this) - , mRelayReceive(OT_URI_PATH_RELAY_RX, &Commissioner::HandleRelayReceive, this) - , mDatasetChanged(OT_URI_PATH_DATASET_CHANGED, &Commissioner::HandleDatasetChanged, this) - , mJoinerFinalize(OT_URI_PATH_JOINER_FINALIZE, &Commissioner::HandleJoinerFinalize, this) + , mRelayReceive(UriPath::kRelayRx, &Commissioner::HandleRelayReceive, this) + , mDatasetChanged(UriPath::kDatasetChanged, &Commissioner::HandleDatasetChanged, this) + , mJoinerFinalize(UriPath::kJoinerFinalize, &Commissioner::HandleJoinerFinalize, this) , mAnnounceBegin(aInstance) , mEnergyScan(aInstance) , mPanIdQuery(aInstance) @@ -698,7 +698,7 @@ otError Commissioner::SendMgmtCommissionerGetRequest(const uint8_t *aTlvs, uint8 VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_COMMISSIONER_GET)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kCommissionerGet)); if (aLength > 0) { @@ -746,7 +746,7 @@ void Commissioner::HandleMgmtCommissionerGetResponse(Coap::Message * aMe { OT_UNUSED_VARIABLE(aMessageInfo); - VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == OT_COAP_CODE_CHANGED, OT_NOOP); + VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged, OT_NOOP); otLogInfoMeshCoP("received MGMT_COMMISSIONER_GET response"); exit: @@ -763,7 +763,7 @@ otError Commissioner::SendMgmtCommissionerSetRequest(const otCommissioningDatase VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_COMMISSIONER_SET)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kCommissionerSet)); SuccessOrExit(error = message->SetPayloadMarker()); if (aDataset.mIsLocatorSet) @@ -832,7 +832,7 @@ void Commissioner::HandleMgmtCommissionerSetResponse(Coap::Message * aMe { OT_UNUSED_VARIABLE(aMessageInfo); - VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == OT_COAP_CODE_CHANGED, OT_NOOP); + VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged, OT_NOOP); otLogInfoMeshCoP("received MGMT_COMMISSIONER_SET response"); exit: @@ -850,7 +850,7 @@ otError Commissioner::SendPetition(void) VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_LEADER_PETITION)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kLeaderPetition)); SuccessOrExit(error = message->SetPayloadMarker()); commissionerId.Init(); @@ -895,7 +895,7 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Message * aMessage bool retransmit = false; VerifyOrExit(mState != OT_COMMISSIONER_STATE_ACTIVE, OT_NOOP); - VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == OT_COAP_CODE_CHANGED, + VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged, retransmit = (mState == OT_COMMISSIONER_STATE_PETITION)); otLogInfoMeshCoP("received Leader Petition response"); @@ -950,7 +950,7 @@ void Commissioner::SendKeepAlive(uint16_t aSessionId) VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_LEADER_KEEP_ALIVE)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kLeaderKeepAlive)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit( @@ -998,7 +998,7 @@ void Commissioner::HandleLeaderKeepAliveResponse(Coap::Message * aMessag uint8_t state; VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, OT_NOOP); - VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == OT_COAP_CODE_CHANGED, + VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged, IgnoreError(Stop(/* aResign */ false))); otLogInfoMeshCoP("received Leader keep-alive response"); @@ -1032,7 +1032,7 @@ void Commissioner::HandleRelayReceive(Coap::Message &aMessage, const Ip6::Messag VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE); - VerifyOrExit(aMessage.IsNonConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), OT_NOOP); SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kJoinerUdpPort, joinerPort)); SuccessOrExit(error = Tlv::FindTlv(aMessage, Tlv::kJoinerIid, &joinerIid, sizeof(joinerIid))); @@ -1089,7 +1089,7 @@ void Commissioner::HandleDatasetChanged(void *aContext, otMessage *aMessage, con void Commissioner::HandleDatasetChanged(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) { - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), OT_NOOP); otLogInfoMeshCoP("received dataset changed"); @@ -1205,8 +1205,8 @@ otError Commissioner::SendRelayTransmit(Message &aMessage, const Ip6::MessageInf VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_RELAY_TX)); + message->InitAsNonConfirmablePost(); + SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kRelayTx)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kJoinerUdpPort, mJoinerPort)); diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 820cadfb5..8a4a0b781 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -44,7 +44,7 @@ #include "radio/radio.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { namespace MeshCoP { @@ -280,7 +280,7 @@ void DatasetManager::SendSet(void) VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, mUriSet)); + SuccessOrExit(error = message->InitAsConfirmablePost(mUriSet)); SuccessOrExit(error = message->SetPayloadMarker()); IgnoreError(mLocal.Read(dataset)); @@ -365,7 +365,7 @@ void DatasetManager::HandleGet(const Coap::Message &aMessage, const Ip6::Message } // MGMT_PENDING_GET.rsp must include Delay Timer TLV (Thread 1.1.1 Section 8.7.5.4) - VerifyOrExit(length > 0 && strcmp(mUriGet, OT_URI_PATH_PENDING_GET) == 0, OT_NOOP); + VerifyOrExit(length > 0 && strcmp(mUriGet, UriPath::kPendingGet) == 0, OT_NOOP); for (uint8_t i = 0; i < length; i++) { @@ -451,7 +451,7 @@ otError DatasetManager::SendSetRequest(const otOperationalDataset &aDataset, con VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, mUriSet)); + SuccessOrExit(error = message->InitAsConfirmablePost(mUriSet)); SuccessOrExit(error = message->SetPayloadMarker()); #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD @@ -669,7 +669,7 @@ otError DatasetManager::SendGetRequest(const otOperationalDatasetComponents &aDa VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, mUriGet)); + SuccessOrExit(error = message->InitAsConfirmablePost(mUriGet)); if (aLength + length > 0) { @@ -719,14 +719,10 @@ exit: } ActiveDataset::ActiveDataset(Instance &aInstance) - : DatasetManager(aInstance, - Dataset::kActive, - OT_URI_PATH_ACTIVE_GET, - OT_URI_PATH_ACTIVE_SET, - ActiveDataset::HandleTimer) - , mResourceGet(OT_URI_PATH_ACTIVE_GET, &ActiveDataset::HandleGet, this) + : DatasetManager(aInstance, Dataset::kActive, UriPath::kActiveGet, UriPath::kActiveSet, ActiveDataset::HandleTimer) + , mResourceGet(UriPath::kActiveGet, &ActiveDataset::HandleGet, this) #if OPENTHREAD_FTD - , mResourceSet(OT_URI_PATH_ACTIVE_SET, &ActiveDataset::HandleSet, this) + , mResourceSet(UriPath::kActiveSet, &ActiveDataset::HandleSet, this) #endif { Get().AddResource(mResourceGet); @@ -769,13 +765,13 @@ void ActiveDataset::HandleTimer(Timer &aTimer) PendingDataset::PendingDataset(Instance &aInstance) : DatasetManager(aInstance, Dataset::kPending, - OT_URI_PATH_PENDING_GET, - OT_URI_PATH_PENDING_SET, + UriPath::kPendingGet, + UriPath::kPendingSet, PendingDataset::HandleTimer) , mDelayTimer(aInstance, PendingDataset::HandleDelayTimer, this) - , mResourceGet(OT_URI_PATH_PENDING_GET, &PendingDataset::HandleGet, this) + , mResourceGet(UriPath::kPendingGet, &PendingDataset::HandleGet, this) #if OPENTHREAD_FTD - , mResourceSet(OT_URI_PATH_PENDING_SET, &PendingDataset::HandleSet, this) + , mResourceSet(UriPath::kPendingSet, &PendingDataset::HandleSet, this) #endif { Get().AddResource(mResourceGet); diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index aad142571..c23156f13 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -52,7 +52,7 @@ #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { namespace MeshCoP { @@ -105,7 +105,7 @@ otError DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInf // verify that does not overflow dataset buffer VerifyOrExit((offset - aMessage.GetOffset()) <= Dataset::kMaxSize, OT_NOOP); - type = (strcmp(mUriSet, OT_URI_PATH_ACTIVE_SET) == 0 ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp); + type = (strcmp(mUriSet, UriPath::kActiveSet) == 0 ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp); if (Tlv::FindTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp) != OT_ERROR_NONE) { diff --git a/src/core/meshcop/energy_scan_client.cpp b/src/core/meshcop/energy_scan_client.cpp index 7b4b7b9e0..06384983f 100644 --- a/src/core/meshcop/energy_scan_client.cpp +++ b/src/core/meshcop/energy_scan_client.cpp @@ -43,7 +43,7 @@ #include "meshcop/meshcop.hpp" #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD @@ -53,7 +53,7 @@ EnergyScanClient::EnergyScanClient(Instance &aInstance) : InstanceLocator(aInstance) , mCallback(nullptr) , mContext(nullptr) - , mEnergyScan(OT_URI_PATH_ENERGY_REPORT, &EnergyScanClient::HandleReport, this) + , mEnergyScan(UriPath::kEnergyReport, &EnergyScanClient::HandleReport, this) { Get().AddResource(mEnergyScan); } @@ -74,9 +74,7 @@ otError EnergyScanClient::SendQuery(uint32_t aChannelM VerifyOrExit(Get().IsActive(), error = OT_ERROR_INVALID_STATE); VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = - message->Init(aAddress.IsMulticast() ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, - OT_COAP_CODE_POST, OT_URI_PATH_ENERGY_SCAN)); + SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kEnergyScan)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerSessionId, @@ -127,7 +125,7 @@ void EnergyScanClient::HandleReport(Coap::Message &aMessage, const Ip6::MessageI uint8_t list[OPENTHREAD_CONFIG_TMF_ENERGY_SCAN_MAX_RESULTS]; } OT_TOOL_PACKED_END energyList; - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), OT_NOOP); otLogInfoMeshCoP("received energy scan report"); diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index d5928b035..c38151732 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -45,7 +45,7 @@ #include "meshcop/meshcop.hpp" #include "radio/radio.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #include "utils/otns.hpp" #if OPENTHREAD_CONFIG_JOINER_ENABLE @@ -65,7 +65,7 @@ Joiner::Joiner(Instance &aInstance) , mJoinerRouterIndex(0) , mFinalizeMessage(nullptr) , mTimer(aInstance, Joiner::HandleTimer, this) - , mJoinerEntrust(OT_URI_PATH_JOINER_ENTRUST, &Joiner::HandleJoinerEntrust, this) + , mJoinerEntrust(UriPath::kJoinerEntrust, &Joiner::HandleJoinerEntrust, this) { SetIdFromIeeeEui64(); mDiscerner.Clear(); @@ -443,8 +443,8 @@ otError Joiner::PrepareJoinerFinalizeMessage(const char *aProvisioningUrl, VerifyOrExit((mFinalizeMessage = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - mFinalizeMessage->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = mFinalizeMessage->AppendUriPathOptions(OT_URI_PATH_JOINER_FINALIZE)); + mFinalizeMessage->InitAsConfirmablePost(); + SuccessOrExit(error = mFinalizeMessage->AppendUriPathOptions(UriPath::kJoinerFinalize)); SuccessOrExit(error = mFinalizeMessage->SetPayloadMarker()); mFinalizeMessage->SetOffset(mFinalizeMessage->GetLength()); @@ -540,7 +540,7 @@ void Joiner::HandleJoinerFinalizeResponse(Coap::Message & aMessage, uint8_t state; VerifyOrExit(mState == OT_JOINER_STATE_CONNECTED && aResult == OT_ERROR_NONE && aMessage.IsAck() && - aMessage.GetCode() == OT_COAP_CODE_CHANGED, + aMessage.GetCode() == Coap::kCodeChanged, OT_NOOP); SuccessOrExit(Tlv::FindUint8Tlv(aMessage, Tlv::kState, state)); @@ -570,9 +570,7 @@ void Joiner::HandleJoinerEntrust(Coap::Message &aMessage, const Ip6::MessageInfo otError error; otOperationalDataset dataset; - VerifyOrExit(mState == OT_JOINER_STATE_ENTRUST && aMessage.IsConfirmable() && - aMessage.GetCode() == OT_COAP_CODE_POST, - error = OT_ERROR_DROP); + VerifyOrExit(mState == OT_JOINER_STATE_ENTRUST && aMessage.IsConfirmablePostRequest(), error = OT_ERROR_DROP); otLogInfoMeshCoP("Joiner received entrust"); otLogCertMeshCoP("[THCI] direction=recv | type=JOIN_ENT.ntf"); diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index c6b2a824f..281d58ad4 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -46,7 +46,7 @@ #include "meshcop/meshcop_tlvs.hpp" #include "thread/mle.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" using ot::Encoding::BigEndian::HostSwap16; @@ -56,7 +56,7 @@ namespace MeshCoP { JoinerRouter::JoinerRouter(Instance &aInstance) : InstanceLocator(aInstance) , mSocket(aInstance) - , mRelayTransmit(OT_URI_PATH_RELAY_TX, &JoinerRouter::HandleRelayTransmit, this) + , mRelayTransmit(UriPath::kRelayTx, &JoinerRouter::HandleRelayTransmit, this) , mTimer(aInstance, JoinerRouter::HandleTimer, this) , mJoinerUdpPort(0) , mIsJoinerPortConfigured(false) @@ -145,7 +145,7 @@ void JoinerRouter::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &a VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_RELAY_RX)); + SuccessOrExit(error = message->InitAsNonConfirmablePost(UriPath::kRelayRx)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kJoinerUdpPort, aMessageInfo.GetPeerPort())); @@ -197,7 +197,7 @@ void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::Messa Message::Settings settings(Message::kNoLinkSecurity, Message::kPriorityNet); Ip6::MessageInfo messageInfo; - VerifyOrExit(aMessage.IsNonConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); + VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), error = OT_ERROR_DROP); otLogInfoMeshCoP("Received relay transmit"); @@ -343,8 +343,8 @@ Coap::Message *JoinerRouter::PrepareJoinerEntrustMessage(void) VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_JOINER_ENTRUST)); + message->InitAsConfirmablePost(); + SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kJoinerEntrust)); SuccessOrExit(error = message->SetPayloadMarker()); message->SetSubType(Message::kSubTypeJoinerEntrust); @@ -440,7 +440,7 @@ void JoinerRouter::HandleJoinerEntrustResponse(Coap::Message * aMessage, VerifyOrExit(aResult == OT_ERROR_NONE && aMessage != nullptr, OT_NOOP); - VerifyOrExit(aMessage->GetCode() == OT_COAP_CODE_CHANGED, OT_NOOP); + VerifyOrExit(aMessage->GetCode() == Coap::kCodeChanged, OT_NOOP); otLogInfoMeshCoP("Receive joiner entrust response"); otLogCertMeshCoP("[THCI] direction=recv | type=JOIN_ENT.rsp"); diff --git a/src/core/meshcop/meshcop_leader.cpp b/src/core/meshcop/meshcop_leader.cpp index f85c69d35..113d5b9c6 100644 --- a/src/core/meshcop/meshcop_leader.cpp +++ b/src/core/meshcop/meshcop_leader.cpp @@ -47,15 +47,15 @@ #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { namespace MeshCoP { Leader::Leader(Instance &aInstance) : InstanceLocator(aInstance) - , mPetition(OT_URI_PATH_LEADER_PETITION, Leader::HandlePetition, this) - , mKeepAlive(OT_URI_PATH_LEADER_KEEP_ALIVE, Leader::HandleKeepAlive, this) + , mPetition(UriPath::kLeaderPetition, Leader::HandlePetition, this) + , mKeepAlive(UriPath::kLeaderKeepAlive, Leader::HandleKeepAlive, this) , mTimer(aInstance, HandleTimer, this) , mDelayTimerMinimal(DelayTimerTlv::kDelayTimerMinimal) , mSessionId(Random::NonCrypto::GetUint16()) @@ -251,7 +251,7 @@ void Leader::SendDatasetChanged(const Ip6::Address &aAddress) VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DATASET_CHANGED)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kDatasetChanged)); messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); diff --git a/src/core/meshcop/panid_query_client.cpp b/src/core/meshcop/panid_query_client.cpp index ef2e987df..836e27e60 100644 --- a/src/core/meshcop/panid_query_client.cpp +++ b/src/core/meshcop/panid_query_client.cpp @@ -42,7 +42,7 @@ #include "meshcop/meshcop.hpp" #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD @@ -52,7 +52,7 @@ PanIdQueryClient::PanIdQueryClient(Instance &aInstance) : InstanceLocator(aInstance) , mCallback(nullptr) , mContext(nullptr) - , mPanIdQuery(OT_URI_PATH_PANID_CONFLICT, &PanIdQueryClient::HandleConflict, this) + , mPanIdQuery(UriPath::kPanIdConflict, &PanIdQueryClient::HandleConflict, this) { Get().AddResource(mPanIdQuery); } @@ -71,9 +71,7 @@ otError PanIdQueryClient::SendQuery(uint16_t aPanId, VerifyOrExit(Get().IsActive(), error = OT_ERROR_INVALID_STATE); VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = - message->Init(aAddress.IsMulticast() ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, - OT_COAP_CODE_POST, OT_URI_PATH_PANID_QUERY)); + SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kPanIdQuery)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerSessionId, @@ -117,7 +115,7 @@ void PanIdQueryClient::HandleConflict(Coap::Message &aMessage, const Ip6::Messag Ip6::MessageInfo responseInfo(aMessageInfo); uint32_t mask; - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), OT_NOOP); otLogInfoMeshCoP("received panid conflict"); diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index 229b70670..87909384e 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -47,7 +47,7 @@ #include "thread/mesh_forwarder.hpp" #include "thread/mle_router.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" using ot::Encoding::BigEndian::HostSwap16; @@ -55,9 +55,9 @@ namespace ot { AddressResolver::AddressResolver(Instance &aInstance) : InstanceLocator(aInstance) - , mAddressError(OT_URI_PATH_ADDRESS_ERROR, &AddressResolver::HandleAddressError, this) - , mAddressQuery(OT_URI_PATH_ADDRESS_QUERY, &AddressResolver::HandleAddressQuery, this) - , mAddressNotification(OT_URI_PATH_ADDRESS_NOTIFY, &AddressResolver::HandleAddressNotification, this) + , mAddressError(UriPath::kAddressError, &AddressResolver::HandleAddressError, this) + , mAddressQuery(UriPath::kAddressQuery, &AddressResolver::HandleAddressQuery, this) + , mAddressNotification(UriPath::kAddressNotify, &AddressResolver::HandleAddressNotification, this) , mCacheEntryPool(aInstance) , mCachedList() , mSnoopedList() @@ -525,8 +525,8 @@ otError AddressResolver::SendAddressQuery(const Ip6::Address &aEid) VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_ADDRESS_QUERY)); + message->InitAsNonConfirmablePost(); + SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kAddressQuery)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aEid, sizeof(aEid))); @@ -568,7 +568,7 @@ void AddressResolver::HandleAddressNotification(Coap::Message &aMessage, const I CacheEntry * entry; CacheEntry * prev; - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), OT_NOOP); SuccessOrExit(Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); SuccessOrExit(Tlv::FindTlv(aMessage, ThreadTlv::kMeshLocalEid, &meshLocalIid, sizeof(meshLocalIid))); @@ -636,8 +636,8 @@ void AddressResolver::SendAddressError(const Ip6::Address & aTarget, VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(aDestination == nullptr ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_ADDRESS_ERROR)); + message->Init(aDestination == nullptr ? Coap::kTypeNonConfirmable : Coap::kTypeConfirmable, Coap::kCodePost); + SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kAddressError)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aTarget, sizeof(aTarget))); @@ -686,7 +686,7 @@ void AddressResolver::HandleAddressError(Coap::Message &aMessage, const Ip6::Mes Mac::ExtAddress extAddr; Ip6::Address destination; - VerifyOrExit(aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); + VerifyOrExit(aMessage.IsPostRequest(), error = OT_ERROR_DROP); otLogInfoArp("Received address error notification"); @@ -755,7 +755,7 @@ void AddressResolver::HandleAddressQuery(Coap::Message &aMessage, const Ip6::Mes Ip6::Address target; uint32_t lastTransactionTime; - VerifyOrExit(aMessage.IsNonConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), OT_NOOP); SuccessOrExit(Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); @@ -799,8 +799,8 @@ void AddressResolver::SendAddressQueryResponse(const Ip6::Address & a VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); - SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_ADDRESS_NOTIFY)); + message->InitAsConfirmablePost(); + SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kAddressNotify)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aTarget, sizeof(aTarget))); diff --git a/src/core/thread/announce_begin_server.cpp b/src/core/thread/announce_begin_server.cpp index 5f3872321..2ef3cd832 100644 --- a/src/core/thread/announce_begin_server.cpp +++ b/src/core/thread/announce_begin_server.cpp @@ -43,13 +43,13 @@ #include "common/logging.hpp" #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { AnnounceBeginServer::AnnounceBeginServer(Instance &aInstance) : AnnounceSenderBase(aInstance, AnnounceBeginServer::HandleTimer) - , mAnnounceBegin(OT_URI_PATH_ANNOUNCE_BEGIN, &AnnounceBeginServer::HandleRequest, this) + , mAnnounceBegin(UriPath::kAnnounceBegin, &AnnounceBeginServer::HandleRequest, this) { Get().AddResource(mAnnounceBegin); } @@ -72,7 +72,7 @@ void AnnounceBeginServer::HandleRequest(Coap::Message &aMessage, const Ip6::Mess uint16_t period; Ip6::MessageInfo responseInfo(aMessageInfo); - VerifyOrExit(aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsPostRequest(), OT_NOOP); VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0, OT_NOOP); SuccessOrExit(Tlv::FindUint8Tlv(aMessage, MeshCoP::Tlv::kCount, count)); diff --git a/src/core/thread/dua_manager.cpp b/src/core/thread/dua_manager.cpp index 43241d37b..e1153aba7 100644 --- a/src/core/thread/dua_manager.cpp +++ b/src/core/thread/dua_manager.cpp @@ -44,7 +44,7 @@ #include "thread/mle_types.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #include "utils/slaac_address.hpp" namespace ot { @@ -52,7 +52,7 @@ namespace ot { DuaManager::DuaManager(Instance &aInstance) : InstanceLocator(aInstance) , mRegistrationTask(aInstance, DuaManager::HandleRegistrationTask, this) - , mDuaNotification(OT_URI_PATH_DUA_REGISTRATION_NOTIFY, &DuaManager::HandleDuaNotification, this) + , mDuaNotification(UriPath::kDuaRegistrationNotify, &DuaManager::HandleDuaNotification, this) , mIsDuaPending(false) #if OPENTHREAD_CONFIG_DUA_ENABLE , mDuaState(kNotExist) @@ -424,8 +424,7 @@ void DuaManager::PerformNextRegistration(void) // Prepare DUA.req VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = - message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DUA_REGISTRATION_REQUEST)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kDuaRegistrationRequest)); SuccessOrExit(error = message->SetPayloadMarker()); #if OPENTHREAD_CONFIG_DUA_ENABLE @@ -529,7 +528,7 @@ void DuaManager::HandleDuaResponse(Coap::Message &aMessage, const Ip6::MessageIn ExitNow(error = aResult); } - VerifyOrExit(aResult == OT_ERROR_NONE && aMessage.GetCode() == OT_COAP_CODE_CHANGED, error = OT_ERROR_PARSE); + VerifyOrExit(aResult == OT_ERROR_NONE && aMessage.GetCode() == Coap::kCodeChanged, error = OT_ERROR_PARSE); error = ProcessDuaResponse(aMessage); @@ -549,7 +548,7 @@ void DuaManager::HandleDuaNotification(Coap::Message &aMessage, const Ip6::Messa OT_UNUSED_VARIABLE(error); - VerifyOrExit(aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_PARSE); + VerifyOrExit(aMessage.IsPostRequest(), error = OT_ERROR_PARSE); if (aMessage.IsConfirmable() && Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) { @@ -658,8 +657,7 @@ void DuaManager::SendAddressNotification(Ip6::Address & aAddress, VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = - message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DUA_REGISTRATION_NOTIFY)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kDuaRegistrationNotify)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, ThreadTlv::kStatus, static_cast(aStatus))); diff --git a/src/core/thread/energy_scan_server.cpp b/src/core/thread/energy_scan_server.cpp index 1e8031901..462983553 100644 --- a/src/core/thread/energy_scan_server.cpp +++ b/src/core/thread/energy_scan_server.cpp @@ -42,7 +42,7 @@ #include "meshcop/meshcop.hpp" #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { @@ -56,7 +56,7 @@ EnergyScanServer::EnergyScanServer(Instance &aInstance) , mActive(false) , mScanResultsLength(0) , mTimer(aInstance, EnergyScanServer::HandleTimer, this) - , mEnergyScan(OT_URI_PATH_ENERGY_SCAN, &EnergyScanServer::HandleRequest, this) + , mEnergyScan(UriPath::kEnergyScan, &EnergyScanServer::HandleRequest, this) { Get().AddResource(mEnergyScan); } @@ -75,7 +75,7 @@ void EnergyScanServer::HandleRequest(Coap::Message &aMessage, const Ip6::Message Ip6::MessageInfo responseInfo(aMessageInfo); uint32_t mask; - VerifyOrExit(aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsPostRequest(), OT_NOOP); SuccessOrExit(Tlv::FindUint8Tlv(aMessage, MeshCoP::Tlv::kCount, count)); SuccessOrExit(Tlv::FindUint16Tlv(aMessage, MeshCoP::Tlv::kPeriod, period)); @@ -177,7 +177,7 @@ void EnergyScanServer::SendReport(void) VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_ENERGY_REPORT)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kEnergyReport)); SuccessOrExit(error = message->SetPayloadMarker()); channelMask.Init(); diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 1a8a4dea4..94318f308 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -47,8 +47,8 @@ #include "net/icmp6.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" #include "thread/time_sync_service.hpp" +#include "thread/uri_paths.hpp" #include "utils/otns.hpp" using ot::Encoding::BigEndian::HostSwap16; @@ -59,8 +59,8 @@ namespace Mle { MleRouter::MleRouter(Instance &aInstance) : Mle(aInstance) , mAdvertiseTimer(aInstance, MleRouter::HandleAdvertiseTimer, nullptr, this) - , mAddressSolicit(OT_URI_PATH_ADDRESS_SOLICIT, &MleRouter::HandleAddressSolicit, this) - , mAddressRelease(OT_URI_PATH_ADDRESS_RELEASE, &MleRouter::HandleAddressRelease, this) + , mAddressSolicit(UriPath::kAddressSolicit, &MleRouter::HandleAddressSolicit, this) + , mAddressRelease(UriPath::kAddressRelease, &MleRouter::HandleAddressRelease, this) , mChildTable(aInstance) , mRouterTable(aInstance) , mChallengeTimeout(0) @@ -3580,7 +3580,7 @@ otError MleRouter::SendAddressSolicit(ThreadStatusTlv::Status aStatus) VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_ADDRESS_SOLICIT)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kAddressSolicit)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kExtMacAddress, Get().GetExtAddress().m8, @@ -3626,7 +3626,7 @@ void MleRouter::SendAddressRelease(void) VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_ADDRESS_RELEASE)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kAddressRelease)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, Rloc16FromRouterId(mRouterId))); @@ -3677,7 +3677,7 @@ void MleRouter::HandleAddressSolicitResponse(Coap::Message * aMessage, VerifyOrExit(aResult == OT_ERROR_NONE && aMessage != nullptr && aMessage != nullptr, OT_NOOP); - VerifyOrExit(aMessage->GetCode() == OT_COAP_CODE_CHANGED, OT_NOOP); + VerifyOrExit(aMessage->GetCode() == Coap::kCodeChanged, OT_NOOP); Log("Receive Address Reply", aMessageInfo->GetPeerAddr()); @@ -3776,7 +3776,7 @@ void MleRouter::HandleAddressSolicit(Coap::Message &aMessage, const Ip6::Message uint16_t xtalAccuracy; #endif - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_PARSE); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), error = OT_ERROR_PARSE); Log("Receive Address Solicit", aMessageInfo.GetPeerAddr()); @@ -3905,7 +3905,7 @@ void MleRouter::HandleAddressRelease(Coap::Message &aMessage, const Ip6::Message uint8_t routerId; Router * router; - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), OT_NOOP); Log("Receive Address Release", aMessageInfo.GetPeerAddr()); diff --git a/src/core/thread/mlr_manager.cpp b/src/core/thread/mlr_manager.cpp index 54f7abdf5..91811ff01 100644 --- a/src/core/thread/mlr_manager.cpp +++ b/src/core/thread/mlr_manager.cpp @@ -41,7 +41,7 @@ #include "common/logging.hpp" #include "net/ip6_address.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #include "utils/slaac_address.hpp" namespace ot { @@ -394,9 +394,9 @@ otError MlrManager::SendMulticastListenerRegistrationMessage(const otIp6Address VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); + message->InitAsConfirmablePost(); SuccessOrExit(message->SetToken(Coap::Message::kDefaultTokenLength)); - SuccessOrExit(message->AppendUriPathOptions(OT_URI_PATH_MLR)); + SuccessOrExit(message->AppendUriPathOptions(UriPath::kMlr)); SuccessOrExit(message->SetPayloadMarker()); addressesTlv.Init(); @@ -508,7 +508,7 @@ otError MlrManager::ParseMulticastListenerRegistrationResponse(otError aR aStatus = ThreadStatusTlv::MlrStatus::kMlrGeneralFailure; VerifyOrExit(aResult == OT_ERROR_NONE && aMessage != nullptr, error = OT_ERROR_PARSE); - VerifyOrExit(aMessage->GetCode() == OT_COAP_CODE_CHANGED, error = OT_ERROR_PARSE); + VerifyOrExit(aMessage->GetCode() == Coap::kCodeChanged, error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::FindUint8Tlv(*aMessage, ThreadTlv::kStatus, aStatus)); diff --git a/src/core/thread/network_data.cpp b/src/core/thread/network_data.cpp index d39241122..f7d676338 100644 --- a/src/core/thread/network_data.cpp +++ b/src/core/thread/network_data.cpp @@ -42,7 +42,7 @@ #include "mac/mac_types.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { namespace NetworkData { @@ -808,7 +808,7 @@ otError NetworkData::SendServerDataNotification(uint16_t aRloc16, Coap::Response VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_SERVER_DATA)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kServerData)); SuccessOrExit(error = message->SetPayloadMarker()); if (mType == kTypeLocal) diff --git a/src/core/thread/network_data_leader.cpp b/src/core/thread/network_data_leader.cpp index f2cdcac73..bbe640bb4 100644 --- a/src/core/thread/network_data_leader.cpp +++ b/src/core/thread/network_data_leader.cpp @@ -48,7 +48,7 @@ #include "thread/mle_router.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { namespace NetworkData { diff --git a/src/core/thread/network_data_leader_ftd.cpp b/src/core/thread/network_data_leader_ftd.cpp index 6ea87b543..5620eb944 100644 --- a/src/core/thread/network_data_leader_ftd.cpp +++ b/src/core/thread/network_data_leader_ftd.cpp @@ -50,7 +50,7 @@ #include "thread/mle_router.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { namespace NetworkData { @@ -58,9 +58,9 @@ namespace NetworkData { Leader::Leader(Instance &aInstance) : LeaderBase(aInstance) , mTimer(aInstance, Leader::HandleTimer, this) - , mServerData(OT_URI_PATH_SERVER_DATA, &Leader::HandleServerData, this) - , mCommissioningDataGet(OT_URI_PATH_COMMISSIONER_GET, &Leader::HandleCommissioningGet, this) - , mCommissioningDataSet(OT_URI_PATH_COMMISSIONER_SET, &Leader::HandleCommissioningSet, this) + , mServerData(UriPath::kServerData, &Leader::HandleServerData, this) + , mCommissioningDataGet(UriPath::kCommissionerGet, &Leader::HandleCommissioningGet, this) + , mCommissioningDataSet(UriPath::kCommissionerSet, &Leader::HandleCommissioningSet, this) { Reset(); } diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index dcf914fb1..13a4aad02 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -46,7 +46,7 @@ #include "thread/mle_router.hpp" #include "thread/thread_netif.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE @@ -56,10 +56,10 @@ namespace NetworkDiagnostic { NetworkDiagnostic::NetworkDiagnostic(Instance &aInstance) : InstanceLocator(aInstance) - , mDiagnosticGetRequest(OT_URI_PATH_DIAGNOSTIC_GET_REQUEST, &NetworkDiagnostic::HandleDiagnosticGetRequest, this) - , mDiagnosticGetQuery(OT_URI_PATH_DIAGNOSTIC_GET_QUERY, &NetworkDiagnostic::HandleDiagnosticGetQuery, this) - , mDiagnosticGetAnswer(OT_URI_PATH_DIAGNOSTIC_GET_ANSWER, &NetworkDiagnostic::HandleDiagnosticGetAnswer, this) - , mDiagnosticReset(OT_URI_PATH_DIAGNOSTIC_RESET, &NetworkDiagnostic::HandleDiagnosticReset, this) + , mDiagnosticGetRequest(UriPath::kDiagnosticGetRequest, &NetworkDiagnostic::HandleDiagnosticGetRequest, this) + , mDiagnosticGetQuery(UriPath::kDiagnosticGetQuery, &NetworkDiagnostic::HandleDiagnosticGetQuery, this) + , mDiagnosticGetAnswer(UriPath::kDiagnosticGetAnswer, &NetworkDiagnostic::HandleDiagnosticGetAnswer, this) + , mDiagnosticReset(UriPath::kDiagnosticReset, &NetworkDiagnostic::HandleDiagnosticReset, this) , mReceiveDiagnosticGetCallback(nullptr) , mReceiveDiagnosticGetCallbackContext(nullptr) { @@ -89,14 +89,12 @@ otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, if (aDestination.IsMulticast()) { - SuccessOrExit( - error = message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DIAGNOSTIC_GET_QUERY)); + SuccessOrExit(error = message->InitAsNonConfirmablePost(UriPath::kDiagnosticGetQuery)); } else { handler = &NetworkDiagnostic::HandleDiagnosticGetResponse; - SuccessOrExit( - error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DIAGNOSTIC_GET_REQUEST)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kDiagnosticGetRequest)); } if (aCount > 0) @@ -149,7 +147,7 @@ void NetworkDiagnostic::HandleDiagnosticGetResponse(Coap::Message * aMes otError aResult) { VerifyOrExit(aResult == OT_ERROR_NONE, OT_NOOP); - VerifyOrExit(aMessage && aMessage->GetCode() == OT_COAP_CODE_CHANGED, OT_NOOP); + VerifyOrExit(aMessage && aMessage->GetCode() == Coap::kCodeChanged, OT_NOOP); otLogInfoNetDiag("Received diagnostic get response"); @@ -172,7 +170,7 @@ void NetworkDiagnostic::HandleDiagnosticGetAnswer(void * aContext, void NetworkDiagnostic::HandleDiagnosticGetAnswer(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) { - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), OT_NOOP); otLogInfoNetDiag("Diagnostic get answer received"); @@ -474,7 +472,7 @@ void NetworkDiagnostic::HandleDiagnosticGetQuery(Coap::Message &aMessage, const NetworkDiagnosticTlv networkDiagnosticTlv; Ip6::MessageInfo messageInfo; - VerifyOrExit(aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); + VerifyOrExit(aMessage.IsPostRequest(), error = OT_ERROR_DROP); otLogInfoNetDiag("Received diagnostic get query"); @@ -495,8 +493,7 @@ void NetworkDiagnostic::HandleDiagnosticGetQuery(Coap::Message &aMessage, const VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = - message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DIAGNOSTIC_GET_ANSWER)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kDiagnosticGetAnswer)); if (networkDiagnosticTlv.GetLength() > 0) { @@ -550,7 +547,7 @@ void NetworkDiagnostic::HandleDiagnosticGetRequest(Coap::Message &aMessage, cons NetworkDiagnosticTlv networkDiagnosticTlv; Ip6::MessageInfo messageInfo(aMessageInfo); - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), error = OT_ERROR_DROP); otLogInfoNetDiag("Received diagnostic get request"); @@ -595,7 +592,7 @@ otError NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination, VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DIAGNOSTIC_RESET)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kDiagnosticReset)); if (aCount > 0) { @@ -647,7 +644,7 @@ void NetworkDiagnostic::HandleDiagnosticReset(Coap::Message &aMessage, const Ip6 otLogInfoNetDiag("Received diagnostic reset request"); - VerifyOrExit(aMessage.IsConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsConfirmablePostRequest(), OT_NOOP); VerifyOrExit((aMessage.Read(aMessage.GetOffset(), sizeof(tlv), &tlv) == sizeof(tlv)), OT_NOOP); diff --git a/src/core/thread/panid_query_server.cpp b/src/core/thread/panid_query_server.cpp index d0a7d4903..5793ffa88 100644 --- a/src/core/thread/panid_query_server.cpp +++ b/src/core/thread/panid_query_server.cpp @@ -42,7 +42,7 @@ #include "meshcop/meshcop.hpp" #include "meshcop/meshcop_tlvs.hpp" #include "thread/thread_netif.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { @@ -51,7 +51,7 @@ PanIdQueryServer::PanIdQueryServer(Instance &aInstance) , mChannelMask(0) , mPanId(Mac::kPanIdBroadcast) , mTimer(aInstance, PanIdQueryServer::HandleTimer, this) - , mPanIdQuery(OT_URI_PATH_PANID_QUERY, &PanIdQueryServer::HandleQuery, this) + , mPanIdQuery(UriPath::kPanIdQuery, &PanIdQueryServer::HandleQuery, this) { Get().AddResource(mPanIdQuery); } @@ -68,7 +68,7 @@ void PanIdQueryServer::HandleQuery(Coap::Message &aMessage, const Ip6::MessageIn Ip6::MessageInfo responseInfo(aMessageInfo); uint32_t mask; - VerifyOrExit(aMessage.GetCode() == OT_COAP_CODE_POST, OT_NOOP); + VerifyOrExit(aMessage.IsPostRequest(), OT_NOOP); VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0, OT_NOOP); SuccessOrExit(Tlv::FindUint16Tlv(aMessage, MeshCoP::Tlv::kPanId, panId)); @@ -117,7 +117,7 @@ void PanIdQueryServer::SendConflict(void) VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); - SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_PANID_CONFLICT)); + SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kPanIdConflict)); SuccessOrExit(error = message->SetPayloadMarker()); channelMask.Init(); diff --git a/src/core/thread/thread_netif.cpp b/src/core/thread/thread_netif.cpp index 113ef49d9..63cd652d8 100644 --- a/src/core/thread/thread_netif.cpp +++ b/src/core/thread/thread_netif.cpp @@ -43,7 +43,7 @@ #include "net/udp6.hpp" #include "thread/mle.hpp" #include "thread/thread_tlvs.hpp" -#include "thread/thread_uri_paths.hpp" +#include "thread/uri_paths.hpp" namespace ot { diff --git a/src/core/thread/thread_uri_paths.hpp b/src/core/thread/thread_uri_paths.hpp deleted file mode 100644 index a2dd2c088..000000000 --- a/src/core/thread/thread_uri_paths.hpp +++ /dev/null @@ -1,338 +0,0 @@ -/* - * Copyright (c) 2016, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * This file includes definitions for Thread URIs. - */ - -#ifndef THREAD_URIS_HPP_ -#define THREAD_URIS_HPP_ - -#include "openthread-core-config.h" - -namespace ot { - -/** - * The URI Path for Address Query. - * - */ -#define OT_URI_PATH_ADDRESS_QUERY "a/aq" - -/** - * @def OT_URI_PATH_ADDRESS_NOTIFY - * - * The URI Path for Address Notify. - * - */ -#define OT_URI_PATH_ADDRESS_NOTIFY "a/an" - -/** - * @def OT_URI_PATH_ADDRESS_ERROR - * - * The URI Path for Address Error. - * - */ -#define OT_URI_PATH_ADDRESS_ERROR "a/ae" - -/** - * @def OT_URI_PATH_ADDRESS_RELEASE - * - * The URI Path for Address Release. - * - */ -#define OT_URI_PATH_ADDRESS_RELEASE "a/ar" - -/** - * @def OT_URI_PATH_ADDRESS_SOLICIT - * - * The URI Path for Address Solicit. - * - */ -#define OT_URI_PATH_ADDRESS_SOLICIT "a/as" - -/** - * @def OT_URI_PATH_ACTIVE_GET - * - * The URI Path for MGMT_ACTIVE_GET - * - */ -#define OT_URI_PATH_ACTIVE_GET "c/ag" - -/** - * @def OT_URI_PATH_ACTIVE_SET - * - * The URI Path for MGMT_ACTIVE_SET - * - */ -#define OT_URI_PATH_ACTIVE_SET "c/as" - -/** - * @def OT_URI_PATH_DATASET_CHANGED - * - * The URI Path for MGMT_DATASET_CHANGED - * - */ -#define OT_URI_PATH_DATASET_CHANGED "c/dc" - -/** - * @def OT_URI_PATH_ENERGY_SCAN - * - * The URI Path for Energy Scan - * - */ -#define OT_URI_PATH_ENERGY_SCAN "c/es" - -/** - * @def OT_URI_PATH_ENERGY_REPORT - * - * The URI Path for Energy Report - * - */ -#define OT_URI_PATH_ENERGY_REPORT "c/er" - -/** - * @def OT_URI_PATH_PENDING_GET - * - * The URI Path for MGMT_PENDING_GET - * - */ -#define OT_URI_PATH_PENDING_GET "c/pg" - -/** - * @def OT_URI_PATH_PENDING_SET - * - * The URI Path for MGMT_PENDING_SET - * - */ -#define OT_URI_PATH_PENDING_SET "c/ps" - -/** - * @def OT_URI_PATH_SERVER_DATA - * - * The URI Path for Server Data Registration. - * - */ -#define OT_URI_PATH_SERVER_DATA "a/sd" - -/** - * @def OT_URI_PATH_ANNOUNCE_BEGIN - * - * The URI Path for Announce Begin. - * - */ -#define OT_URI_PATH_ANNOUNCE_BEGIN "c/ab" - -/** - * @def OT_URI_PATH_PROXY_RX - * - * The URI Path for Proxy RX. - * - */ -#define OT_URI_PATH_PROXY_RX "c/ur" - -/** - * @def OT_URI_PATH_PROXY_TX - * - * The URI Path for Proxy TX. - * - */ -#define OT_URI_PATH_PROXY_TX "c/ut" - -/** - * @def OT_URI_PATH_RELAY_RX - * - * The URI Path for Relay RX. - * - */ -#define OT_URI_PATH_RELAY_RX "c/rx" - -/** - * @def OT_URI_PATH_RELAY_TX - * - * The URI Path for Relay TX. - * - */ -#define OT_URI_PATH_RELAY_TX "c/tx" - -/** - * @def OT_URI_PATH_JOINER_FINALIZE - * - * The URI Path for Joiner Finalize - * - */ -#define OT_URI_PATH_JOINER_FINALIZE "c/jf" - -/** - * @def OT_URI_PATH_JOINER_ENTRUST - * - * The URI Path for Joiner Entrust - * - */ -#define OT_URI_PATH_JOINER_ENTRUST "c/je" - -/** - * @def OT_URI_PATH_LEADER_PETITION - * - * The URI Path for Leader Petition - * - */ -#define OT_URI_PATH_LEADER_PETITION "c/lp" - -/** - * @def OT_URI_PATH_LEADER_KEEP_ALIVE - * - * The URI Path for Leader Keep Alive - * - */ -#define OT_URI_PATH_LEADER_KEEP_ALIVE "c/la" - -/** - * @def OT_URI_PATH_PANID_CONFLICT - * - * The URI Path for PAN ID Conflict - * - */ -#define OT_URI_PATH_PANID_CONFLICT "c/pc" - -/** - * @def OT_URI_PATH_PANID_QUERY - * - * The URI Path for PAN ID Query - * - */ -#define OT_URI_PATH_PANID_QUERY "c/pq" - -/** - * @def OT_URI_PATH_COMMISSIONER_GET - * - * The URI Path for MGMT_COMMISSIONER_GET - * - */ -#define OT_URI_PATH_COMMISSIONER_GET "c/cg" - -/** - * @def OT_URI_PATH_COMMISSIONER_KEEP_ALIVE - * - * The URI Path for Commissioner Keep Alive. - * - */ -#define OT_URI_PATH_COMMISSIONER_KEEP_ALIVE "c/ca" - -/** - * @def OT_URI_PATH_COMMISSIONER_PETITION - * - * The URI Path for Commissioner Petition. - * - */ -#define OT_URI_PATH_COMMISSIONER_PETITION "c/cp" - -/** - * @def OT_URI_PATH_COMMISSIONER_SET - * - * The URI Path for MGMT_COMMISSIONER_SET - * - */ -#define OT_URI_PATH_COMMISSIONER_SET "c/cs" - -/** - * @def OT_URI_PATH_DIAGNOSTIC_GET_REQUEST - * - * The URI Path for Network Diagnostic Get Request. - * - */ -#define OT_URI_PATH_DIAGNOSTIC_GET_REQUEST "d/dg" - -/** - * @def OT_URI_PATH_DIAGNOSTIC_GET_QUERY - * - * The URI Path for Network Diagnostic Get Query. - * - */ -#define OT_URI_PATH_DIAGNOSTIC_GET_QUERY "d/dq" - -/** - * @def OT_URI_PATH_DIAGNOSTIC_GET_ANSWER - * - * The URI Path for Network Diagnostic Get Answer. - * - */ -#define OT_URI_PATH_DIAGNOSTIC_GET_ANSWER "d/da" - -/** - * @def OT_URI_PATH_DIAG_RST - * - * The URI Path for Network Diagnostic Reset. - * - */ -#define OT_URI_PATH_DIAGNOSTIC_RESET "d/dr" - -/** - * @def OT_URI_PATH_MLR - * - * The URI Path for Multicast Listener Registration. - * - */ -#define OT_URI_PATH_MLR "n/mr" - -/** - * @def OT_URI_PATH_DUA_REGISTRATION_REQUEST - * - * The URI Path for Domain Unicast Address Registration Request (DUA.req). - * - */ -#define OT_URI_PATH_DUA_REGISTRATION_REQUEST "n/dr" - -/** - * @def OT_URI_PATH_DUA_REGISTRATION_NOTIFY - * - * The URI Path for Domain Unicast Address Registration Notification (DUA.ntf). - * - */ -#define OT_URI_PATH_DUA_REGISTRATION_NOTIFY "n/dn" - -/** - * @def OT_URI_PATH_BACKBONE_QUERY - * - * The URI Path for Backbone Query (BB.qry). - * - */ -#define OT_URI_PATH_BACKBONE_QUERY "b/bq" - -/** - * @def OT_URI_PATH_BACKBONE_ANSWER - * - * The URI Path for Backbone Answer with destination to a link-local unicast address (BB.ans), - * or Proactive Backbone Notification with destination to a link-local multicast address (PRO_BB.ntf). - * - */ -#define OT_URI_PATH_BACKBONE_ANSWER "b/ba" - -} // namespace ot - -#endif // THREAD_URIS_HPP_ diff --git a/src/core/thread/uri_paths.cpp b/src/core/thread/uri_paths.cpp new file mode 100644 index 000000000..51bec71b8 --- /dev/null +++ b/src/core/thread/uri_paths.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2020, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes definitions for Thread URIs. + */ + +#include "uri_paths.hpp" + +namespace ot { + +const char UriPath::kAddressQuery[] = "a/aq"; +const char UriPath::kAddressNotify[] = "a/an"; +const char UriPath::kAddressError[] = "a/ae"; +const char UriPath::kAddressRelease[] = "a/ar"; +const char UriPath::kAddressSolicit[] = "a/as"; +const char UriPath::kActiveGet[] = "c/ag"; +const char UriPath::kActiveSet[] = "c/as"; +const char UriPath::kDatasetChanged[] = "c/dc"; +const char UriPath::kEnergyScan[] = "c/es"; +const char UriPath::kEnergyReport[] = "c/er"; +const char UriPath::kPendingGet[] = "c/pg"; +const char UriPath::kPendingSet[] = "c/ps"; +const char UriPath::kServerData[] = "a/sd"; +const char UriPath::kAnnounceBegin[] = "c/ab"; +const char UriPath::kProxyRx[] = "c/ur"; +const char UriPath::kProxyTx[] = "c/ut"; +const char UriPath::kRelayRx[] = "c/rx"; +const char UriPath::kRelayTx[] = "c/tx"; +const char UriPath::kJoinerFinalize[] = "c/jf"; +const char UriPath::kJoinerEntrust[] = "c/je"; +const char UriPath::kLeaderPetition[] = "c/lp"; +const char UriPath::kLeaderKeepAlive[] = "c/la"; +const char UriPath::kPanIdConflict[] = "c/pc"; +const char UriPath::kPanIdQuery[] = "c/pq"; +const char UriPath::kCommissionerGet[] = "c/cg"; +const char UriPath::kCommissionerKeepAlive[] = "c/ca"; +const char UriPath::kCommissionerPetition[] = "c/cp"; +const char UriPath::kCommissionerSet[] = "c/cs"; +const char UriPath::kDiagnosticGetRequest[] = "d/dg"; +const char UriPath::kDiagnosticGetQuery[] = "d/dq"; +const char UriPath::kDiagnosticGetAnswer[] = "d/da"; +const char UriPath::kDiagnosticReset[] = "d/dr"; +const char UriPath::kMlr[] = "n/mr"; +const char UriPath::kDuaRegistrationRequest[] = "n/dr"; +const char UriPath::kDuaRegistrationNotify[] = "n/dn"; +const char UriPath::kBackboneQuery[] = "b/bq"; +const char UriPath::kBackboneAnswer[] = "b/ba"; + +} // namespace ot diff --git a/src/core/thread/uri_paths.hpp b/src/core/thread/uri_paths.hpp new file mode 100644 index 000000000..0d08e5de9 --- /dev/null +++ b/src/core/thread/uri_paths.hpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes definitions for Thread URIs. + */ + +#ifndef URI_PATHS_HPP_ +#define URI_PATHS_HPP_ + +#include "openthread-core-config.h" + +namespace ot { + +/** + * + * This structure contains Thread URI Path string definitions. + * + */ +struct UriPath +{ + static const char kAddressQuery[]; ///< The URI Path for Address Query ("a/aq"). + static const char kAddressNotify[]; ///< The URI Path for Address Notify ("a/an"). + static const char kAddressError[]; ///< The URI Path for Address Error ("a/ae"). + static const char kAddressRelease[]; ///< The URI Path for Address Release ("a/ar"). + static const char kAddressSolicit[]; ///< The URI Path for Address Solicit ("a/as"). + static const char kActiveGet[]; ///< The URI Path for MGMT_ACTIVE_GE ("c/ag")T + static const char kActiveSet[]; ///< The URI Path for MGMT_ACTIVE_SET ("c/as"). + static const char kDatasetChanged[]; ///< The URI Path for MGMT_DATASET_CHANGED ("c/dc"). + static const char kEnergyScan[]; ///< The URI Path for Energy Scan ("c/es"). + static const char kEnergyReport[]; ///< The URI Path for Energy Report ("c/er"). + static const char kPendingGet[]; ///< The URI Path for MGMT_PENDING_GET ("c/pg"). + static const char kPendingSet[]; ///< The URI Path for MGMT_PENDING_SET ("c/ps"). + static const char kServerData[]; ///< The URI Path for Server Data Registration ("a/sd"). + static const char kAnnounceBegin[]; ///< The URI Path for Announce Begin ("c/ab"). + static const char kProxyRx[]; ///< The URI Path for Proxy RX ("c/ur"). + static const char kProxyTx[]; ///< The URI Path for Proxy TX ("c/ut"). + static const char kRelayRx[]; ///< The URI Path for Relay RX ("c/rx"). + static const char kRelayTx[]; ///< The URI Path for Relay TX ("c/tx"). + static const char kJoinerFinalize[]; ///< The URI Path for Joiner Finalize ("c/jf"). + static const char kJoinerEntrust[]; ///< The URI Path for Joiner Entrust ("c/je"). + static const char kLeaderPetition[]; ///< The URI Path for Leader Petition ("c/lp"). + static const char kLeaderKeepAlive[]; ///< The URI Path for Leader Keep Alive ("c/la"). + static const char kPanIdConflict[]; ///< The URI Path for PAN ID Conflict ("c/pc"). + static const char kPanIdQuery[]; ///< The URI Path for PAN ID Query ("c/pq"). + static const char kCommissionerGet[]; ///< The URI Path for MGMT_COMMISSIONER_GET ("c/cg"). + static const char kCommissionerKeepAlive[]; ///< The URI Path for Commissioner Keep Alive ("c/ca"). + static const char kCommissionerPetition[]; ///< The URI Path for Commissioner Petition ("c/cp"). + static const char kCommissionerSet[]; ///< The URI Path for MGMT_COMMISSIONER_SET ("c/cs"). + static const char kDiagnosticGetRequest[]; ///< The URI Path for Network Diagnostic Get Request ("d/dg"). + static const char kDiagnosticGetQuery[]; ///< The URI Path for Network Diagnostic Get Query ("d/dq"). + static const char kDiagnosticGetAnswer[]; ///< The URI Path for Network Diagnostic Get Answer ("d/da"). + static const char kDiagnosticReset[]; ///< The URI Path for Network Diagnostic Reset ("d/dr"). + static const char kMlr[]; ///< The URI Path for Multicast Listener Registration ("n/mr"). + static const char kDuaRegistrationRequest[]; ///< The URI Path for DUA Registration Request ("n/dr"). + static const char kDuaRegistrationNotify[]; ///< The URI Path for DUA Registration Notification ("n/dn"). + static const char kBackboneQuery[]; ///< The URI Path for Backbone Query ("b/bq"). + static const char kBackboneAnswer[]; ///< The URI Path for Backbone Answer ("b/ba"). +}; + +} // namespace ot + +#endif // URI_PATHS_HPP_