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 <bitset>
12
13namespace ns3
14{
15namespace lorawan
16{
17
18NS_LOG_COMPONENT_DEFINE("LoraDeviceAddress");
19
20// NwkID
21////////
22
23NwkID::NwkID(uint8_t nwkId)
24 : m_nwkId(nwkId)
25{
26}
27
28void
29NwkID::Set(uint8_t nwkId)
30{
31 // Check whether the MSB is set
32 if (nwkId >> 7)
33 {
34 NS_LOG_WARN("Attempting to set too big a network ID. Will only consider the 7 least "
35 "significant bits.");
36 }
37 m_nwkId = nwkId & 0x7F; // 0x7f = ob01111111
38}
39
40uint8_t
42{
43 return m_nwkId;
44}
45
46// NwkAddr
47//////////
48
50 : m_nwkAddr(nwkAddr)
51{
52}
53
54void
56{
57 // Check whether the most significant bits are set
58 if (nwkAddr >> 25)
59 {
60 NS_LOG_WARN("Attempting to set too big a network address. Will only consider the 25 least "
61 "significant bits.");
62 }
63 m_nwkAddr = nwkAddr & 0x1FFFFFF;
64}
65
68{
69 return m_nwkAddr;
70}
71
72// LoraDeviceAddress
73////////////////////
74
79
81{
82 NS_LOG_FUNCTION(this << address);
83
84 Set(address);
85}
86
88{
89 NS_LOG_FUNCTION(this << unsigned(nwkId) << nwkAddr);
90
91 m_nwkId.Set(nwkId);
92 m_nwkAddr.Set(nwkAddr);
93}
94
96{
97 NS_LOG_FUNCTION(this << unsigned(nwkId.Get()) << nwkAddr.Get());
98
99 m_nwkId = nwkId;
100 m_nwkAddr = nwkAddr;
101}
102
103void
104LoraDeviceAddress::Serialize(uint8_t buf[4]) const
105{
106 NS_LOG_FUNCTION(this << &buf);
107
108 uint32_t address = Get();
109
110 buf[0] = (address >> 24) & 0xff;
111 buf[1] = (address >> 16) & 0xff;
112 buf[2] = (address >> 8) & 0xff;
113 buf[3] = (address >> 0) & 0xff;
114}
115
118{
119 NS_LOG_FUNCTION(&buf);
120
121 // Craft the address from the buffer
122 uint32_t address = 0;
123 address |= buf[0];
124 address <<= 8;
125 address |= buf[1];
126 address <<= 8;
127 address |= buf[2];
128 address <<= 8;
129 address |= buf[3];
130
131 return LoraDeviceAddress(address);
132}
133
136{
137 NS_LOG_FUNCTION(this);
138
139 uint8_t addressBuffer[4];
140 Serialize(addressBuffer);
141 return Address(GetType(), addressBuffer, 4);
142}
143
146{
147 // Create the new, empty address
149 uint8_t addressBuffer[4];
150
151 // Check that the address we want to convert is compatible with a
152 // LoraDeviceAddress
153 NS_ASSERT(address.CheckCompatible(GetType(), 4));
154 address.CopyTo(addressBuffer);
155 ad = Deserialize(addressBuffer);
156 return ad;
157}
158
159uint8_t
161{
163
164 static uint8_t type = Address::Register("LoraDeviceAddress", 4);
165 return type;
166}
167
170{
172
173 uint32_t address = 0;
174 uint32_t nwkId = m_nwkId.Get() << 25;
175 address |= (m_nwkAddr.Get() | nwkId);
176 NS_LOG_DEBUG("m_nwkId + m_nwkAddr = " << std::bitset<32>(address));
177
178 return address;
179}
180
181void
183{
185
186 m_nwkId.Set(address >> 25); // Only leave the 7 most significant bits
187 m_nwkAddr.Set(address & 0x1FFFFFF); // Only consider the 25 least significant bits
188}
189
190uint8_t
192{
194
195 return m_nwkId.Get();
196}
197
200{
202
203 return m_nwkAddr.Get();
204}
205
206void
208{
209 NS_LOG_FUNCTION(this << unsigned(nwkId));
210
211 m_nwkId.Set(nwkId);
212}
213
214void
216{
217 NS_LOG_FUNCTION(this << nwkAddr);
218
219 m_nwkAddr.Set(nwkAddr);
220}
221
222std::string
224{
226
227 std::string result;
228 result += std::bitset<7>(m_nwkId.Get()).to_string();
229 result += "|";
230 result += std::bitset<25>(m_nwkAddr.Get()).to_string();
231 return result;
232}
233
234bool
236{
237 return this->Get() == other.Get();
238}
239
240bool
242{
243 return this->Get() != other.Get();
244}
245
246bool
248{
249 return this->Get() < other.Get();
250}
251
252bool
254{
255 return !(this->Get() < other.Get());
256}
257
258std::ostream&
259operator<<(std::ostream& os, const LoraDeviceAddress& address)
260{
261 os << address.Print();
262 return os;
263}
264
265} // namespace lorawan
266} // namespace ns3
return result
a polymophic address class
Definition address.h:114
static uint8_t Register(const std::string &kind, uint8_t length)
Allocate a new type id for a new type of address.
Definition address.cc:130
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:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#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:253
std::ostream & operator<<(std::ostream &os, const EndDeviceLoraPhy::State &state)
Overloaded operator to print the value of a EndDeviceLoraPhy::State.
Every class exported by the ns3 library is enclosed in the ns3 namespace.