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