Loading...
Searching...
No Matches
lora-device-address.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 * Modified by: Bastien Tauran <bastien.tauran@viveris.fr>
21 */
22
23#include "lora-device-address.h"
24
25#include "ns3/log.h"
26
27#include <bitset>
28#include <ostream>
29#include <string>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("LoraDeviceAddress");
35
36// NwkID
38
39NwkID::NwkID(uint8_t nwkId)
40 : m_nwkId(nwkId)
41{
42}
43
44void
45NwkID::Set(uint8_t nwkId)
46{
47 // Check whether the MSB is set
48 if (nwkId >> 7)
49 {
50 NS_LOG_WARN("Attempting to set too big a network ID. Will only consider the 7 least "
51 "significant bits.");
52 }
53 m_nwkId = nwkId & 0x7F; // 0x7f = ob01111111
54}
55
56uint8_t
57NwkID::Get(void) const
58{
59 return m_nwkId;
60}
61
62// NwkAddr
64
65NwkAddr::NwkAddr(uint32_t nwkAddr)
66 : m_nwkAddr(nwkAddr)
67{
68}
69
70void
71NwkAddr::Set(uint32_t nwkAddr)
72{
73 // Check whether the most significant bits are set
74 if (nwkAddr >> 25)
75 {
76 NS_LOG_WARN("Attempting to set too big a network address. Will only consider the 25 least "
77 "significant bits.");
78 }
79 m_nwkAddr = nwkAddr & 0x1FFFFFF;
80}
81
82uint32_t
83NwkAddr::Get(void) const
84{
85 return m_nwkAddr;
86}
87
88// LoraDeviceAddress
90
92{
93 NS_LOG_FUNCTION_NOARGS();
94}
95
97{
98 NS_LOG_FUNCTION(this << address);
99
100 Set(address);
101}
102
103LoraDeviceAddress::LoraDeviceAddress(uint8_t nwkId, uint32_t nwkAddr)
104{
105 NS_LOG_FUNCTION(this << unsigned(nwkId) << nwkAddr);
106
107 m_nwkId.Set(nwkId);
108 m_nwkAddr.Set(nwkAddr);
109}
110
112{
113 NS_LOG_FUNCTION(this << unsigned(nwkId.Get()) << nwkAddr.Get());
114
115 m_nwkId = nwkId;
116 m_nwkAddr = nwkAddr;
117}
118
119void
120LoraDeviceAddress::Serialize(uint8_t buf[4]) const
121{
122 NS_LOG_FUNCTION(this << &buf);
123
124 uint32_t address = Get();
125
126 buf[0] = (address >> 24) & 0xff;
127 buf[1] = (address >> 16) & 0xff;
128 buf[2] = (address >> 8) & 0xff;
129 buf[3] = (address >> 0) & 0xff;
130}
131
134{
135 NS_LOG_FUNCTION(&buf);
136
137 // Craft the address from the buffer
138 uint32_t address = 0;
139 address |= buf[0];
140 address <<= 8;
141 address |= buf[1];
142 address <<= 8;
143 address |= buf[2];
144 address <<= 8;
145 address |= buf[3];
146
147 return LoraDeviceAddress(address);
148}
149
150Address
152{
153 NS_LOG_FUNCTION(this);
154
155 uint8_t addressBuffer[4];
156 Serialize(addressBuffer);
157 return Address(GetType(), addressBuffer, 4);
158}
159
161LoraDeviceAddress::ConvertFrom(const Address& address)
162{
163 // Create the new, empty address
165 uint8_t addressBuffer[4];
166
167 // Check that the address we want to convert is compatible with a
168 // LoraDeviceAddress
169 NS_ASSERT(address.CheckCompatible(GetType(), 4));
170 address.CopyTo(addressBuffer);
171 ad = Deserialize(addressBuffer);
172 return ad;
173}
174
175uint8_t
177{
178 NS_LOG_FUNCTION_NOARGS();
179
180 static uint8_t type = Address::Register("LoraDeviceAddress", 4);
181 return type;
182}
183
184uint32_t
186{
187 NS_LOG_FUNCTION_NOARGS();
188
189 uint32_t address = 0;
190 uint32_t nwkId = uint32_t(m_nwkId.Get() << 25);
191 address |= (m_nwkAddr.Get() | nwkId);
192 NS_LOG_DEBUG("m_nwkId + m_nwkAddr = " << std::bitset<32>(address));
193
194 return address;
195}
196
197void
198LoraDeviceAddress::Set(uint32_t address)
199{
200 NS_LOG_FUNCTION_NOARGS();
201
202 m_nwkId.Set(address >> 25); // Only leave the 7 most significant bits
203 m_nwkAddr.Set(address & 0x1FFFFFF); // Only consider the 25 least significant bits
204}
205
206uint8_t
208{
209 NS_LOG_FUNCTION_NOARGS();
210
211 return m_nwkId.Get();
212}
213
214uint32_t
216{
217 NS_LOG_FUNCTION_NOARGS();
218
219 return m_nwkAddr.Get();
220}
221
222void
224{
225 NS_LOG_FUNCTION(this << unsigned(nwkId));
226
227 m_nwkId.Set(nwkId);
228}
229
230void
232{
233 NS_LOG_FUNCTION(this << nwkAddr);
234
235 m_nwkAddr.Set(nwkAddr);
236}
237
238std::string
240{
241 NS_LOG_FUNCTION_NOARGS();
242
243 std::string result;
244 result += std::bitset<7>(m_nwkId.Get()).to_string();
245 result += "|";
246 result += std::bitset<25>(m_nwkAddr.Get()).to_string();
247 return result;
248}
249
250bool
252{
253 return this->Get() == other.Get();
254}
255
256bool
258{
259 return this->Get() != other.Get();
260}
261
262bool
264{
265 return this->Get() < other.Get();
266}
267
268bool
270{
271 return !(this->Get() < other.Get());
272}
273
274std::ostream&
275operator<<(std::ostream& os, const LoraDeviceAddress& address)
276{
277 os << address.Print();
278 return os;
279}
280} // namespace ns3
This class represents the device address of a LoraWAN End Device.
NwkID m_nwkId
The network Id of this address.
static LoraDeviceAddress Deserialize(const uint8_t buf[4])
Convert the input buffer into a new address.
Address ConvertTo(void) const
Convert this instance of LoraDeviceAddress to an Address.
bool operator>(const LoraDeviceAddress &other) const
static LoraDeviceAddress ConvertFrom(const Address &address)
Convert from an ordinary address to a LoraDeviceAddress instance.
void Set(uint32_t address)
Set the address as a 32 bit integer.
uint32_t GetNwkAddr(void)
Get the NwkAddr of this device.
void Serialize(uint8_t buf[4]) const
Convert this address to a buffer.
bool operator!=(const LoraDeviceAddress &other) const
bool operator==(const LoraDeviceAddress &other) const
void SetNwkID(uint8_t nwkId)
Set the NwkID of this device.
uint32_t Get(void) const
Get the address in 32-bit integer form.
uint8_t GetNwkID(void)
Get the NwkID of this device.
static uint8_t GetType(void)
void SetNwkAddr(uint32_t nwkAddr)
Set the NwkAddr of this device.
std::string Print(void) const
Print the address bit-by-bit to a human-readable string.
NwkAddr m_nwkAddr
The network address of this address.
bool operator<(const LoraDeviceAddress &other) const
Class representing the Network Address component of a LoraDeviceAddress (25 bits).
NwkAddr(uint32_t nwkId=0)
void Set(uint32_t nwkAddr)
Set the NwkAddr, starting from a 32-bit representation of a 25-bit integer.
uint32_t Get(void) const
Get an uint32_t representation of the 25-bit network address.
uint32_t m_nwkAddr
8-bit integer representation of the network id
Class representing the NetworkId component of a LoraDeviceAddress (7 bits).
uint8_t m_nwkId
8-bit integer representation of the network id
uint8_t Get(void) 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.
NwkID(uint8_t nwkId=0)
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.
std::ostream & operator<<(std::ostream &os, const GeoCoordinate &coordinate)