A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-fields.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan.
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: Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
18 */
19
20#include "lr-wpan-fields.h"
21
22#include <ns3/address-utils.h>
23#include <ns3/log.h>
24
25namespace ns3
26{
27
29{
33 SetBattLifeExt(false);
34 SetPanCoor(false);
35 SetAssocPermit(false);
36}
37
39{
40 SetSuperframe(bitmap);
41}
42
43void
44SuperframeField::SetSuperframe(uint16_t superFrmSpec)
45{
46 m_sspecBcnOrder = (superFrmSpec) & (0x0F); // Bits 0-3
47 m_sspecSprFrmOrder = (superFrmSpec >> 4) & (0x0F); // Bits 4-7
48 m_sspecFnlCapSlot = (superFrmSpec >> 8) & (0x0F); // Bits 8-11
49 m_sspecBatLifeExt = (superFrmSpec >> 12) & (0x01); // Bit 12
50 // Bit 13 (Reserved)
51 m_sspecPanCoor = (superFrmSpec >> 14) & (0x01); // Bit 14
52 m_sspecAssocPermit = (superFrmSpec >> 15) & (0x01); // Bit 15
53}
54
55void
57{
58 if (bcnOrder > 15)
59 {
60 NS_ABORT_MSG("SuperframeField Beacon Order value must be 15 or less");
61 }
62 else
63 {
64 m_sspecBcnOrder = bcnOrder;
65 }
66}
67
68void
70{
71 if (frmOrder > 15)
72 {
73 NS_ABORT_MSG("SuperframeField Frame Order value must be 15 or less");
74 }
75 else
76 {
77 m_sspecSprFrmOrder = frmOrder;
78 }
79}
80
81void
83{
84 if (capSlot > 15)
85 {
86 NS_ABORT_MSG("The final slot cannot be greater than the slots in a CAP (15)");
87 }
88 else
89 {
90 m_sspecFnlCapSlot = capSlot;
91 }
92}
93
94void
96{
97 m_sspecBatLifeExt = battLifeExt;
98}
99
100void
102{
103 m_sspecPanCoor = panCoor;
104}
105
106void
108{
109 m_sspecAssocPermit = assocPermit;
110}
111
112uint8_t
114{
115 return m_sspecBcnOrder;
116}
117
118uint8_t
120{
121 return m_sspecSprFrmOrder;
122}
123
124uint8_t
126{
127 return m_sspecFnlCapSlot;
128}
129
130bool
132{
133 return m_sspecBatLifeExt;
134}
135
136bool
138{
139 return m_sspecPanCoor;
140}
141
142bool
144{
145 return m_sspecAssocPermit;
146}
147
148uint16_t
150{
151 uint16_t superframe;
152
153 superframe = m_sspecBcnOrder & (0x0F); // Bits 0-3
154 superframe |= (m_sspecSprFrmOrder << 4) & (0x0F << 4); // Bits 4-7
155 superframe |= (m_sspecFnlCapSlot << 8) & (0x0F << 8); // Bits 8-11
156 superframe |= (m_sspecBatLifeExt << 12) & (0x01 << 12); // Bit 12
157 // Bit 13 (Reserved)
158 superframe |= (m_sspecPanCoor << 14) & (0x01 << 14); // Bit 14
159 superframe |= (m_sspecAssocPermit << 15) & (0x01 << 15); // Bit 15
160
161 return superframe;
162}
163
164std::ostream&
165operator<<(std::ostream& os, const SuperframeField& superframeField)
166{
167 os << " Beacon Order = " << uint32_t(superframeField.GetBeaconOrder())
168 << ", Frame Order = " << uint32_t(superframeField.GetFrameOrder())
169 << ", Final CAP slot = " << uint32_t(superframeField.GetFinalCapSlot())
170 << ", Battery Life Ext = " << bool(superframeField.IsBattLifeExt())
171 << ", PAN Coordinator = " << bool(superframeField.IsPanCoor())
172 << ", Association Permit = " << bool(superframeField.IsAssocPermit());
173 return os;
174}
175
176/***********************************************************
177 * Guaranteed Time Slots (GTS) Fields
178 ***********************************************************/
179
181{
182 // GTS Specification Field
184 m_gtsSpecPermit = 0;
185 // GTS Direction Field
186 m_gtsDirMask = 0;
187}
188
189uint8_t
191{
192 uint8_t gtsSpecField;
193
194 gtsSpecField = m_gtsSpecDescCount & (0x07); // Bits 0-2
195 // Bits 3-6 (Reserved)
196 gtsSpecField |= (m_gtsSpecPermit << 7) & (0x01 << 7); // Bit 7
197
198 return gtsSpecField;
199}
200
201uint8_t
203{
204 uint8_t gtsDirectionField;
205
206 gtsDirectionField = m_gtsDirMask & (0x7F); // Bit 0-6
207 // Bit 7 (Reserved)
208 return gtsDirectionField;
209}
210
211void
213{
214 m_gtsSpecDescCount = (gtsSpec) & (0x07); // Bits 0-2
215 // Bits 3-6 (Reserved)
216 m_gtsSpecPermit = (gtsSpec >> 7) & (0x01); // Bit 7
217}
218
219void
221{
222 m_gtsDirMask = (gtsDir) & (0x7F); // Bits 0-6
223 // Bit 7 (Reserved)
224}
225
226bool
228{
229 return m_gtsSpecPermit;
230}
231
234{
235 uint32_t size;
236
237 size = 1; // 1 octet GTS Specification Field
238 if (m_gtsSpecDescCount > 0)
239 {
240 size += 1; // 1 octet GTS Direction Field
241 size += (m_gtsSpecDescCount * 3); // 3 octets per GTS descriptor
242 }
243
244 return size;
245}
246
249{
251
252 if (m_gtsSpecDescCount > 0)
253 {
254 uint8_t gtsDescStartAndLength;
256
257 for (int j = 0; j < m_gtsSpecDescCount; j++)
258 {
259 WriteTo(i, m_gtsList[j].m_gtsDescDevShortAddr);
260
261 gtsDescStartAndLength =
262 (m_gtsList[j].m_gtsDescStartSlot & 0x0F) | // GTS descriptor bits 16-19
263 (m_gtsList[j].m_gtsDescLength & 0xF0); // GTS descriptor bits 20-23
264
265 i.WriteU8(gtsDescStartAndLength);
266 }
267 }
268 return i;
269}
270
273{
274 uint8_t gtsSpecField = i.ReadU8();
275 SetGtsSpecField(gtsSpecField);
276
277 if (m_gtsSpecDescCount > 0)
278 {
279 uint8_t gtsDirectionField = i.ReadU8();
280 SetGtsDirectionField(gtsDirectionField);
281
282 uint8_t gtsDescStartAndLength;
283 for (int j = 0; j < m_gtsSpecDescCount; j++)
284 {
285 ReadFrom(i, m_gtsList[j].m_gtsDescDevShortAddr);
286
287 gtsDescStartAndLength = i.ReadU8();
288 m_gtsList[j].m_gtsDescStartSlot = (gtsDescStartAndLength) & (0x0F);
289 m_gtsList[j].m_gtsDescLength = (gtsDescStartAndLength >> 4) & (0x0F);
290 }
291 }
292 return i;
293}
294
295std::ostream&
296operator<<(std::ostream& os, const GtsFields& gtsFields)
297{
298 os << " GTS specification = " << uint32_t(gtsFields.GetGtsSpecField())
299 << ", GTS direction = " << uint32_t(gtsFields.GetGtsDirectionField());
300 return os;
301}
302
303/***********************************************************
304 * Pending Address Fields
305 ***********************************************************/
306
308{
311}
312
313uint8_t
315{
317}
318
319uint8_t
321{
323}
324
325uint8_t
327{
328 uint8_t pndAddrSpecField;
329
330 pndAddrSpecField = m_pndAddrSpecNumShortAddr & (0x07); // Bits 0-2
331 // Bit 3 (Reserved)
332 pndAddrSpecField |= (m_pndAddrSpecNumExtAddr << 4) & (0x07 << 4); // Bits 4-6
333 // Bit 7 (Reserved)
334
335 return pndAddrSpecField;
336}
337
338void
340{
341 uint8_t totalPendAddr = m_pndAddrSpecNumShortAddr + m_pndAddrSpecNumExtAddr;
342
343 if (totalPendAddr == 7)
344 {
345 return;
346 }
347 else
348 {
351 }
352}
353
354void
356{
357 uint8_t totalPendAddr = m_pndAddrSpecNumShortAddr + m_pndAddrSpecNumExtAddr;
358
359 if (totalPendAddr == 7)
360 {
361 return;
362 }
363 else
364 {
367 }
368}
369
370bool
372{
373 for (int j = 0; j <= m_pndAddrSpecNumShortAddr; j++)
374 {
375 if (shortAddr == m_shortAddrList[j])
376 {
377 return true;
378 }
379 }
380
381 return false;
382}
383
384bool
386{
387 for (int j = 0; j <= m_pndAddrSpecNumExtAddr; j++)
388 {
389 if (extAddr == m_extAddrList[j])
390 {
391 return true;
392 }
393 }
394
395 return false;
396}
397
398void
400{
401 m_pndAddrSpecNumShortAddr = (pndAddrSpecField) & (0x07); // Bit 0-2
402 // Bit 3
403 m_pndAddrSpecNumExtAddr = (pndAddrSpecField >> 4) & (0x07); // Bit 4-6
404 // Bit 7
405}
406
409{
410 uint32_t size;
411
412 size = 1; // 1 octet (Pending Address Specification Field)
413 size = size + (m_pndAddrSpecNumShortAddr * 2); // X octets (Short Pending Address List)
414 size = size + (m_pndAddrSpecNumExtAddr * 8); // X octets (Extended Pending Address List)
415
416 return size;
417}
418
421{
423
424 for (int j = 0; j < m_pndAddrSpecNumShortAddr; j++)
425 {
427 }
428
429 for (int k = 0; k < m_pndAddrSpecNumExtAddr; k++)
430 {
431 WriteTo(i, m_extAddrList[k]);
432 }
433
434 return i;
435}
436
439{
440 uint8_t pndAddrSpecField = i.ReadU8();
441
442 SetPndAddrSpecField(pndAddrSpecField);
443
444 for (int j = 0; j < m_pndAddrSpecNumShortAddr; j++)
445 {
447 }
448
449 for (int k = 0; k < m_pndAddrSpecNumExtAddr; k++)
450 {
451 ReadFrom(i, m_extAddrList[k]);
452 }
453
454 return i;
455}
456
457std::ostream&
458operator<<(std::ostream& os, const PendingAddrFields& pendingAddrFields)
459{
460 os << " Num. Short Addr = " << uint32_t(pendingAddrFields.GetNumShortAddr())
461 << ", Num. Ext Addr = " << uint32_t(pendingAddrFields.GetNumExtAddr());
462 return os;
463}
464
465/***********************************************************
466 * Capability Information Field
467 ***********************************************************/
468
470{
471 m_deviceType = true;
472 m_powerSource = false;
474 m_securityCap = false;
475 m_allocAddr = true;
476}
477
479{
480 SetCapability(bitmap);
481}
482
483uint8_t
485{
486 uint8_t capability;
487
488 capability = (m_reservedBit0) & (0x01);
489 capability |= (m_deviceType << 1) & (0x01 << 1);
490 capability |= (m_powerSource << 2) & (0x01 << 2);
491 capability |= (m_receiverOnWhenIdle << 3) & (0x01 << 3);
492 capability |= (m_reservedBit45 << 4) & (0x03 << 4);
493 capability |= (m_securityCap << 6) & (0x01 << 6);
494 capability |= (m_allocAddr << 7) & (0x01 << 7);
495
496 return capability;
497}
498
499void
501{
502 m_reservedBit0 = (bitmap) & (0x01);
503 m_deviceType = (bitmap >> 1) & (0x01);
504 m_powerSource = (bitmap >> 2) & (0x01);
505 m_receiverOnWhenIdle = (bitmap >> 3) & (0x01);
506 m_reservedBit45 = (bitmap >> 4) & (0x03);
507 m_securityCap = (bitmap >> 6) & (0x01);
508 m_allocAddr = (bitmap >> 7) & (0x01);
509}
510
511bool
513{
514 return m_deviceType;
515}
516
517bool
519{
520 return m_powerSource;
521}
522
523bool
525{
527}
528
529bool
531{
532 return m_securityCap;
533}
534
535bool
537{
538 return m_allocAddr;
539}
540
541void
543{
544 m_deviceType = devType;
545}
546
547void
549{
550 m_powerSource = pow;
551}
552
553void
555{
556 m_receiverOnWhenIdle = rxIdle;
557}
558
559void
561{
562 m_securityCap = sec;
563}
564
565void
567{
568 m_allocAddr = addrAlloc;
569}
570
579std::ostream&
580operator<<(std::ostream& os, const CapabilityField& capabilityField)
581{
582 os << " FFD device capable = " << bool(capabilityField.IsDeviceTypeFfd())
583 << ", Alternate Power Current Available = " << bool(capabilityField.IsPowSrcAvailable())
584 << ", Receiver On When Idle = " << bool(capabilityField.IsReceiverOnWhenIdle())
585 << ", Security Capable = " << bool(capabilityField.IsSecurityCapability())
586 << ", Coordinator Allocate Short Address = " << bool(capabilityField.IsShortAddrAllocOn());
587 return os;
588}
589
590} // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
Represent the Capability Information Field.
uint8_t GetCapability() const
Get the bitmap representing the device capability.
bool m_securityCap
Capability Information Field, Security Capability (bit 6)
void SetCapability(uint8_t bitmap)
Set the bitmap representing the device capability.
bool m_allocAddr
Capability Information Field, Allocate Address (bit 7)
void SetPowSrcAvailable(bool pow)
Set the Power Source available flag in the Capability Information Field.
void SetShortAddrAllocOn(bool addrAlloc)
Set the Short Address Flag in the Capability Information Field.
bool IsDeviceTypeFfd() const
True if the device type is a Full Functional Device (FFD) false if is a Reduced Functional Device (RF...
bool m_reservedBit0
Capability Information Field, Reserved (bit 0)
uint8_t m_reservedBit45
Capability Information Field, Reserved (bit 4 & 5)
void SetSecurityCap(bool sec)
Set the Security Capability flag in the Capability Information Field.
bool IsSecurityCapability() const
True if the device is capable of sending and receiving cryptographically protected MAC frames.
bool IsPowSrcAvailable() const
True if the device is receiving power from alternating current mains.
void SetRxOnWhenIdle(bool rxIdle)
Indicate if the receiver is On on Idle.
bool IsReceiverOnWhenIdle() const
True if the device does not disable its receiver to conserve power during idle periods.
bool IsShortAddrAllocOn() const
True if the device wishes the coordinator to allocate a short address as result of the association pr...
bool m_deviceType
Capability Information Field, Device Type (bit 1)
bool m_powerSource
Capability Information Field, Power Source (bit 2)
void SetFfdDevice(bool devType)
Set the Device type in the Capability Information Field.
bool m_receiverOnWhenIdle
Capability Information Field, Receiver On When Idle (bit 3)
Represent the GTS information fields.
void SetGtsSpecField(uint8_t gtsSpec)
Set the GTS Specification Field to the GTS Fields.
void SetGtsDirectionField(uint8_t gtsDir)
Set the GTS direction field to the GTS Fields.
GtsDescriptor m_gtsList[7]
GTS List field (maximum descriptors stored == 7)
bool GetGtsPermit() const
Get the GTS Specification Permit.
uint8_t m_gtsSpecPermit
GTS specification field GTS Permit (Bit 7)
uint8_t m_gtsSpecDescCount
GTS specification field Descriptor Count (Bit 0-2)
Buffer::Iterator Serialize(Buffer::Iterator i) const
Serialize the entire GTS fields.
uint32_t GetSerializedSize() const
Get the size of the serialized GTS fields.
uint8_t GetGtsDirectionField() const
Get the GTS Direction Field from the GTS Fields.
Buffer::Iterator Deserialize(Buffer::Iterator i)
Deserialize the entire GTS fields.
uint8_t m_gtsDirMask
GTS Direction field Directions Mask (Bit 0-6)
uint8_t GetGtsSpecField() const
Get the GTS Specification Field from the GTS Fields.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
an EUI-64 address
Definition: mac64-address.h:46
Represent the Pending Address Specification field.
std::array< Mac64Address, 7 > m_extAddrList
Pending Extended Address List.
Buffer::Iterator Deserialize(Buffer::Iterator i)
Deserialize the all the Pending Address Fields.
uint8_t GetNumExtAddr() const
Get the number of Extended Pending Address indicated in the Pending Address Specification Field.
uint32_t GetSerializedSize() const
Get the size of the serialized Pending Address Fields.
uint8_t GetPndAddrSpecField() const
Get the whole Pending Address Specification Field from the Pending Address Fields.
uint8_t m_pndAddrSpecNumShortAddr
Pending Address Specification field Number of Short Address (Bits 0-2) Pending Address Specification ...
uint8_t m_pndAddrSpecNumExtAddr
Pending Address Specification field Number of Extended Address (Bits 4-6) Pending Address Specificati...
std::array< Mac16Address, 7 > m_shortAddrList
Pending Short Address List.
bool SearchAddress(Mac16Address shortAddr)
Search for the short Pending Address in the Address List.
Buffer::Iterator Serialize(Buffer::Iterator i) const
Serialize the entire Pending Address Fields.
void AddAddress(Mac16Address shortAddr)
Add a short Pending Address to the Address List.
uint8_t GetNumShortAddr() const
Get the number of Short Pending Address indicated in the Pending Address Specification Field.
void SetPndAddrSpecField(uint8_t pndAddrSpecField)
Set the whole Pending Address Specification field.
Represent the Superframe Specification information field.
bool m_sspecBatLifeExt
Superframe Specification field Battery Life Extension (Bit 12)
bool m_sspecAssocPermit
Superframe Specification field Association Permit (Bit 15)
uint8_t GetFinalCapSlot() const
Get the the Final CAP Slot.
uint16_t GetSuperframe() const
Get the Superframe specification information field.
bool m_sspecPanCoor
Superframe Specification field PAN Coordinator (Bit 14)
uint8_t m_sspecFnlCapSlot
Superframe Specification field Final CAP slot (Bit 8-11)
void SetAssocPermit(bool assocPermit)
Set the Superframe Specification Association Permit field.
uint8_t m_sspecBcnOrder
Superframe Specification field Beacon Order (Bit 0-3)
void SetBattLifeExt(bool battLifeExt)
Set the Superframe Specification Battery Life Extension (BLE).
uint8_t GetBeaconOrder() const
Get the Superframe Specification Beacon Order field.
uint8_t m_sspecSprFrmOrder
Superframe Specification field Superframe Order (Bit 4-7)
bool IsBattLifeExt() const
Check if the Battery Life Extension bit is enabled.
void SetFinalCapSlot(uint8_t capSlot)
Set the superframe specification Final CAP slot field.
void SetSuperframeOrder(uint8_t frmOrder)
Set the superframe specification Superframe Order field.
void SetPanCoor(bool panCoor)
Set the Superframe Specification PAN coordinator field.
void SetBeaconOrder(uint8_t bcnOrder)
Set the superframe specification Beacon Order field.
bool IsPanCoor() const
Check if the PAN Coordinator bit is enabled.
bool IsAssocPermit() const
Check if the Association Permit bit is enabled.
uint8_t GetFrameOrder() const
Get the Superframe Specification Frame Order field.
void SetSuperframe(uint16_t superFrm)
Set the whole Superframe Specification Information field.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
uint8_t m_gtsDescLength
GTS Descriptor GTS Length (Bit 20-23)
uint8_t m_gtsDescStartSlot
GTS Descriptor GTS Starting Slot(Bit 16-19)