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 * 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("LoraDeviceAddress");
21
22// NwkID
23////////
24
25NwkID::NwkID(uint8_t nwkId)
26 : m_nwkId(nwkId)
27{
28}
29
30void
31NwkID::Set(uint8_t nwkId)
32{
33 // Check whether the MSB is set
34 if (nwkId >> 7)
35 {
36 NS_LOG_WARN("Attempting to set too big a network ID. Will only consider the 7 least "
37 "significant bits.");
38 }
39 m_nwkId = nwkId & 0x7F; // 0x7f = ob01111111
40}
41
42uint8_t
44{
45 return m_nwkId;
46}
47
48// NwkAddr
49//////////
50
52 : m_nwkAddr(nwkAddr)
53{
54}
55
56void
58{
59 // Check whether the most significant bits are set
60 if (nwkAddr >> 25)
61 {
62 NS_LOG_WARN("Attempting to set too big a network address. Will only consider the 25 least "
63 "significant bits.");
64 }
65 m_nwkAddr = nwkAddr & 0x1FFFFFF;
66}
67
70{
71 return m_nwkAddr;
72}
73
74// LoraDeviceAddress
75////////////////////
76
81
83{
84 NS_LOG_FUNCTION(this << address);
85
86 Set(address);
87}
88
90{
91 NS_LOG_FUNCTION(this << unsigned(nwkId) << nwkAddr);
92
93 m_nwkId.Set(nwkId);
94 m_nwkAddr.Set(nwkAddr);
95}
96
98{
99 NS_LOG_FUNCTION(this << unsigned(nwkId.Get()) << nwkAddr.Get());
100
101 m_nwkId = nwkId;
102 m_nwkAddr = nwkAddr;
103}
104
105void
106LoraDeviceAddress::Serialize(uint8_t buf[4]) const
107{
108 NS_LOG_FUNCTION(this << &buf);
109
110 uint32_t address = Get();
111
112 buf[0] = (address >> 24) & 0xff;
113 buf[1] = (address >> 16) & 0xff;
114 buf[2] = (address >> 8) & 0xff;
115 buf[3] = (address >> 0) & 0xff;
116}
117
120{
121 NS_LOG_FUNCTION(&buf);
122
123 // Craft the address from the buffer
124 uint32_t address = 0;
125 address |= buf[0];
126 address <<= 8;
127 address |= buf[1];
128 address <<= 8;
129 address |= buf[2];
130 address <<= 8;
131 address |= buf[3];
132
133 return LoraDeviceAddress(address);
134}
135
138{
139 NS_LOG_FUNCTION(this);
140
141 uint8_t addressBuffer[4];
142 Serialize(addressBuffer);
143 return Address(GetType(), addressBuffer, 4);
144}
145
148{
149 // Create the new, empty address
151 uint8_t addressBuffer[4];
152
153 // Check that the address we want to convert is compatible with a
154 // LoraDeviceAddress
155 NS_ASSERT(address.CheckCompatible(GetType(), 4));
156 address.CopyTo(addressBuffer);
157 ad = Deserialize(addressBuffer);
158 return ad;
159}
160
161uint8_t
163{
165
166 static uint8_t type = Address::Register();
167 return type;
168}
169
172{
174
175 uint32_t address = 0;
176 uint32_t nwkId = m_nwkId.Get() << 25;
177 address |= (m_nwkAddr.Get() | nwkId);
178 NS_LOG_DEBUG("m_nwkId + m_nwkAddr = " << std::bitset<32>(address));
179
180 return address;
181}
182
183void
185{
187
188 m_nwkId.Set(address >> 25); // Only leave the 7 most significant bits
189 m_nwkAddr.Set(address & 0x1FFFFFF); // Only consider the 25 least significant bits
190}
191
192uint8_t
199
207
208void
210{
211 NS_LOG_FUNCTION(this << unsigned(nwkId));
212
213 m_nwkId.Set(nwkId);
214}
215
216void
218{
219 NS_LOG_FUNCTION(this << nwkAddr);
220
221 m_nwkAddr.Set(nwkAddr);
222}
223
224std::string
226{
228
229 std::string result;
230 result += std::bitset<7>(m_nwkId.Get()).to_string();
231 result += "|";
232 result += std::bitset<25>(m_nwkAddr.Get()).to_string();
233 return result;
234}
235
236bool
238{
239 return this->Get() == other.Get();
240}
241
242bool
244{
245 return this->Get() != other.Get();
246}
247
248bool
250{
251 return this->Get() < other.Get();
252}
253
254bool
256{
257 return !(this->Get() < other.Get());
258}
259
260std::ostream&
261operator<<(std::ostream& os, const LoraDeviceAddress& address)
262{
263 os << address.Print();
264 return os;
265}
266} // namespace lorawan
267} // namespace ns3
a polymophic address class
Definition address.h:90
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition address.cc:135
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: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 ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
std::ostream & operator<<(std::ostream &os, const EndDeviceStatus &status)
Every class exported by the ns3 library is enclosed in the ns3 namespace.