Skip to content

Commit

Permalink
[coaps] use LinkSecurityMode to determine layer two security usage (o…
Browse files Browse the repository at this point in the history
…penthread#10899)

This commit updates the `CoapSecure`, `Tmf`, and `SecureTransport`
modules to use the `LinkSecurityMode` enum and its defined constants
to indicate whether or not layer two security should be used. This
replaces the use of boolean input parameters with `kWithLinkSecurity`
or `kNoLinkSecurity` constants, improving code readability.
  • Loading branch information
abtink authored Nov 6, 2024
1 parent fa95df5 commit e43120d
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/core/coap/coap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Message *CoapBase::NewMessage(void) { return NewMessage(Message::Settings::GetDe

Message *CoapBase::NewPriorityMessage(void)
{
return NewMessage(Message::Settings(Message::kWithLinkSecurity, Message::kPriorityNet));
return NewMessage(Message::Settings(kWithLinkSecurity, Message::kPriorityNet));
}

Message *CoapBase::NewPriorityConfirmablePostMessage(Uri aUri)
Expand Down
2 changes: 1 addition & 1 deletion src/core/coap/coap_secure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Coap {

RegisterLogModule("CoapSecure");

CoapSecure::CoapSecure(Instance &aInstance, bool aLayerTwoSecurity)
CoapSecure::CoapSecure(Instance &aInstance, LinkSecurityMode aLayerTwoSecurity)
: CoapBase(aInstance, &CoapSecure::Send)
, mDtls(aInstance, aLayerTwoSecurity)
, mTransmitTask(aInstance, CoapSecure::HandleTransmit, this)
Expand Down
2 changes: 1 addition & 1 deletion src/core/coap/coap_secure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CoapSecure : public CoapBase
* @param[in] aInstance A reference to the OpenThread instance.
* @param[in] aLayerTwoSecurity Specifies whether to use layer two security or not.
*/
explicit CoapSecure(Instance &aInstance, bool aLayerTwoSecurity = false);
explicit CoapSecure(Instance &aInstance, LinkSecurityMode aLayerTwoSecurity);

/**
* Starts the secure CoAP agent.
Expand Down
18 changes: 9 additions & 9 deletions src/core/common/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ class MessageQueue;
class PriorityQueue;
class ThreadLinkInfo;

/**
* Represents the link security mode indicating whether to use MAC (layer two) security.
*/
enum LinkSecurityMode : bool
{
kNoLinkSecurity = false, ///< Link security disabled (no link security).
kWithLinkSecurity = true, ///< Link security enabled.
};

/**
* Represents a Message buffer.
*/
Expand Down Expand Up @@ -308,15 +317,6 @@ class Message : public otMessage, public Buffer, public GetProvider<Message>

static constexpr uint8_t kNumPriorities = 4; ///< Number of priority levels.

/**
* Represents the link security mode (used by `Settings` constructor).
*/
enum LinkSecurityMode : bool
{
kNoLinkSecurity = false, ///< Link security disabled (no link security).
kWithLinkSecurity = true, ///< Link security enabled.
};

/**
* Represents the message ownership model when a `Message` instance is passed to a method/function.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/core/instance/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Instance::Instance(void)
, mApplicationCoap(*this)
#endif
#if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
, mApplicationCoapSecure(*this, /* aLayerTwoSecurity */ true)
, mApplicationCoapSecure(*this, kWithLinkSecurity)
#endif
#if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE
, mApplicationBleSecure(*this)
Expand Down
2 changes: 1 addition & 1 deletion src/core/meshcop/joiner_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ template <> void JoinerRouter::HandleTmf<kUriRelayTx>(Coap::Message &aMessage, c
Kek kek;
OffsetRange offsetRange;
Message *message = nullptr;
Message::Settings settings(Message::kNoLinkSecurity, Message::kPriorityNet);
Message::Settings settings(kNoLinkSecurity, Message::kPriorityNet);
Ip6::MessageInfo messageInfo;

VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), error = kErrorDrop);
Expand Down
2 changes: 1 addition & 1 deletion src/core/meshcop/secure_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const int SecureTransport::kHashes[] = {MBEDTLS_MD_SHA256, MBEDTLS_MD_NONE};
#endif
#endif

SecureTransport::SecureTransport(Instance &aInstance, bool aLayerTwoSecurity, bool aDatagramTransport)
SecureTransport::SecureTransport(Instance &aInstance, LinkSecurityMode aLayerTwoSecurity, bool aDatagramTransport)
: InstanceLocator(aInstance)
, mState(kStateClosed)
, mPskLength(0)
Expand Down
2 changes: 1 addition & 1 deletion src/core/meshcop/secure_transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class SecureTransport : public InstanceLocator
* @param[in] aLayerTwoSecurity Specifies whether to use layer two security or not.
* @param[in] aDatagramTransport Specifies if dtls of tls connection should be used.
*/
explicit SecureTransport(Instance &aInstance, bool aLayerTwoSecurity, bool aDatagramTransport = true);
explicit SecureTransport(Instance &aInstance, LinkSecurityMode aLayerTwoSecurity, bool aDatagramTransport = true);

/**
* Opens the socket.
Expand Down
2 changes: 1 addition & 1 deletion src/core/net/icmp6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Error Icmp::SendError(Header::Type aType, Header::Code aCode, const MessageInfo
MessageInfo messageInfoLocal;
Message *message = nullptr;
Header icmp6Header;
Message::Settings settings(Message::kWithLinkSecurity, Message::kPriorityNet);
Message::Settings settings(kWithLinkSecurity, Message::kPriorityNet);

if (aHeaders.GetIpProto() == kProtoIcmp6)
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/radio/ble_secure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ RegisterLogModule("BleSecure");

BleSecure::BleSecure(Instance &aInstance)
: InstanceLocator(aInstance)
, mTls(aInstance, false, false)
, mTls(aInstance, kNoLinkSecurity, /* aDatagramTransport */ false)
, mTcatAgent(aInstance)
, mTlvMode(false)
, mReceivedMessage(nullptr)
Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/mle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4531,7 +4531,7 @@ Mle::TxMessage *Mle::NewMleMessage(Command aCommand)
{
Error error = kErrorNone;
TxMessage *message;
Message::Settings settings(Message::kNoLinkSecurity, Message::kPriorityNet);
Message::Settings settings(kNoLinkSecurity, Message::kPriorityNet);
uint8_t securitySuite;

message = static_cast<TxMessage *>(mSocket.NewMessage(0, settings));
Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/tmf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Message::Priority Agent::DscpToPriority(uint8_t aDscp)
#if OPENTHREAD_CONFIG_SECURE_TRANSPORT_ENABLE

SecureAgent::SecureAgent(Instance &aInstance)
: Coap::CoapSecure(aInstance)
: Coap::CoapSecure(aInstance, kNoLinkSecurity)
{
SetResourceHandler(&HandleResource);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void TestMessage(void)
Random::NonCrypto::FillBuffer(writeBuffer, kMaxSize);

VerifyOrQuit((message = messagePool->Allocate(Message::kTypeIp6)) != nullptr);
message->SetLinkSecurityEnabled(Message::kWithLinkSecurity);
message->SetLinkSecurityEnabled(kWithLinkSecurity);
SuccessOrQuit(message->SetPriority(Message::Priority::kPriorityNet));
message->SetType(Message::Type::kType6lowpan);
message->SetSubType(Message::SubType::kSubTypeJoinerEntrust);
Expand Down

0 comments on commit e43120d

Please sign in to comment.