[message] adding Message::Settings (#4977)

The `Message::Settings` mirrors `otMessageSettings` and is used
when allocating a new `Message`.
This commit is contained in:
Abtin Keshavarzian
2020-05-20 22:57:56 -07:00
committed by Jonathan Hui
parent 17a0405c51
commit 8160f3423b
16 changed files with 157 additions and 111 deletions
+1 -10
View File
@@ -45,18 +45,9 @@ using namespace ot;
otMessage *otCoapNewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
{
Message * message;
Instance &instance = *static_cast<Instance *>(aInstance);
if (aSettings != NULL)
{
VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL);
}
message = instance.GetApplicationCoap().NewMessage(aSettings);
exit:
return message;
return instance.GetApplicationCoap().NewMessage(Message::Settings(aSettings));
}
void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode)
+9 -12
View File
@@ -170,17 +170,8 @@ otError otIp6Send(otInstance *aInstance, otMessage *aMessage)
otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
{
Instance &instance = *static_cast<Instance *>(aInstance);
Message * message;
if (aSettings != NULL)
{
VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL);
}
message = instance.Get<Ip6::Ip6>().NewMessage(0, aSettings);
exit:
return message;
return instance.Get<Ip6::Ip6>().NewMessage(0, Message::Settings(aSettings));
}
otMessage *otIp6NewMessageFromBuffer(otInstance * aInstance,
@@ -191,9 +182,15 @@ otMessage *otIp6NewMessageFromBuffer(otInstance * aInstance,
Instance &instance = *static_cast<Instance *>(aInstance);
Message * message;
VerifyOrExit((message = instance.Get<Ip6::Ip6>().NewMessage(aData, aDataLength, aSettings)) != NULL, OT_NOOP);
if (aSettings != NULL)
{
message = instance.Get<Ip6::Ip6>().NewMessage(aData, aDataLength, Message::Settings(aSettings));
}
else
{
message = instance.Get<Ip6::Ip6>().NewMessage(aData, aDataLength);
}
exit:
return message;
}
+1 -10
View File
@@ -45,17 +45,8 @@ using namespace ot;
otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
{
Instance &instance = *static_cast<Instance *>(aInstance);
Message * message;
if (aSettings != NULL)
{
VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL);
}
message = instance.Get<Ip6::Udp>().NewMessage(0, aSettings);
exit:
return message;
return instance.Get<Ip6::Udp>().NewMessage(0, Message::Settings(aSettings));
}
otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext)
+1 -1
View File
@@ -113,7 +113,7 @@ void CoapBase::SetInterceptor(Interceptor aInterceptor, void *aContext)
mContext = aContext;
}
Message *CoapBase::NewMessage(const otMessageSettings *aSettings)
Message *CoapBase::NewMessage(const Message::Settings &aSettings)
{
Message *message = NULL;
+3 -7
View File
@@ -330,15 +330,12 @@ public:
/**
* This method creates a new message with a CoAP header.
*
* @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to
* OT_MESSAGE_PRIORITY_NORMAL by default.
*
* @param[in] aSettings A pointer to the message settings or NULL to set default settings.
* @param[in] aSettings The message settings.
*
* @returns A pointer to the message or NULL if failed to allocate message.
*
*/
Message *NewMessage(const otMessageSettings *aSettings = NULL);
Message *NewMessage(const Message::Settings &aSettings = Message::Settings::GetDefault());
/**
* This method creates a new message with a CoAP header that has Network Control priority level.
@@ -348,8 +345,7 @@ public:
*/
Message *NewPriorityMessage(void)
{
otMessageSettings settings = {true, static_cast<otMessagePriority>(Message::kPriorityNet)};
return NewMessage(&settings);
return NewMessage(Message::Settings(Message::kWithLinkSecurity, Message::kPriorityNet));
}
/**
+17 -17
View File
@@ -87,27 +87,13 @@ exit:
return message;
}
Message *MessagePool::New(Message::Type aType, uint16_t aReserveHeader, const otMessageSettings *aSettings)
Message *MessagePool::New(Message::Type aType, uint16_t aReserveHeader, const Message::Settings &aSettings)
{
Message * message;
bool linkSecurityEnabled;
Message::Priority priority;
Message *message = New(aType, aReserveHeader, aSettings.GetPriority());
if (aSettings == NULL)
{
linkSecurityEnabled = true;
priority = Message::kPriorityNormal;
}
else
{
linkSecurityEnabled = aSettings->mLinkSecurityEnabled;
priority = static_cast<Message::Priority>(aSettings->mPriority);
}
message = New(aType, aReserveHeader, priority);
if (message)
{
message->SetLinkSecurityEnabled(linkSecurityEnabled);
message->SetLinkSecurityEnabled(aSettings.IsLinkSecurityEnabled());
}
return message;
@@ -198,6 +184,20 @@ uint16_t MessagePool::GetFreeBufferCount(void) const
return rval;
}
const Message::Settings Message::Settings::kDefault(Message::kWithLinkSecurity, Message::kPriorityNormal);
Message::Settings::Settings(LinkSecurityMode aSecurityMode, Priority aPriority)
: mLinkSecurityEnabled(aSecurityMode == kWithLinkSecurity)
, mPriority(aPriority)
{
}
Message::Settings::Settings(const otMessageSettings *aSettings)
: mLinkSecurityEnabled((aSettings != NULL) ? aSettings->mLinkSecurityEnabled : true)
, mPriority((aSettings != NULL) ? static_cast<Priority>(aSettings->mPriority) : kPriorityNormal)
{
}
otError Message::ResizeMessage(uint16_t aLength)
{
otError error = OT_ERROR_NONE;
+75 -7
View File
@@ -274,6 +274,72 @@ public:
kNumPriorities = 4, ///< Number of priority levels.
};
/**
* This enumeration represents the link security mode (used by `Settings` constructor).
*
*/
enum LinkSecurityMode
{
kNoLinkSecurity, ///< Link security disabled (no link security).
kWithLinkSecurity, ///< Link security enabled.
};
/**
* This class represents settings used for creating a new message.
*
*/
class Settings
{
public:
/**
* This constructor initializes the Settings object.
*
* @param[in] aSecurityMode A link security mode.
* @param[in] aPriority A message priority.
*
*/
Settings(LinkSecurityMode aSecurityMode, Priority aPriority);
/**
* This constructor initializes the `Settings` object from a given `otMessageSettings`.
*
* @param[in] aSettings A pointer to `otMessageSettings` to covert from. If NULL default settings (link
* security enabled with `kPriorityNormal` priority) would be used.
*
*/
Settings(const otMessageSettings *aSettings);
/**
* This method gets the message priority.
*
* @returns The message priority.
*
*/
Priority GetPriority(void) const { return mPriority; }
/**
* This method indicates whether the link security should be enabled.
*
* @returns TRUE if link security should be enabled, FALSE otherwise.
*
*/
bool IsLinkSecurityEnabled(void) const { return mLinkSecurityEnabled; }
/**
* This static method returns the default settings with link security enabled and `kPriorityNormal` priority.
*
* @returns A reference to the default settings (link security enable and `kPriorityNormal` priority).
*
*/
static const Settings &GetDefault(void) { return kDefault; }
private:
static const Settings kDefault;
bool mLinkSecurityEnabled;
Priority mPriority;
};
/**
* This method frees this message buffer.
*
@@ -1138,29 +1204,31 @@ public:
/**
* This method is used to obtain a new message.
*
* The link security is enabled by default on the newly obtained message.
*
* @param[in] aType The message type.
* @param[in] aReserveHeader The number of header bytes to reserve.
* @param[in] aPriority The priority level of the message (default value is `Message::kPriorityNormal`).
* @param[in] aPriority The priority level of the message.
*
* @returns A pointer to the message or NULL if no message buffers are available.
*
*/
Message *New(Message::Type aType, uint16_t aReserveHeader, Message::Priority aPriority = Message::kPriorityNormal);
Message *New(Message::Type aType, uint16_t aReserveHeader, Message::Priority aPriority);
public:
/**
* This method is used to obtain a new message with specified settings.
*
* @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to
* OT_MESSAGE_PRIORITY_NORMAL by default.
*
* @param[in] aType The message type.
* @param[in] aReserveHeader The number of header bytes to reserve.
* @param[in] aSettings A pointer to the message settings or NULL to set default settings.
* @param[in] aSettings The message settings.
*
* @returns A pointer to the message or NULL if no message buffers are available.
*
*/
Message *New(Message::Type aType, uint16_t aReserveHeader, const otMessageSettings *aSettings);
Message *New(Message::Type aType,
uint16_t aReserveHeader,
const Message::Settings &aSettings = Message::Settings::GetDefault());
/**
* This method is used to free a message and return all message buffers to the buffer pool.
+3 -3
View File
@@ -195,8 +195,8 @@ void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::Messa
Kek kek;
uint16_t offset;
uint16_t length;
Message * message = NULL;
otMessageSettings settings = {false, static_cast<otMessagePriority>(kMeshCoPMessagePriority)};
Message * message = NULL;
Message::Settings settings(Message::kNoLinkSecurity, Message::kPriorityNet);
Ip6::MessageInfo messageInfo;
VerifyOrExit(aMessage.IsNonConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP);
@@ -208,7 +208,7 @@ void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::Messa
SuccessOrExit(error = Tlv::GetValueOffset(aMessage, Tlv::kJoinerDtlsEncapsulation, offset, length));
VerifyOrExit((message = mSocket.NewMessage(0, &settings)) != NULL, error = OT_ERROR_NO_BUFS);
VerifyOrExit((message = mSocket.NewMessage(0, settings)) != NULL, error = OT_ERROR_NO_BUFS);
SuccessOrExit(error = message->SetLength(length));
aMessage.CopyTo(offset, 0, length, *message);
+3 -5
View File
@@ -52,18 +52,16 @@ namespace MeshCoP {
enum
{
kMeshCoPMessagePriority = Message::kPriorityNet, ///< The priority for MeshCoP message
kBorderAgentUdpPort = 49191, ///< UDP port of border agent service.
kBorderAgentUdpPort = 49191, ///< UDP port of border agent service.
};
/**
* This function create Message for MeshCoP
* This function creates Message for MeshCoP.
*
*/
inline Coap::Message *NewMeshCoPMessage(Coap::CoapBase &aCoap)
{
otMessageSettings settings = {true, static_cast<otMessagePriority>(kMeshCoPMessagePriority)};
return aCoap.NewMessage(&settings);
return aCoap.NewMessage(Message::Settings(Message::kWithLinkSecurity, Message::kPriorityNet));
}
/**
+2 -2
View File
@@ -97,7 +97,7 @@ otError Icmp::SendError(IcmpHeader::Type aType,
Message * message = NULL;
IcmpHeader icmp6Header;
Header ip6Header;
otMessageSettings settings = {true, static_cast<otMessagePriority>(Message::kPriorityNet)};
Message::Settings settings(Message::kWithLinkSecurity, Message::kPriorityNet);
VerifyOrExit(aMessage.GetLength() >= sizeof(ip6Header), error = OT_ERROR_INVALID_ARGS);
@@ -113,7 +113,7 @@ otError Icmp::SendError(IcmpHeader::Type aType,
messageInfoLocal = aMessageInfo;
VerifyOrExit((message = Get<Ip6>().NewMessage(0, &settings)) != NULL, error = OT_ERROR_NO_BUFS);
VerifyOrExit((message = Get<Ip6>().NewMessage(0, settings)) != NULL, error = OT_ERROR_NO_BUFS);
SuccessOrExit(error = message->SetLength(sizeof(icmp6Header) + sizeof(ip6Header)));
message->Write(sizeof(icmp6Header), sizeof(ip6Header), &ip6Header);
+16 -13
View File
@@ -67,26 +67,17 @@ Ip6::Ip6(Instance &aInstance)
{
}
Message *Ip6::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings)
Message *Ip6::NewMessage(uint16_t aReserved, const Message::Settings &aSettings)
{
return Get<MessagePool>().New(Message::kTypeIp6,
sizeof(Header) + sizeof(HopByHopHeader) + sizeof(OptionMpl) + aReserved, aSettings);
}
Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength, const otMessageSettings *aSettings)
Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings)
{
otMessageSettings settings = {true, OT_MESSAGE_PRIORITY_NORMAL};
Message * message = NULL;
Message::Priority priority;
Message *message = Get<MessagePool>().New(Message::kTypeIp6, 0, aSettings);
if (aSettings != NULL)
{
settings = *aSettings;
}
SuccessOrExit(GetDatagramPriority(aData, aDataLength, priority));
settings.mPriority = static_cast<otMessagePriority>(priority);
VerifyOrExit((message = Get<MessagePool>().New(Message::kTypeIp6, 0, &settings)) != NULL, OT_NOOP);
VerifyOrExit(message != NULL, OT_NOOP);
if (message->Append(aData, aDataLength) != OT_ERROR_NONE)
{
@@ -98,6 +89,18 @@ exit:
return message;
}
Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength)
{
Message * message = NULL;
Message::Priority priority;
SuccessOrExit(GetDatagramPriority(aData, aDataLength, priority));
message = NewMessage(aData, aDataLength, Message::Settings(Message::kWithLinkSecurity, priority));
exit:
return message;
}
Message::Priority Ip6::DscpToPriority(uint8_t aDscp)
{
Message::Priority priority;
+17 -11
View File
@@ -117,32 +117,38 @@ public:
/**
* This method allocates a new message buffer from the buffer pool.
*
* @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to
* OT_MESSAGE_PRIORITY_NORMAL by default.
*
* @param[in] aReserved The number of header bytes to reserve following the IPv6 header.
* @param[in] aSettings A pointer to the message settings or NULL to set default settings.
* @param[in] aSettings The message settings.
*
* @returns A pointer to the message or NULL if insufficient message buffers are available.
*
*/
Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL);
Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings = Message::Settings::GetDefault());
/**
* This method allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message.
*
* @note If @p aSettings is NULL, the link layer security is enabled and the message priority is obtained from
* IPv6 message itself.
* If @p aSettings is not NULL, the @p aSetting->mPriority is ignored and obtained from IPv6 message itself.
*
* @param[in] aData A pointer to the IPv6 datagram buffer.
* @param[in] aDataLength The size of the IPV6 datagram buffer pointed by @p aData.
* @param[in] aSettings A pointer to the message settings or NULL to set default settings.
* @param[in] aSettings The message settings.
*
* @returns A pointer to the message or NULL if malformed IPv6 header or insufficient message buffers are available.
*
*/
Message *NewMessage(const uint8_t *aData, uint16_t aDataLength, const otMessageSettings *aSettings);
Message *NewMessage(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings);
/**
* This method allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message.
*
* @note The link layer security is enabled and the message priority is obtained from IPv6 message itself.
*
* @param[in] aData A pointer to the IPv6 datagram buffer.
* @param[in] aDataLength The size of the IPV6 datagram buffer pointed by @p aData.
*
* @returns A pointer to the message or NULL if malformed IPv6 header or insufficient message buffers are available.
*
*/
Message *NewMessage(const uint8_t *aData, uint16_t aDataLength);
/**
* This method converts the message priority level to IPv6 DSCP value.
+2 -2
View File
@@ -66,7 +66,7 @@ UdpSocket::UdpSocket(Udp &aUdp)
mHandle = NULL;
}
Message *UdpSocket::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings)
Message *UdpSocket::NewMessage(uint16_t aReserved, const Message::Settings &aSettings)
{
return Get<Udp>().NewMessage(aReserved, aSettings);
}
@@ -260,7 +260,7 @@ uint16_t Udp::GetEphemeralPort(void)
return rval;
}
Message *Udp::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings)
Message *Udp::NewMessage(uint16_t aReserved, const Message::Settings &aSettings)
{
return Get<Ip6>().NewMessage(sizeof(UdpHeader) + aReserved, aSettings);
}
+4 -7
View File
@@ -107,16 +107,13 @@ public:
/**
* This method returns a new UDP message with sufficient header space reserved.
*
* @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to
* OT_MESSAGE_PRIORITY_NORMAL by default.
*
* @param[in] aReserved The number of header bytes to reserve after the UDP header.
* @param[in] aSettings A pointer to the message settings or NULL to set default settings.
* @param[in] aSettings The message settings (default is used if not provided).
*
* @returns A pointer to the message or NULL if no buffers are available.
*
*/
Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL);
Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings = Message::Settings::GetDefault());
/**
* This method opens the UDP socket.
@@ -273,12 +270,12 @@ public:
* This method returns a new UDP message with sufficient header space reserved.
*
* @param[in] aReserved The number of header bytes to reserve after the UDP header.
* @param[in] aPriority The priority of the message.
* @param[in] aSettings The message settings.
*
* @returns A pointer to the message or NULL if no buffers are available.
*
*/
Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL);
Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings = Message::Settings::GetDefault());
/**
* This method sends an IPv6 datagram.
+2 -2
View File
@@ -1092,9 +1092,9 @@ const LeaderData &Mle::GetLeaderData(void)
Message *Mle::NewMleMessage(void)
{
Message * message;
otMessageSettings settings = {false, static_cast<otMessagePriority>(kMleMessagePriority)};
Message::Settings settings(Message::kNoLinkSecurity, Message::kPriorityNet);
message = mSocket.NewMessage(0, &settings);
message = mSocket.NewMessage(0, settings);
VerifyOrExit(message != NULL, OT_NOOP);
message->SetSubType(Message::kSubTypeMleGeneral);
+1 -2
View File
@@ -1650,8 +1650,7 @@ protected:
private:
enum
{
kMleMessagePriority = Message::kPriorityNet,
kMleHopLimit = 255,
kMleHopLimit = 255,
// Parameters related to "periodic parent search" feature (CONFIG_ENABLE_PERIODIC_PARENT_SEARCH).
// All timer intervals are converted to milliseconds.