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 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE("LoraDeviceAddress");
33 
34 // NwkID
36 
37 NwkID::NwkID(uint8_t nwkId)
38  : m_nwkId(nwkId)
39 {
40 }
41 
42 void
43 NwkID::Set(uint8_t nwkId)
44 {
45  // Check whether the MSB is set
46  if (nwkId >> 7)
47  {
48  NS_LOG_WARN("Attempting to set too big a network ID. Will only consider the 7 least "
49  "significant bits.");
50  }
51  m_nwkId = nwkId & 0x7F; // 0x7f = ob01111111
52 }
53 
54 uint8_t
55 NwkID::Get(void) const
56 {
57  return m_nwkId;
58 }
59 
60 // NwkAddr
62 
63 NwkAddr::NwkAddr(uint32_t nwkAddr)
64  : m_nwkAddr(nwkAddr)
65 {
66 }
67 
68 void
69 NwkAddr::Set(uint32_t nwkAddr)
70 {
71  // Check whether the most significant bits are set
72  if (nwkAddr >> 25)
73  {
74  NS_LOG_WARN("Attempting to set too big a network address. Will only consider the 25 least "
75  "significant bits.");
76  }
77  m_nwkAddr = nwkAddr & 0x1FFFFFF;
78 }
79 
80 uint32_t
81 NwkAddr::Get(void) const
82 {
83  return m_nwkAddr;
84 }
85 
86 // LoraDeviceAddress
88 
90 {
91  NS_LOG_FUNCTION_NOARGS();
92 }
93 
95 {
96  NS_LOG_FUNCTION(this << address);
97 
98  Set(address);
99 }
100 
101 LoraDeviceAddress::LoraDeviceAddress(uint8_t nwkId, uint32_t nwkAddr)
102 {
103  NS_LOG_FUNCTION(this << unsigned(nwkId) << nwkAddr);
104 
105  m_nwkId.Set(nwkId);
106  m_nwkAddr.Set(nwkAddr);
107 }
108 
110 {
111  NS_LOG_FUNCTION(this << unsigned(nwkId.Get()) << nwkAddr.Get());
112 
113  m_nwkId = nwkId;
114  m_nwkAddr = nwkAddr;
115 }
116 
117 void
118 LoraDeviceAddress::Serialize(uint8_t buf[4]) const
119 {
120  NS_LOG_FUNCTION(this << &buf);
121 
122  uint32_t address = Get();
123 
124  buf[0] = (address >> 24) & 0xff;
125  buf[1] = (address >> 16) & 0xff;
126  buf[2] = (address >> 8) & 0xff;
127  buf[3] = (address >> 0) & 0xff;
128 }
129 
131 LoraDeviceAddress::Deserialize(const uint8_t buf[4])
132 {
133  NS_LOG_FUNCTION(&buf);
134 
135  // Craft the address from the buffer
136  uint32_t address = 0;
137  address |= buf[0];
138  address <<= 8;
139  address |= buf[1];
140  address <<= 8;
141  address |= buf[2];
142  address <<= 8;
143  address |= buf[3];
144 
145  return LoraDeviceAddress(address);
146 }
147 
148 Address
150 {
151  NS_LOG_FUNCTION(this);
152 
153  uint8_t addressBuffer[4];
154  Serialize(addressBuffer);
155  return Address(GetType(), addressBuffer, 4);
156 }
157 
159 LoraDeviceAddress::ConvertFrom(const Address& address)
160 {
161  // Create the new, empty address
163  uint8_t addressBuffer[4];
164 
165  // Check that the address we want to convert is compatible with a
166  // LoraDeviceAddress
167  NS_ASSERT(address.CheckCompatible(GetType(), 4));
168  address.CopyTo(addressBuffer);
169  ad = Deserialize(addressBuffer);
170  return ad;
171 }
172 
173 uint8_t
175 {
176  NS_LOG_FUNCTION_NOARGS();
177 
178  static uint8_t type = Address::Register();
179  return type;
180 }
181 
182 uint32_t
184 {
185  NS_LOG_FUNCTION_NOARGS();
186 
187  uint32_t address = 0;
188  uint32_t nwkId = uint32_t(m_nwkId.Get() << 25);
189  address |= (m_nwkAddr.Get() | nwkId);
190  NS_LOG_DEBUG("m_nwkId + m_nwkAddr = " << std::bitset<32>(address));
191 
192  return address;
193 }
194 
195 void
196 LoraDeviceAddress::Set(uint32_t address)
197 {
198  NS_LOG_FUNCTION_NOARGS();
199 
200  m_nwkId.Set(address >> 25); // Only leave the 7 most significant bits
201  m_nwkAddr.Set(address & 0x1FFFFFF); // Only consider the 25 least significant bits
202 }
203 
204 uint8_t
206 {
207  NS_LOG_FUNCTION_NOARGS();
208 
209  return m_nwkId.Get();
210 }
211 
212 uint32_t
214 {
215  NS_LOG_FUNCTION_NOARGS();
216 
217  return m_nwkAddr.Get();
218 }
219 
220 void
222 {
223  NS_LOG_FUNCTION(this << unsigned(nwkId));
224 
225  m_nwkId.Set(nwkId);
226 }
227 
228 void
230 {
231  NS_LOG_FUNCTION(this << nwkAddr);
232 
233  m_nwkAddr.Set(nwkAddr);
234 }
235 
236 std::string
238 {
239  NS_LOG_FUNCTION_NOARGS();
240 
241  std::string result;
242  result += std::bitset<7>(m_nwkId.Get()).to_string();
243  result += "|";
244  result += std::bitset<25>(m_nwkAddr.Get()).to_string();
245  return result;
246 }
247 
248 bool
250 {
251  return this->Get() == other.Get();
252 }
253 
254 bool
256 {
257  return this->Get() != other.Get();
258 }
259 
260 bool
262 {
263  return this->Get() < other.Get();
264 }
265 
266 bool
268 {
269  return !(this->Get() < other.Get());
270 }
271 
272 std::ostream&
273 operator<<(std::ostream& os, const LoraDeviceAddress& address)
274 {
275  os << address.Print();
276  return os;
277 }
278 } // 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)