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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
10
11#include "ns3/log.h"
12
13#include <bitset>
14
15namespace ns3
16{
17namespace lorawan
18{
19
20NS_LOG_COMPONENT_DEFINE("LorawanMacHeader");
21
23 : m_major(0)
24{
25}
26
30
33{
34 static TypeId tid =
35 TypeId("LorawanMacHeader").SetParent<Header>().AddConstructor<LorawanMacHeader>();
36 return tid;
37}
38
44
47{
49
50 return 1; // This header only consists in 8 bits
51}
52
53void
55{
57
58 // The header we need to fill
59 uint8_t header = 0;
60
61 // The MType
62 header |= m_mtype << 5;
63
64 // Do nothing for the bits that are RFU
65
66 // The major version bits
67 header |= m_major;
68
69 // Write the byte
70 start.WriteU8(header);
71
72 NS_LOG_DEBUG("Serialization of MAC header: " << std::bitset<8>(header));
73}
74
77{
79
80 // Save the byte on a temporary variable
81 uint8_t byte;
82 byte = start.ReadU8();
83
84 // Get the 2 least significant bits to have the Major
85 m_major = byte & 0b11;
86
87 // Move the three most significant bits to the least significant positions
88 // to get the MType
89 m_mtype = byte >> 5;
90
91 return 1; // the number of bytes consumed.
92}
93
94void
95LorawanMacHeader::Print(std::ostream& os) const
96{
97 os << "MessageType=" << unsigned(m_mtype) << std::endl;
98 os << "Major=" << unsigned(m_major) << std::endl;
99}
100
101void
103{
104 NS_LOG_FUNCTION(this << mtype);
105
106 m_mtype = mtype;
107}
108
109uint8_t
111{
113
114 return m_mtype;
115}
116
117void
119{
121
122 NS_ASSERT(0 <= major && major < 4);
123
124 m_major = major;
125}
126
127uint8_t
129{
131
132 return m_major;
133}
134
135bool
143
144bool
151} // namespace lorawan
152} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
Protocol header serialization and deserialization.
Definition header.h:33
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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.