Add otNewIp6Message API (#569)

* Add new otNewIPv6Message API
* Fix Message::Append and otAppendMessage
This commit is contained in:
Nick Banks
2016-09-12 15:02:13 -07:00
committed by Jonathan Hui
parent ffbe65c6dd
commit 8d465e5429
4 changed files with 45 additions and 19 deletions
+15 -3
View File
@@ -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().
+5 -1
View File
@@ -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;
+13 -1
View File
@@ -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<Message *>(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<Message *>(aMessage);
return message->Append(aBuf, aLength);
+12 -14
View File
@@ -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<uint16_t>(frame_len));
errorCode = otAppendMessage(message, frame_ptr, static_cast<uint16_t>(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<uint16_t>(frame_len));
errorCode = otAppendMessage(message, frame_ptr, static_cast<uint16_t>(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)