From 8d465e5429f2c623bb46676dfab39a6e0ed01c83 Mon Sep 17 00:00:00 2001 From: Nick Banks Date: Mon, 12 Sep 2016 15:02:13 -0700 Subject: [PATCH] Add otNewIp6Message API (#569) * Add new otNewIPv6Message API * Fix Message::Append and otAppendMessage --- include/openthread.h | 18 +++++++++++++++--- src/core/common/message.cpp | 6 +++++- src/core/openthread.cpp | 14 +++++++++++++- src/ncp/ncp_base.cpp | 26 ++++++++++++-------------- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/include/openthread.h b/include/openthread.h index 629a77542..0e3c8ab32 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -1851,7 +1851,8 @@ ThreadError otSetMessageOffset(otMessage aMessage, uint16_t aOffset); * @param[in] aBuf A pointer to the data to append. * @param[in] aLength Number of bytes to append. * - * @returns The number of bytes appended. + * @retval kThreadErrorNone Successfully appended to the message + * @retval kThreadErrorNoBufs No available buffers to grow the message. * * @sa otNewUdpMessage * @sa otFreeMessage @@ -1862,7 +1863,7 @@ ThreadError otSetMessageOffset(otMessage aMessage, uint16_t aOffset); * @sa otReadMessage * @sa otWriteMessage */ -int otAppendMessage(otMessage aMessage, const void *aBuf, uint16_t aLength); +ThreadError otAppendMessage(otMessage aMessage, const void *aBuf, uint16_t aLength); /** * Read bytes from a message. @@ -1921,6 +1922,18 @@ int otWriteMessage(otMessage aMessage, uint16_t aOffset, const void *aBuf, uint1 * */ +/** + * Allocate a new message buffer for sending an IPv6 message. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aLinkSecurityEnabled TRUE if the message should be secured at Layer 2 + * + * @returns A pointer to the message buffer or NULL if no message buffers are available. + * + * @sa otFreeMessage + */ +otMessage otNewIp6Message(otInstance *aInstance, bool aLinkSecurityEnabled); + /** * This function pointer is called when an IPv6 datagram is received. * @@ -1948,7 +1961,6 @@ typedef void (*otReceiveIp6DatagramCallback)(otMessage aMessage, void *aContext) void otSetReceiveIp6DatagramCallback(otInstance *aInstance, otReceiveIp6DatagramCallback aCallback, void *aCallbackContext); - /** * This function indicates whether or not Thread control traffic is filtered out when delivering IPv6 datagrams * via the callback specified in otSetReceiveIp6DatagramCallback(). diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 4791ff421..486b065b6 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -235,9 +235,13 @@ ThreadError Message::Append(const void *aBuf, uint16_t aLength) { ThreadError error = kThreadError_None; uint16_t oldLength = GetLength(); + int bytesWritten; SuccessOrExit(error = SetLength(GetLength() + aLength)); - Write(oldLength, aLength, aBuf); + bytesWritten = Write(oldLength, aLength, aBuf); + + assert(bytesWritten == (int)aLength); + (void)bytesWritten; exit: return error; diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index d07044a91..6e40476ee 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -1108,6 +1108,18 @@ otMessage otNewUdpMessage(otInstance *) return sIp6->mUdp.NewMessage(0); } +otMessage otNewIp6Message(otInstance *, bool aLinkSecurityEnabled) +{ + Message *message = sIp6->mMessagePool.New(Message::kTypeIp6, 0); + + if (message) + { + message->SetLinkSecurityEnabled(aLinkSecurityEnabled); + } + + return message; +} + ThreadError otFreeMessage(otMessage aMessage) { return static_cast(aMessage)->Free(); @@ -1137,7 +1149,7 @@ ThreadError otSetMessageOffset(otMessage aMessage, uint16_t aOffset) return message->SetOffset(aOffset); } -int otAppendMessage(otMessage aMessage, const void *aBuf, uint16_t aLength) +ThreadError otAppendMessage(otMessage aMessage, const void *aBuf, uint16_t aLength) { Message *message = static_cast(aMessage); return message->Append(aBuf, aLength); diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 4c5c487d9..cc15ad54d 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -3148,7 +3148,9 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET_INSECURE(uint8_t header, spin unsigned int frame_len(0); const uint8_t *meta_ptr(NULL); unsigned int meta_len(0); - Message *message(sIp6->mMessagePool.New(Message::kTypeIp6, 0)); + + // STREAM_NET_INSECURE packets are not secured at layer 2. + otMessage message = otNewIp6Message(mInstance, false); if (message == NULL) { @@ -3156,9 +3158,6 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET_INSECURE(uint8_t header, spin } else { - // STREAM_NET_INSECURE packets are not secured at layer 2. - message->SetLinkSecurityEnabled(false); - parsedLength = spinel_datatype_unpack( value_ptr, value_len, @@ -3174,8 +3173,8 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET_INSECURE(uint8_t header, spin (void)meta_ptr; (void)meta_len; (void)parsedLength; - - errorCode = message->Append(frame_ptr, static_cast(frame_len)); + + errorCode = otAppendMessage(message, frame_ptr, static_cast(frame_len)); } if (errorCode == kThreadError_None) @@ -3184,7 +3183,7 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET_INSECURE(uint8_t header, spin } else if (message) { - message->Free(); + otFreeMessage(message); } if (errorCode == kThreadError_None) @@ -3219,7 +3218,9 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET(uint8_t header, spinel_prop_k unsigned int frame_len(0); const uint8_t *meta_ptr(NULL); unsigned int meta_len(0); - Message *message(sIp6->mMessagePool.New(Message::kTypeIp6, 0)); + + // STREAM_NET requires layer 2 security. + otMessage message = otNewIp6Message(mInstance, true); if (message == NULL) { @@ -3227,9 +3228,6 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET(uint8_t header, spinel_prop_k } else { - // STREAM_NET requires layer 2 security. - message->SetLinkSecurityEnabled(true); - parsedLength = spinel_datatype_unpack( value_ptr, value_len, @@ -3245,8 +3243,8 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET(uint8_t header, spinel_prop_k (void)meta_ptr; (void)meta_len; (void)parsedLength; - - errorCode = message->Append(frame_ptr, static_cast(frame_len)); + + errorCode = otAppendMessage(message, frame_ptr, static_cast(frame_len)); } if (errorCode == kThreadError_None) @@ -3255,7 +3253,7 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET(uint8_t header, spinel_prop_k } else if (message) { - message->Free(); + otFreeMessage(message); } if (errorCode == kThreadError_None)