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) { return Append<uint16_t>(BigEndian::HostSwap16(aUint16)); }
58 
AppendBigEndianUint32(uint32_t aUint32)59 Error FrameBuilder::AppendBigEndianUint32(uint32_t aUint32) { return Append<uint32_t>(BigEndian::HostSwap32(aUint32)); }
60 
AppendLittleEndianUint16(uint16_t aUint16)61 Error FrameBuilder::AppendLittleEndianUint16(uint16_t aUint16)
62 {
63     return Append<uint16_t>(LittleEndian::HostSwap16(aUint16));
64 }
65 
AppendLittleEndianUint32(uint32_t aUint32)66 Error FrameBuilder::AppendLittleEndianUint32(uint32_t aUint32)
67 {
68     return Append<uint32_t>(LittleEndian::HostSwap32(aUint32));
69 }
70 
AppendBytes(const void * aBuffer,uint16_t aLength)71 Error FrameBuilder::AppendBytes(const void *aBuffer, uint16_t aLength)
72 {
73     Error error = kErrorNone;
74 
75     VerifyOrExit(CanAppend(aLength), error = kErrorNoBufs);
76     memcpy(mBuffer + mLength, aBuffer, aLength);
77     mLength += aLength;
78 
79 exit:
80     return error;
81 }
82 
AppendMacAddress(const Mac::Address & aMacAddress)83 Error FrameBuilder::AppendMacAddress(const Mac::Address &aMacAddress)
84 {
85     Error error = kErrorNone;
86 
87     switch (aMacAddress.GetType())
88     {
89     case Mac::Address::kTypeNone:
90         break;
91 
92     case Mac::Address::kTypeShort:
93         error = AppendLittleEndianUint16(aMacAddress.GetShort());
94         break;
95 
96     case Mac::Address::kTypeExtended:
97         VerifyOrExit(CanAppend(sizeof(Mac::ExtAddress)), error = kErrorNoBufs);
98         aMacAddress.GetExtended().CopyTo(mBuffer + mLength, Mac::ExtAddress::kReverseByteOrder);
99         mLength += sizeof(Mac::ExtAddress);
100         break;
101     }
102 
103 exit:
104     return error;
105 }
106 
107 #if OPENTHREAD_FTD || OPENTHREAD_MTD
AppendBytesFromMessage(const Message & aMessage,uint16_t aOffset,uint16_t aLength)108 Error FrameBuilder::AppendBytesFromMessage(const Message &aMessage, uint16_t aOffset, uint16_t aLength)
109 {
110     Error error = kErrorNone;
111 
112     VerifyOrExit(CanAppend(aLength), error = kErrorNoBufs);
113     SuccessOrExit(error = aMessage.Read(aOffset, mBuffer + mLength, aLength));
114     mLength += aLength;
115 
116 exit:
117     return error;
118 }
119 #endif
120 
WriteBytes(uint16_t aOffset,const void * aBuffer,uint16_t aLength)121 void FrameBuilder::WriteBytes(uint16_t aOffset, const void *aBuffer, uint16_t aLength)
122 {
123     memcpy(mBuffer + aOffset, aBuffer, aLength);
124 }
125 
InsertBytes(uint16_t aOffset,const void * aBuffer,uint16_t aLength)126 Error FrameBuilder::InsertBytes(uint16_t aOffset, const void *aBuffer, uint16_t aLength)
127 {
128     Error error = kErrorNone;
129 
130     OT_ASSERT(aOffset <= mLength);
131 
132     VerifyOrExit(CanAppend(aLength), error = kErrorNoBufs);
133 
134     memmove(mBuffer + aOffset + aLength, mBuffer + aOffset, mLength - aOffset);
135     memcpy(mBuffer + aOffset, aBuffer, aLength);
136     mLength += aLength;
137 
138 exit:
139     return error;
140 }
141 
RemoveBytes(uint16_t aOffset,uint16_t aLength)142 void FrameBuilder::RemoveBytes(uint16_t aOffset, uint16_t aLength)
143 {
144     memmove(mBuffer + aOffset, mBuffer + aOffset + aLength, mLength - aOffset - aLength);
145     mLength -= aLength;
146 }
147 
148 } // namespace ot
149