1 /*
2 * Copyright (c) 2019, 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" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /**
29 * @file
30 * This file implements Thread Radio Encapsulation Link (TREL) packet.
31 */
32
33 #include "trel_packet.hpp"
34
35 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
36
37 #include "common/code_utils.hpp"
38 #include "common/debug.hpp"
39 #include "common/instance.hpp"
40 #include "common/locator_getters.hpp"
41 #include "common/logging.hpp"
42
43 namespace ot {
44 namespace Trel {
45
SetAckMode(AckMode aAckMode)46 void Header::SetAckMode(AckMode aAckMode)
47 {
48 switch (aAckMode)
49 {
50 case kNoAck:
51 mControl &= ~kAckModeFlag;
52 break;
53
54 case kAckRequested:
55 mControl |= kAckModeFlag;
56 break;
57 }
58 }
59
GetSize(Type aType)60 uint16_t Header::GetSize(Type aType)
61 {
62 uint16_t size = sizeof(Header);
63
64 switch (aType)
65 {
66 case kTypeBroadcast:
67 size -= sizeof(Mac::ExtAddress); // Exclude mDestination field.
68 break;
69
70 case kTypeUnicast:
71 case kTypeAck:
72 break;
73 }
74
75 return size;
76 }
77
ToString(void) const78 Header::InfoString Header::ToString(void) const
79 {
80 Type type = GetType();
81 InfoString string;
82
83 switch (type)
84 {
85 case kTypeBroadcast:
86 string.Append("broadcast ch:%d", GetChannel());
87 break;
88
89 case kTypeUnicast:
90 string.Append("unicast ch:%d", GetChannel());
91 break;
92
93 case kTypeAck:
94 string.Append("ack");
95 break;
96 }
97
98 string.Append(" panid:%04x num:%lu src:%s", GetPanId(), GetPacketNumber(), GetSource().ToString().AsCString());
99
100 if ((type == kTypeUnicast) || (type == kTypeAck))
101 {
102 string.Append(" dst:%s", GetDestination().ToString().AsCString());
103 }
104
105 if ((type == kTypeUnicast) || (type == kTypeBroadcast))
106 {
107 string.Append(GetAckMode() == kNoAck ? " no-ack" : " ack-req");
108 }
109
110 return string;
111 }
112
Init(uint8_t * aBuffer,uint16_t aLength)113 void Packet::Init(uint8_t *aBuffer, uint16_t aLength)
114 {
115 mBuffer = aBuffer;
116 mLength = aLength;
117 }
118
Init(Header::Type aType,uint8_t * aPayload,uint16_t aPayloadLength)119 void Packet::Init(Header::Type aType, uint8_t *aPayload, uint16_t aPayloadLength)
120 {
121 uint16_t headerSize = Header::GetSize(aType);
122
123 // The payload buffer should reserve enough bytes for
124 // header (depending on type) before the payload.
125
126 Init(aPayload - headerSize, aPayloadLength + headerSize);
127 GetHeader().Init(aType);
128 }
129
IsHeaderValid(void) const130 bool Packet::IsHeaderValid(void) const
131 {
132 return ((mBuffer != nullptr) && (mLength > 0) && GetHeader().IsVersionValid() &&
133 (mLength >= GetHeader().GetLength()));
134 }
135
136 } // namespace Trel
137 } // namespace ot
138
139 #endif // #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
140