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 "instance/instance.hpp"
38
39 namespace ot {
40 namespace Trel {
41
SetAckMode(AckMode aAckMode)42 void Header::SetAckMode(AckMode aAckMode)
43 {
44 switch (aAckMode)
45 {
46 case kNoAck:
47 mControl &= ~kAckModeFlag;
48 break;
49
50 case kAckRequested:
51 mControl |= kAckModeFlag;
52 break;
53 }
54 }
55
GetSize(Type aType)56 uint16_t Header::GetSize(Type aType)
57 {
58 uint16_t size = sizeof(Header);
59
60 switch (aType)
61 {
62 case kTypeBroadcast:
63 size -= sizeof(Mac::ExtAddress); // Exclude mDestination field.
64 break;
65
66 case kTypeUnicast:
67 case kTypeAck:
68 break;
69 }
70
71 return size;
72 }
73
ToString(void) const74 Header::InfoString Header::ToString(void) const
75 {
76 Type type = GetType();
77 InfoString string;
78
79 switch (type)
80 {
81 case kTypeBroadcast:
82 string.Append("broadcast ch:%d", GetChannel());
83 break;
84
85 case kTypeUnicast:
86 string.Append("unicast ch:%d", GetChannel());
87 break;
88
89 case kTypeAck:
90 string.Append("ack");
91 break;
92 }
93
94 string.Append(" panid:%04x num:%lu src:%s", GetPanId(), ToUlong(GetPacketNumber()),
95 GetSource().ToString().AsCString());
96
97 if ((type == kTypeUnicast) || (type == kTypeAck))
98 {
99 string.Append(" dst:%s", GetDestination().ToString().AsCString());
100 }
101
102 if ((type == kTypeUnicast) || (type == kTypeBroadcast))
103 {
104 string.Append(GetAckMode() == kNoAck ? " no-ack" : " ack-req");
105 }
106
107 return string;
108 }
109
Init(Header::Type aType,uint8_t * aPayload,uint16_t aPayloadLength)110 void Packet::Init(Header::Type aType, uint8_t *aPayload, uint16_t aPayloadLength)
111 {
112 uint16_t headerSize = Header::GetSize(aType);
113
114 // The payload buffer should reserve enough bytes for
115 // header (depending on type) before the payload.
116
117 Init(aPayload - headerSize, aPayloadLength + headerSize);
118 GetHeader().Init(aType);
119 }
120
IsHeaderValid(void) const121 bool Packet::IsHeaderValid(void) const
122 {
123 return ((GetBytes() != nullptr) && (GetLength() > 0) && GetHeader().IsVersionValid() &&
124 (GetLength() >= GetHeader().GetLength()));
125 }
126
127 } // namespace Trel
128 } // namespace ot
129
130 #endif // #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
131