• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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(), GetPacketNumber(), GetSource().ToString().AsCString());
98 
99     if ((type == kTypeUnicast) || (type == kTypeAck))
100     {
101         string.Append(" dst:%s", GetDestination().ToString().AsCString());
102     }
103 
104     if ((type == kTypeUnicast) || (type == kTypeBroadcast))
105     {
106         string.Append(GetAckMode() == kNoAck ? " no-ack" : " ack-req");
107     }
108 
109     return string;
110 }
111 
Init(Header::Type aType,uint8_t * aPayload,uint16_t aPayloadLength)112 void Packet::Init(Header::Type aType, uint8_t *aPayload, uint16_t aPayloadLength)
113 {
114     uint16_t headerSize = Header::GetSize(aType);
115 
116     // The payload buffer should reserve enough bytes for
117     // header (depending on type) before the payload.
118 
119     Init(aPayload - headerSize, aPayloadLength + headerSize);
120     GetHeader().Init(aType);
121 }
122 
IsHeaderValid(void) const123 bool Packet::IsHeaderValid(void) const
124 {
125     return ((GetBytes() != nullptr) && (GetLength() > 0) && GetHeader().IsVersionValid() &&
126             (GetLength() >= GetHeader().GetLength()));
127 }
128 
129 } // namespace Trel
130 } // namespace ot
131 
132 #endif // #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
133