1 /*
2 * Copyright (c) 2022, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements the `FrameBuilder` class.
32 */
33
34 #include "frame_builder.hpp"
35
36 #include <string.h>
37
38 #include "common/code_utils.hpp"
39 #include "common/debug.hpp"
40 #include "common/encoding.hpp"
41
42 #if OPENTHREAD_FTD || OPENTHREAD_MTD
43 #include "common/message.hpp"
44 #endif
45
46 namespace ot {
47
Init(void * aBuffer,uint16_t aLength)48 void FrameBuilder::Init(void *aBuffer, uint16_t aLength)
49 {
50 mBuffer = static_cast<uint8_t *>(aBuffer);
51 mLength = 0;
52 mMaxLength = aLength;
53 }
54
AppendUint8(uint8_t aUint8)55 Error FrameBuilder::AppendUint8(uint8_t aUint8) { return Append<uint8_t>(aUint8); }
56
AppendBigEndianUint16(uint16_t aUint16)57 Error FrameBuilder::AppendBigEndianUint16(uint16_t aUint16)
58 {
59 return Append<uint16_t>(Encoding::BigEndian::HostSwap16(aUint16));
60 }
61
AppendBigEndianUint32(uint32_t aUint32)62 Error FrameBuilder::AppendBigEndianUint32(uint32_t aUint32)
63 {
64 return Append<uint32_t>(Encoding::BigEndian::HostSwap32(aUint32));
65 }
66
AppendLittleEndianUint16(uint16_t aUint16)67 Error FrameBuilder::AppendLittleEndianUint16(uint16_t aUint16)
68 {
69 return Append<uint16_t>(Encoding::LittleEndian::HostSwap16(aUint16));
70 }
71
AppendLittleEndianUint32(uint32_t aUint32)72 Error FrameBuilder::AppendLittleEndianUint32(uint32_t aUint32)
73 {
74 return Append<uint32_t>(Encoding::LittleEndian::HostSwap32(aUint32));
75 }
76
AppendBytes(const void * aBuffer,uint16_t aLength)77 Error FrameBuilder::AppendBytes(const void *aBuffer, uint16_t aLength)
78 {
79 Error error = kErrorNone;
80
81 VerifyOrExit(CanAppend(aLength), error = kErrorNoBufs);
82 memcpy(mBuffer + mLength, aBuffer, aLength);
83 mLength += aLength;
84
85 exit:
86 return error;
87 }
88
AppendMacAddress(const Mac::Address & aMacAddress)89 Error FrameBuilder::AppendMacAddress(const Mac::Address &aMacAddress)
90 {
91 Error error = kErrorNone;
92
93 switch (aMacAddress.GetType())
94 {
95 case Mac::Address::kTypeNone:
96 break;
97
98 case Mac::Address::kTypeShort:
99 error = AppendLittleEndianUint16(aMacAddress.GetShort());
100 break;
101
102 case Mac::Address::kTypeExtended:
103 VerifyOrExit(CanAppend(sizeof(Mac::ExtAddress)), error = kErrorNoBufs);
104 aMacAddress.GetExtended().CopyTo(mBuffer + mLength, Mac::ExtAddress::kReverseByteOrder);
105 mLength += sizeof(Mac::ExtAddress);
106 break;
107 }
108
109 exit:
110 return error;
111 }
112
113 #if OPENTHREAD_FTD || OPENTHREAD_MTD
AppendBytesFromMessage(const Message & aMessage,uint16_t aOffset,uint16_t aLength)114 Error FrameBuilder::AppendBytesFromMessage(const Message &aMessage, uint16_t aOffset, uint16_t aLength)
115 {
116 Error error = kErrorNone;
117
118 VerifyOrExit(CanAppend(aLength), error = kErrorNoBufs);
119 SuccessOrExit(error = aMessage.Read(aOffset, mBuffer + mLength, aLength));
120 mLength += aLength;
121
122 exit:
123 return error;
124 }
125 #endif
126
WriteBytes(uint16_t aOffset,const void * aBuffer,uint16_t aLength)127 void FrameBuilder::WriteBytes(uint16_t aOffset, const void *aBuffer, uint16_t aLength)
128 {
129 memcpy(mBuffer + aOffset, aBuffer, aLength);
130 }
131
InsertBytes(uint16_t aOffset,const void * aBuffer,uint16_t aLength)132 Error FrameBuilder::InsertBytes(uint16_t aOffset, const void *aBuffer, uint16_t aLength)
133 {
134 Error error = kErrorNone;
135
136 OT_ASSERT(aOffset <= mLength);
137
138 VerifyOrExit(CanAppend(aLength), error = kErrorNoBufs);
139
140 memmove(mBuffer + aOffset + aLength, mBuffer + aOffset, mLength - aOffset);
141 memcpy(mBuffer + aOffset, aBuffer, aLength);
142 mLength += aLength;
143
144 exit:
145 return error;
146 }
147
RemoveBytes(uint16_t aOffset,uint16_t aLength)148 void FrameBuilder::RemoveBytes(uint16_t aOffset, uint16_t aLength)
149 {
150 memmove(mBuffer + aOffset, mBuffer + aOffset + aLength, mLength - aOffset - aLength);
151 mLength -= aLength;
152 }
153
154 } // namespace ot
155