A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lorawan-mac-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 */
19
20#include "lorawan-mac-header.h"
21
22#include "ns3/log.h"
23
24#include <bitset>
25
26namespace ns3
27{
28namespace lorawan
29{
30
31NS_LOG_COMPONENT_DEFINE("LorawanMacHeader");
32
34 : m_major(0)
35{
36}
37
39{
40}
41
44{
45 static TypeId tid =
46 TypeId("LorawanMacHeader").SetParent<Header>().AddConstructor<LorawanMacHeader>();
47 return tid;
48}
49
52{
53 return GetTypeId();
54}
55
58{
60
61 return 1; // This header only consists in 8 bits
62}
63
64void
66{
68
69 // The header we need to fill
70 uint8_t header = 0;
71
72 // The MType
73 header |= m_mtype << 5;
74
75 // Do nothing for the bits that are RFU
76
77 // The major version bits
78 header |= m_major;
79
80 // Write the byte
81 start.WriteU8(header);
82
83 NS_LOG_DEBUG("Serialization of MAC header: " << std::bitset<8>(header));
84}
85
88{
90
91 // Save the byte on a temporary variable
92 uint8_t byte;
93 byte = start.ReadU8();
94
95 // Get the 2 least significant bits to have the Major
96 m_major = byte & 0b11;
97
98 // Move the three most significant bits to the least significant positions
99 // to get the MType
100 m_mtype = byte >> 5;
101
102 return 1; // the number of bytes consumed.
103}
104
105void
106LorawanMacHeader::Print(std::ostream& os) const
107{
108 os << "MessageType=" << unsigned(m_mtype) << std::endl;
109 os << "Major=" << unsigned(m_major) << std::endl;
110}
111
112void
114{
115 NS_LOG_FUNCTION(this << mtype);
116
117 m_mtype = mtype;
118}
119
120uint8_t
122{
124
125 return m_mtype;
126}
127
128void
130{
132
133 NS_ASSERT(0 <= major && major < 4);
134
135 m_major = major;
136}
137
138uint8_t
140{
142
143 return m_major;
144}
145
146bool
148{
150
151 return (m_mtype == JOIN_REQUEST) || (m_mtype == UNCONFIRMED_DATA_UP) ||
153}
154
155bool
157{
159
161}
162} // namespace lorawan
163} // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
Protocol header serialization and deserialization.
Definition: header.h:44
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetMajor(uint8_t major)
Set the major version of this header.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the header.
uint8_t GetMType() const
Get the message type from the header.
bool IsUplink() const
Check whether this header is for an uplink message.
LorawanMacHeader()
Default constructor.
~LorawanMacHeader() override
Destructor.
uint8_t m_major
The major version this header is using.
static TypeId GetTypeId()
Register this type.
void Print(std::ostream &os) const override
Print the header in a human readable format.
uint8_t m_mtype
The Message Type.
void Serialize(Buffer::Iterator start) const override
Serialize the header.
bool IsConfirmed() const
Check whether this header is for a confirmed message, i.e.
void SetMType(enum MType mtype)
Set the message type.
uint32_t GetSerializedSize() const override
uint8_t GetMajor() const
Get the major version from the header.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.