lorawan-mac-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2017 University of Padova
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Davide Magrin <magrinda@dei.unipd.it>
19  */
20 
21 #include "lorawan-mac-header.h"
22 
23 #include <ns3/log.h>
24 
25 #include <bitset>
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("LorawanMacHeader");
31 
33  : m_major(0)
34 {
35 }
36 
38 {
39 }
40 
41 TypeId
43 {
44  static TypeId tid =
45  TypeId("LorawanMacHeader").SetParent<Header>().AddConstructor<LorawanMacHeader>();
46  return tid;
47 }
48 
49 TypeId
51 {
52  return GetTypeId();
53 }
54 
55 uint32_t
57 {
58  NS_LOG_FUNCTION_NOARGS();
59 
60  return 1; // This header only consists in 8 bits
61 }
62 
63 void
64 LorawanMacHeader::Serialize(Buffer::Iterator start) const
65 {
66  NS_LOG_FUNCTION_NOARGS();
67 
68  // The header we need to fill
69  uint8_t header = 0;
70 
71  // The MType
72  header |= m_mtype << 5;
73 
74  // Do nothing for the bits that are RFU
75 
76  // The major version bits
77  header |= m_major;
78 
79  // Write the byte
80  start.WriteU8(header);
81 
82  NS_LOG_DEBUG("Serialization of MAC header: " << std::bitset<8>(header));
83 }
84 
85 uint32_t
86 LorawanMacHeader::Deserialize(Buffer::Iterator start)
87 {
88  NS_LOG_FUNCTION_NOARGS();
89 
90  // Save the byte on a temporary variable
91  uint8_t byte;
92  byte = start.ReadU8();
93 
94  // Get the 2 least significant bits to have the Major
95  m_major = byte & 0b11;
96 
97  // Move the three most significant bits to the least significant positions
98  // to get the MType
99  m_mtype = byte >> 5;
100 
101  return 1; // the number of bytes consumed.
102 }
103 
104 void
105 LorawanMacHeader::Print(std::ostream& os) const
106 {
107  os << "MessageType=" << unsigned(m_mtype) << std::endl;
108  os << "Major=" << unsigned(m_major) << std::endl;
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION(this << mtype);
115 
116  m_mtype = mtype;
117 }
118 
119 uint8_t
121 {
122  NS_LOG_FUNCTION_NOARGS();
123 
124  return m_mtype;
125 }
126 
127 void
129 {
130  NS_LOG_FUNCTION_NOARGS();
131 
132  NS_ASSERT(0 <= major && major < 4);
133 
134  m_major = major;
135 }
136 
137 uint8_t
139 {
140  NS_LOG_FUNCTION_NOARGS();
141 
142  return m_major;
143 }
144 
145 bool
147 {
148  NS_LOG_FUNCTION_NOARGS();
149 
150  return (m_mtype == JOIN_REQUEST) || (m_mtype == UNCONFIRMED_DATA_UP) ||
152 }
153 
154 bool
156 {
157  NS_LOG_FUNCTION_NOARGS();
158 
160 }
161 } // namespace ns3
void SetMajor(uint8_t major)
Set the major version of this header.
uint8_t m_major
The major version this header is using.
virtual void Serialize(Buffer::Iterator start) const
Serialize the header.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the header.
MType
The message type.
uint8_t GetMajor(void) const
Get the major version from the header.
bool IsConfirmed(void) const
virtual TypeId GetInstanceTypeId(void) const
bool IsUplink(void) const
Check whether this header is for an uplink message.
uint8_t m_mtype
The Message Type.
static TypeId GetTypeId(void)
uint8_t GetMType(void) const
Get the message type from the header.
virtual void Print(std::ostream &os) const
Print the header in a human readable format.
virtual uint32_t GetSerializedSize(void) const
void SetMType(enum MType mtype)
Set the message type.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.