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 #include <iostream>
27 #include <ostream>
28 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE("LorawanMacHeader");
33 
35  : m_major(0)
36 {
37 }
38 
40 {
41 }
42 
43 TypeId
45 {
46  static TypeId tid =
47  TypeId("LorawanMacHeader").SetParent<Header>().AddConstructor<LorawanMacHeader>();
48  return tid;
49 }
50 
51 TypeId
53 {
54  return GetTypeId();
55 }
56 
57 uint32_t
59 {
60  NS_LOG_FUNCTION_NOARGS();
61 
62  return 1; // This header only consists in 8 bits
63 }
64 
65 void
66 LorawanMacHeader::Serialize(Buffer::Iterator start) const
67 {
68  NS_LOG_FUNCTION_NOARGS();
69 
70  // The header we need to fill
71  uint8_t header = 0;
72 
73  // The MType
74  header |= m_mtype << 5;
75 
76  // Do nothing for the bits that are RFU
77 
78  // The major version bits
79  header |= m_major;
80 
81  // Write the byte
82  start.WriteU8(header);
83 
84  NS_LOG_DEBUG("Serialization of MAC header: " << std::bitset<8>(header));
85 }
86 
87 uint32_t
88 LorawanMacHeader::Deserialize(Buffer::Iterator start)
89 {
90  NS_LOG_FUNCTION_NOARGS();
91 
92  // Save the byte on a temporary variable
93  uint8_t byte;
94  byte = start.ReadU8();
95 
96  // Get the 2 least significant bits to have the Major
97  m_major = byte & 0b11;
98 
99  // Move the three most significant bits to the least significant positions
100  // to get the MType
101  m_mtype = byte >> 5;
102 
103  return 1; // the number of bytes consumed.
104 }
105 
106 void
107 LorawanMacHeader::Print(std::ostream& os) const
108 {
109  os << "MessageType=" << unsigned(m_mtype) << std::endl;
110  os << "Major=" << unsigned(m_major) << std::endl;
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION(this << mtype);
117 
118  m_mtype = mtype;
119 }
120 
121 uint8_t
123 {
124  NS_LOG_FUNCTION_NOARGS();
125 
126  return m_mtype;
127 }
128 
129 void
131 {
132  NS_LOG_FUNCTION_NOARGS();
133 
134  NS_ASSERT(0 <= major && major < 4);
135 
136  m_major = major;
137 }
138 
139 uint8_t
141 {
142  NS_LOG_FUNCTION_NOARGS();
143 
144  return m_major;
145 }
146 
147 bool
149 {
150  NS_LOG_FUNCTION_NOARGS();
151 
152  return (m_mtype == JOIN_REQUEST) || (m_mtype == UNCONFIRMED_DATA_UP) ||
154 }
155 
156 bool
158 {
159  NS_LOG_FUNCTION_NOARGS();
160 
162 }
163 } // 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.