A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lora-frame-header.h
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#ifndef LORA_FRAME_HEADER_H
21#define LORA_FRAME_HEADER_H
22
23#include "lora-device-address.h"
24#include "mac-command.h"
25
26#include "ns3/header.h"
27
28namespace ns3
29{
30namespace lorawan
31{
32
33/**
34 * \ingroup lorawan
35 *
36 * This class represents the Frame header (FHDR) used in a LoraWAN network.
37 *
38 * Although the specification divides the FHDR from the FPort field, this
39 * implementation considers them as a unique entity (i.e., FPort is treated as
40 * if it were a part of FHDR).
41 *
42 * \remark Prior to using it, this class needs to be informed of whether the
43 * header is for an uplink or downlink message. This is necessary due to the
44 * fact that UL and DL messages have subtly different structure and, hence,
45 * serialization and deserialization schemes.
46 */
47class LoraFrameHeader : public Header
48{
49 public:
50 LoraFrameHeader(); //!< Default constructor
51 ~LoraFrameHeader() override; //!< Destructor
52
53 // Methods inherited from Header
54
55 /**
56 * Register this type.
57 * \return The object TypeId.
58 */
59 static TypeId GetTypeId();
60 TypeId GetInstanceTypeId() const override;
61
62 /**
63 * Return the size required for serialization of this header.
64 *
65 * \return The serialized size in bytes.
66 */
67 uint32_t GetSerializedSize() const override;
68
69 /**
70 * Serialize the header.
71 *
72 * See Page 15 of LoraWAN specification for a representation of fields.
73 *
74 * \param start A pointer to the buffer that will be filled with the
75 * serialization.
76 */
77 void Serialize(Buffer::Iterator start) const override;
78
79 /**
80 * Deserialize the contents of the buffer into a LoraFrameHeader object.
81 *
82 * \param start A pointer to the buffer we need to deserialize.
83 * \return The number of consumed bytes.
84 */
86
87 /**
88 * Print the header in a human-readable format.
89 *
90 * \param os The std::ostream on which to print the header.
91 */
92 void Print(std::ostream& os) const override;
93
94 /**
95 * State that this is an uplink message.
96 *
97 * This method needs to be called at least once before any serialization or
98 * deserialization.
99 */
100 void SetAsUplink();
101
102 /**
103 * State that this is a downlink message.
104 *
105 * This method needs to be called at least once before any serialization or
106 * deserialization.
107 */
108 void SetAsDownlink();
109
110 /**
111 * Set the FPort value.
112 *
113 * \param fPort The FPort to set.
114 */
115 void SetFPort(uint8_t fPort);
116
117 /**
118 * Get the FPort value.
119 *
120 * \return The FPort value.
121 */
122 uint8_t GetFPort() const;
123
124 /**
125 * Set the address.
126 *
127 * \param address The LoraDeviceAddress to set.
128 */
129 void SetAddress(LoraDeviceAddress address);
130
131 /**
132 * Get this header's device address value.
133 *
134 * \return The address value stored in this header.
135 */
137
138 /**
139 * Set the value of the ADR bit field.
140 *
141 * \param adr Whether or not to set the ADR bit field.
142 */
143 void SetAdr(bool adr);
144
145 /**
146 * Get the value of the ADR bit field.
147 *
148 * \return True if the ADR bit is set, false otherwise.
149 */
150 bool GetAdr() const;
151
152 /**
153 * Set the value of the ADRACKReq bit field.
154 *
155 * \param adrAckReq Whether or not to set the ADRACKReq bit field.
156 */
157 void SetAdrAckReq(bool adrAckReq);
158
159 /**
160 * Get the value of the ADRACKReq bit field.
161 *
162 * \return True if the ADRACKReq bit is set, false otherwise.
163 */
164 bool GetAdrAckReq() const;
165
166 /**
167 * Set the value of the ACK bit field.
168 *
169 * \param ack Whether or not to set the ACK bit.
170 */
171 void SetAck(bool ack);
172
173 /**
174 * Get the value of the ACK bit field.
175 *
176 * \return True if the ACK bit is set, false otherwise.
177 */
178 bool GetAck() const;
179
180 /**
181 * Set the value of the FPending bit field.
182 *
183 * \param fPending Whether or not to set the FPending bit.
184 */
185 void SetFPending(bool fPending);
186
187 /**
188 * Get the value of the FPending bit field.
189 *
190 * \return True if the FPending bit is set, false otherwise.
191 */
192 bool GetFPending() const;
193
194 /**
195 * Get the FOptsLen value.
196 *
197 * \remark This value cannot be set since it's directly extracted from the
198 * number and kind of MAC commands.
199 *
200 * \return The FOptsLen value.
201 */
202 uint8_t GetFOptsLen() const;
203
204 /**
205 * Set the FCnt value.
206 *
207 * \param fCnt The FCnt to set.
208 */
209 void SetFCnt(uint16_t fCnt);
210 /**
211 * Get the FCnt value.
212 *
213 * \return The FCnt value.
214 */
215 uint16_t GetFCnt() const;
216
217 /**
218 * Return a pointer to the first MacCommand of type T, or 0 if no such MacCommand exists
219 * in this header.
220 *
221 * \return A pointer to a MacCommand of type T.
222 */
223 template <typename T>
224 inline Ptr<T> GetMacCommand();
225
226 /**
227 * Add a LinkCheckReq command.
228 */
229 void AddLinkCheckReq();
230
231 /**
232 * Add a LinkCheckAns command.
233 *
234 * \param margin The demodulation margin the LinkCheckReq packet was received with.
235 * \param gwCnt The number of gateways the LinkCheckReq packet was received by.
236 */
237 void AddLinkCheckAns(uint8_t margin, uint8_t gwCnt);
238
239 /**
240 * Add a LinkAdrReq command.
241 *
242 * \param dataRate The data rate at which the receiver should transmit.
243 * \param txPower The power at which the receiver should transmit, encoded according to the
244 * LoRaWAN specification of the region. \param enabledChannels A list containing the indices of
245 * channels enabled by this command. \param repetitions The number of repetitions the receiver
246 * should send when transmitting.
247 */
248 void AddLinkAdrReq(uint8_t dataRate,
249 uint8_t txPower,
250 std::list<int> enabledChannels,
251 int repetitions);
252
253 /**
254 * Add a LinkAdrAns command.
255 *
256 * \param powerAck Whether the power can be set or not.
257 * \param dataRateAck Whether the data rate can be set or not.
258 * \param channelMaskAck Whether the channel mask is coherent with the device's current state or
259 * not.
260 */
261 void AddLinkAdrAns(bool powerAck, bool dataRateAck, bool channelMaskAck);
262
263 /**
264 * Add a DutyCycleReq command.
265 *
266 * This command accepts an 8-bit integer as dutyCycle. The actual dutyCycle
267 * that will be implemented in the end-device will then be, in fraction form,
268 * 1/2^(dutyCycle).
269 *
270 * \param dutyCycle The dutyCycle in 8-bit form.
271 */
272 void AddDutyCycleReq(uint8_t dutyCycle);
273
274 /**
275 * Add a DutyCycleAns command.
276 */
277 void AddDutyCycleAns();
278
279 /**
280 * Add a RxParamSetupReq command.
281 *
282 * \param rx1DrOffset The requested data rate offset for the first receive window.
283 * \param rx2DataRate The requested data rate for the second receive window.
284 * \param frequency The frequency at which to listen for the second receive window.
285 */
286 void AddRxParamSetupReq(uint8_t rx1DrOffset, uint8_t rx2DataRate, double frequency);
287
288 /**
289 * Add a RxParamSetupAns command.
290 */
291 void AddRxParamSetupAns();
292
293 /**
294 * Add a DevStatusReq command.
295 */
296 void AddDevStatusReq();
297
298 /**
299 * Add a NewChannelReq command with provided fields.
300 *
301 * \param chIndex The ChIndex field.
302 * \param frequency The Frequency field.
303 * \param minDataRate The MinDR field.
304 * \param maxDataRate The MaxDR field.
305 */
306 void AddNewChannelReq(uint8_t chIndex,
307 double frequency,
308 uint8_t minDataRate,
309 uint8_t maxDataRate);
310
311 /**
312 * Return a list of pointers to all the MAC commands saved in this header.
313 *
314 * \return The list of pointers to MacCommand objects.
315 */
316 std::list<Ptr<MacCommand>> GetCommands();
317
318 /**
319 * Add a predefined command to the list in this frame header.
320 *
321 * \param macCommand A pointer to the MacCommand object to add.
322 */
323 void AddCommand(Ptr<MacCommand> macCommand);
324
325 private:
326 uint8_t m_fPort; //!< The FPort field
327
328 LoraDeviceAddress m_address; //!< The DevAddr field
329
330 bool m_adr; //!< The ADR field of the FCtrl
331 bool m_adrAckReq; //!< The ADRACKReq field of the FCtrl
332 bool m_ack; //!< The ACK field of the FCtrl
333 bool m_fPending; //!< The FPending/ClassB field of the FCtrl
334 uint8_t m_fOptsLen; //!< The FOptsLen field of the FCtrl
335
336 uint16_t m_fCnt; //!< The FCnt field
337
338 Buffer m_fOpts; //!< The FOpts field
339 std::list<Ptr<MacCommand>> m_macCommands; //!< List containing all the MacCommand instances that
340 //!< are contained in this LoraFrameHeader
341
342 bool m_isUplink; //!< Whether this frame header is uplink or not
343};
344
345template <typename T>
346Ptr<T>
348{
349 // Iterate on MAC commands and try casting
350 std::list<Ptr<MacCommand>>::const_iterator it;
351 for (it = m_macCommands.begin(); it != m_macCommands.end(); ++it)
352 {
353 if ((*it)->GetObject<T>())
354 {
355 return (*it)->GetObject<T>();
356 }
357 }
358
359 // If no command was found, return 0
360 return nullptr;
361}
362} // namespace lorawan
363
364} // namespace ns3
365#endif
iterator in a Buffer instance
Definition: buffer.h:100
automatically resized byte buffer
Definition: buffer.h:94
Protocol header serialization and deserialization.
Definition: header.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
This class represents the device address of a LoraWAN end device.
This class represents the Frame header (FHDR) used in a LoraWAN network.
bool m_ack
The ACK field of the FCtrl.
void AddDutyCycleReq(uint8_t dutyCycle)
Add a DutyCycleReq command.
bool GetAck() const
Get the value of the ACK bit field.
uint8_t GetFPort() const
Get the FPort value.
void AddLinkCheckReq()
Add a LinkCheckReq command.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the contents of the buffer into a LoraFrameHeader object.
void AddCommand(Ptr< MacCommand > macCommand)
Add a predefined command to the list in this frame header.
void SetFCnt(uint16_t fCnt)
Set the FCnt value.
bool GetAdr() const
Get the value of the ADR bit field.
Ptr< T > GetMacCommand()
Return a pointer to the first MacCommand of type T, or 0 if no such MacCommand exists in this header.
void AddNewChannelReq(uint8_t chIndex, double frequency, uint8_t minDataRate, uint8_t maxDataRate)
Add a NewChannelReq command with provided fields.
void AddDutyCycleAns()
Add a DutyCycleAns command.
bool m_fPending
The FPending/ClassB field of the FCtrl.
void AddRxParamSetupAns()
Add a RxParamSetupAns command.
void SetAck(bool ack)
Set the value of the ACK bit field.
bool m_adr
The ADR field of the FCtrl.
uint16_t m_fCnt
The FCnt field.
void SetAdr(bool adr)
Set the value of the ADR bit field.
uint8_t m_fOptsLen
The FOptsLen field of the FCtrl.
void AddDevStatusReq()
Add a DevStatusReq command.
bool GetFPending() const
Get the value of the FPending bit field.
uint32_t GetSerializedSize() const override
Return the size required for serialization of this header.
void SetAddress(LoraDeviceAddress address)
Set the address.
uint16_t GetFCnt() const
Get the FCnt value.
void Print(std::ostream &os) const override
Print the header in a human-readable format.
bool GetAdrAckReq() const
Get the value of the ADRACKReq bit field.
std::list< Ptr< MacCommand > > m_macCommands
List containing all the MacCommand instances that are contained in this LoraFrameHeader.
void SetAsUplink()
State that this is an uplink message.
LoraDeviceAddress m_address
The DevAddr field.
Buffer m_fOpts
The FOpts field.
LoraDeviceAddress GetAddress() const
Get this header's device address value.
void SetAdrAckReq(bool adrAckReq)
Set the value of the ADRACKReq bit field.
~LoraFrameHeader() override
Destructor.
void Serialize(Buffer::Iterator start) const override
Serialize the header.
static TypeId GetTypeId()
Register this type.
bool m_adrAckReq
The ADRACKReq field of the FCtrl.
void SetFPending(bool fPending)
Set the value of the FPending bit field.
void AddLinkCheckAns(uint8_t margin, uint8_t gwCnt)
Add a LinkCheckAns command.
void AddLinkAdrAns(bool powerAck, bool dataRateAck, bool channelMaskAck)
Add a LinkAdrAns command.
bool m_isUplink
Whether this frame header is uplink or not.
void AddLinkAdrReq(uint8_t dataRate, uint8_t txPower, std::list< int > enabledChannels, int repetitions)
Add a LinkAdrReq command.
uint8_t m_fPort
The FPort field.
LoraFrameHeader()
Default constructor.
std::list< Ptr< MacCommand > > GetCommands()
Return a list of pointers to all the MAC commands saved in this header.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetAsDownlink()
State that this is a downlink message.
void SetFPort(uint8_t fPort)
Set the FPort value.
void AddRxParamSetupReq(uint8_t rx1DrOffset, uint8_t rx2DataRate, double frequency)
Add a RxParamSetupReq command.
uint8_t GetFOptsLen() const
Get the FOptsLen value.
Every class exported by the ns3 library is enclosed in the ns3 namespace.