A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lora-device-address.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 "lora-device-address.h"
21
22#include "ns3/log.h"
23
24#include <bitset>
25
26namespace ns3
27{
28namespace lorawan
29{
30
31NS_LOG_COMPONENT_DEFINE("LoraDeviceAddress");
32
33// NwkID
34////////
35
36NwkID::NwkID(uint8_t nwkId)
37 : m_nwkId(nwkId)
38{
39}
40
41void
42NwkID::Set(uint8_t nwkId)
43{
44 // Check whether the MSB is set
45 if (nwkId >> 7)
46 {
47 NS_LOG_WARN("Attempting to set too big a network ID. Will only consider the 7 least "
48 "significant bits.");
49 }
50 m_nwkId = nwkId & 0x7F; // 0x7f = ob01111111
51}
52
53uint8_t
55{
56 return m_nwkId;
57}
58
59// NwkAddr
60//////////
61
63 : m_nwkAddr(nwkAddr)
64{
65}
66
67void
69{
70 // Check whether the most significant bits are set
71 if (nwkAddr >> 25)
72 {
73 NS_LOG_WARN("Attempting to set too big a network address. Will only consider the 25 least "
74 "significant bits.");
75 }
76 m_nwkAddr = nwkAddr & 0x1FFFFFF;
77}
78
81{
82 return m_nwkAddr;
83}
84
85// LoraDeviceAddress
86////////////////////
87
89{
91}
92
94{
95 NS_LOG_FUNCTION(this << address);
96
97 Set(address);
98}
99
101{
102 NS_LOG_FUNCTION(this << unsigned(nwkId) << nwkAddr);
103
104 m_nwkId.Set(nwkId);
105 m_nwkAddr.Set(nwkAddr);
106}
107
109{
110 NS_LOG_FUNCTION(this << unsigned(nwkId.Get()) << nwkAddr.Get());
111
112 m_nwkId = nwkId;
113 m_nwkAddr = nwkAddr;
114}
115
116void
117LoraDeviceAddress::Serialize(uint8_t buf[4]) const
118{
119 NS_LOG_FUNCTION(this << &buf);
120
121 uint32_t address = Get();
122
123 buf[0] = (address >> 24) & 0xff;
124 buf[1] = (address >> 16) & 0xff;
125 buf[2] = (address >> 8) & 0xff;
126 buf[3] = (address >> 0) & 0xff;
127}
128
131{
132 NS_LOG_FUNCTION(&buf);
133
134 // Craft the address from the buffer
135 uint32_t address = 0;
136 address |= buf[0];
137 address <<= 8;
138 address |= buf[1];
139 address <<= 8;
140 address |= buf[2];
141 address <<= 8;
142 address |= buf[3];
143
144 return LoraDeviceAddress(address);
145}
146
149{
150 NS_LOG_FUNCTION(this);
151
152 uint8_t addressBuffer[4];
153 Serialize(addressBuffer);
154 return Address(GetType(), addressBuffer, 4);
155}
156
159{
160 // Create the new, empty address
162 uint8_t addressBuffer[4];
163
164 // Check that the address we want to convert is compatible with a
165 // LoraDeviceAddress
166 NS_ASSERT(address.CheckCompatible(GetType(), 4));
167 address.CopyTo(addressBuffer);
168 ad = Deserialize(addressBuffer);
169 return ad;
170}
171
172uint8_t
174{
176
177 static uint8_t type = Address::Register();
178 return type;
179}
180
183{
185
186 uint32_t address = 0;
187 uint32_t nwkId = m_nwkId.Get() << 25;
188 address |= (m_nwkAddr.Get() | nwkId);
189 NS_LOG_DEBUG("m_nwkId + m_nwkAddr = " << std::bitset<32>(address));
190
191 return address;
192}
193
194void
196{
198
199 m_nwkId.Set(address >> 25); // Only leave the 7 most significant bits
200 m_nwkAddr.Set(address & 0x1FFFFFF); // Only consider the 25 least significant bits
201}
202
203uint8_t
205{
207
208 return m_nwkId.Get();
209}
210
213{
215
216 return m_nwkAddr.Get();
217}
218
219void
221{
222 NS_LOG_FUNCTION(this << unsigned(nwkId));
223
224 m_nwkId.Set(nwkId);
225}
226
227void
229{
230 NS_LOG_FUNCTION(this << nwkAddr);
231
232 m_nwkAddr.Set(nwkAddr);
233}
234
235std::string
237{
239
240 std::string result;
241 result += std::bitset<7>(m_nwkId.Get()).to_string();
242 result += "|";
243 result += std::bitset<25>(m_nwkAddr.Get()).to_string();
244 return result;
245}
246
247bool
249{
250 return this->Get() == other.Get();
251}
252
253bool
255{
256 return this->Get() != other.Get();
257}
258
259bool
261{
262 return this->Get() < other.Get();
263}
264
265bool
267{
268 return !(this->Get() < other.Get());
269}
270
271std::ostream&
272operator<<(std::ostream& os, const LoraDeviceAddress& address)
273{
274 os << address.Print();
275 return os;
276}
277} // namespace lorawan
278} // namespace ns3
a polymophic address class
Definition: address.h:101
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
This class represents the device address of a LoraWAN end device.
void SetNwkID(uint8_t nwkId)
Set the NwkID of this device.
LoraDeviceAddress()
Default constructor.
NwkAddr m_nwkAddr
The network address of this address.
static LoraDeviceAddress Deserialize(const uint8_t buf[4])
Convert the input buffer into a new address.
static LoraDeviceAddress ConvertFrom(const Address &address)
Convert from an ordinary address to a LoraDeviceAddress instance.
NwkID m_nwkId
The network Id of this address.
uint32_t GetNwkAddr()
Get the NwkAddr of this device.
uint32_t Get() const
Get the address in 32-bit integer form.
void Set(uint32_t address)
Set the address as a 32 bit integer.
void SetNwkAddr(uint32_t nwkAddr)
Set the NwkAddr of this device.
static uint8_t GetType()
Get a new address type id.
std::string Print() const
Print the address bit-by-bit to a human-readable string.
void Serialize(uint8_t buf[4]) const
Convert this address to a buffer.
bool operator>(const LoraDeviceAddress &other) const
Greater-then comparison operator.
bool operator<(const LoraDeviceAddress &other) const
Less-then comparison operator.
uint8_t GetNwkID()
Get the NwkID of this device.
bool operator!=(const LoraDeviceAddress &other) const
Inequality comparison operator.
Address ConvertTo() const
Convert this instance of LoraDeviceAddress to an Address.
bool operator==(const LoraDeviceAddress &other) const
Equality comparison operator.
Class representing the Network Address component of a LoraDeviceAddress (25 bits)
uint32_t m_nwkAddr
8-bit integer representation of the network id
void Set(uint32_t nwkAddr)
Set the NwkAddr, starting from a 32-bit representation of a 25-bit integer.
uint32_t Get() const
Get an uint32_t representation of the 25-bit network address.
NwkAddr(uint32_t nwkId=0)
Construct a new NwkAddr object.
Class representing the NetworkId component of a LoraDeviceAddress (7 bits).
uint8_t m_nwkId
8-bit integer representation of the network id
NwkID(uint8_t nwkId=0)
Construct a new NwkID object.
uint8_t Get() const
Get an uint8_t representation of the 7-bit network ID.
void Set(uint8_t nwkId)
Set the NwkID, starting from a 8-bit representation of a 7-bit integer.
#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 ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
std::ostream & operator<<(std::ostream &os, const EndDeviceStatus &status)
Every class exported by the ns3 library is enclosed in the ns3 namespace.