A Discrete-Event Network Simulator
API
lr-wpan-fields.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan.
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: Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
19  */
20 
21 
22 
23 #include "lr-wpan-fields.h"
24 #include <ns3/log.h>
25 #include <ns3/address-utils.h>
26 
27 namespace ns3 {
28 
30 {
31  SetBeaconOrder (15);
32  SetSuperframeOrder (15);
33  SetFinalCapSlot (0);
34  SetBattLifeExt (false);
35  SetPanCoor (false);
36  SetAssocPermit (false);
37 }
38 
39 void
40 SuperframeField::SetSuperframe (uint16_t superFrmSpec)
41 {
42  m_sspecBcnOrder = (superFrmSpec) & (0x0F); //Bits 0-3
43  m_sspecSprFrmOrder = (superFrmSpec >> 4) & (0x0F); //Bits 4-7
44  m_sspecFnlCapSlot = (superFrmSpec >> 8) & (0x0F); //Bits 8-11
45  m_sspecBatLifeExt = (superFrmSpec >> 12) & (0x01); //Bit 12
46  //Bit 13 (Reserved)
47  m_sspecPanCoor = (superFrmSpec >> 14) & (0x01); //Bit 14
48  m_sspecAssocPermit = (superFrmSpec >> 15) & (0x01); //Bit 15
49 }
50 
51 void
53 {
54  if (bcnOrder > 15)
55  {
56  std::cout << "SuperframeField Beacon Order value must be 15 or less\n";
57  }
58  else
59  {
60  m_sspecBcnOrder = bcnOrder;
61  }
62 }
63 
64 void
66 {
67  if (frmOrder > 15)
68  {
69  std::cout << "SuperframeField Frame Order value must be 15 or less\n";
70  }
71  else
72  {
73  m_sspecSprFrmOrder = frmOrder;
74  }
75 }
76 
77 void
79 {
80  if (capSlot > 15)
81  {
82  std::cout << "The final slot cannot greater than the slots in a CAP (15)\n";
83  }
84  else
85  {
86  m_sspecFnlCapSlot = capSlot;
87  }
88 }
89 
90 void
92 {
93  m_sspecBatLifeExt = battLifeExt;
94 }
95 
96 void
98 {
99  m_sspecPanCoor = panCoor;
100 }
101 
102 void
104 {
105  m_sspecAssocPermit = assocPermit;
106 }
107 
108 uint8_t
110 {
111  return m_sspecBcnOrder;
112 }
113 
114 uint8_t
116 {
117  return m_sspecSprFrmOrder;
118 }
119 
120 uint8_t
122 {
123  return m_sspecFnlCapSlot;
124 }
125 
126 bool
128 {
129  return m_sspecBatLifeExt;
130 }
131 
132 bool
134 {
135  return m_sspecPanCoor;
136 }
137 
138 bool
140 {
141  return m_sspecAssocPermit;
142 }
143 
144 uint16_t
146 {
147  uint16_t superframe;
148 
149  superframe = m_sspecBcnOrder & (0x0F); // Bits 0-3
150  superframe |= (m_sspecSprFrmOrder << 4) & (0x0F << 4); // Bits 4-7
151  superframe |= (m_sspecFnlCapSlot << 8) & (0x0F << 8); // Bits 8-11
152  superframe |= (m_sspecBatLifeExt << 12) & (0x01 << 12); // Bit 12
153  // Bit 13 (Reserved)
154  superframe |= (m_sspecPanCoor << 14) & (0x01 << 14); // Bit 14
155  superframe |= (m_sspecAssocPermit << 15) & (0x01 << 15); // Bit 15
156 
157  return superframe;
158 }
159 
160 uint32_t
162 {
163  return 2; // 2 Octets (superframeSpec)
164 }
165 
168 {
170  return i;
171 }
172 
175 {
176  uint16_t superframe = i.ReadLsbtohU16 ();
177  SetSuperframe (superframe);
178 
179  return i;
180 }
181 
190 std::ostream &
191 operator << (std::ostream &os, const SuperframeField &superframeField)
192 {
193  os << " Beacon Order = " << uint32_t (superframeField.GetBeaconOrder ())
194  << ", Frame Order = " << uint32_t (superframeField.GetFrameOrder ())
195  << ", Final CAP slot = " << uint32_t (superframeField.GetFinalCapSlot ())
196  << ", Battery Life Ext = " << bool (superframeField.IsBattLifeExt ())
197  << ", PAN Coordinator = " << bool (superframeField.IsPanCoor ())
198  << ", Association Permit = " << bool (superframeField.IsAssocPermit ());
199  return os;
200 }
201 
202 /***********************************************************
203  * Guaranteed Time Slots (GTS) Fields
204  ***********************************************************/
205 
207 {
208  // GTS Specification Field
209  m_gtsSpecDescCount = 0;
210  m_gtsSpecPermit = 0;
211  // GTS Direction Field
212  m_gtsDirMask = 0;
213 }
214 
215 uint8_t
217 {
218  uint8_t gtsSpecField;
219 
220  gtsSpecField = m_gtsSpecDescCount & (0x07); // Bits 0-2
221  // Bits 3-6 (Reserved)
222  gtsSpecField |= (m_gtsSpecPermit << 7) & (0x01 << 7); // Bit 7
223 
224  return gtsSpecField;
225 }
226 
227 uint8_t
229 {
230  uint8_t gtsDirectionField;
231 
232  gtsDirectionField = m_gtsDirMask & (0x7F); // Bit 0-6
233  // Bit 7 (Reserved)
234  return gtsDirectionField;
235 }
236 
237 void
238 GtsFields::SetGtsSpecField (uint8_t gtsSpec)
239 {
240  m_gtsSpecDescCount = (gtsSpec) & (0x07); // Bits 0-2
241  // Bits 3-6 (Reserved)
242  m_gtsSpecPermit = (gtsSpec >> 7) & (0x01); // Bit 7
243 }
244 
245 void
247 {
248  m_gtsDirMask = (gtsDir) & (0x7F); // Bits 0-6
249  // Bit 7 (Reserved)
250 }
251 
252 uint32_t
254 {
255  uint32_t size;
256 
257  size = 1; // 1 octet GTS Specification Field
258  if (m_gtsSpecDescCount > 0)
259  {
260  size += 1; // 1 octet GTS Direction Field
261  size += (m_gtsSpecDescCount * 3); // 3 octets per GTS descriptor
262  }
263 
264  return size;
265 }
266 
269 {
270  i.WriteU8 (GetGtsSpecField ());
271 
272  if (m_gtsSpecDescCount > 0)
273  {
274  uint8_t gtsDescStartAndLenght;
276 
277  for (int j = 0; j < m_gtsSpecDescCount; j++)
278  {
279  WriteTo (i,m_gtsList[j].m_gtsDescDevShortAddr);
280 
281  gtsDescStartAndLenght = m_gtsList[j].m_gtsDescStartSlot & (0x0F);
282  gtsDescStartAndLenght = (m_gtsList[j].m_gtsDescLength << 4) & (0x0F);
283 
284  i.WriteU8 (gtsDescStartAndLenght);
285  }
286  }
287  return i;
288 }
289 
292 {
293 
294  uint8_t gtsSpecField = i.ReadU8 ();
295  SetGtsSpecField (gtsSpecField);
296 
297  if (m_gtsSpecDescCount > 0)
298  {
299  uint8_t gtsDirectionField = i.ReadU8 ();
300  SetGtsDirectionField (gtsDirectionField);
301 
302  uint8_t gtsDescStartAndLenght;
303  for (int j = 0; j < m_gtsSpecDescCount; j++)
304  {
305  ReadFrom (i, m_gtsList[j].m_gtsDescDevShortAddr);
306 
307  gtsDescStartAndLenght = i.ReadU8 ();
308  m_gtsList[j].m_gtsDescStartSlot = (gtsDescStartAndLenght) & (0x0F);
309  m_gtsList[j].m_gtsDescLength = (gtsDescStartAndLenght >> 4) & (0x0F);
310  }
311  }
312  return i;
313 }
314 
315 
316 /***********************************************************
317  * Pending Address Fields
318  ***********************************************************/
319 
321 {
324 }
325 
326 
327 uint8_t
329 {
331 }
332 
333 
334 uint8_t
336 {
338 }
339 
340 uint8_t
342 {
343  uint8_t pndAddrSpecField;
344 
345  pndAddrSpecField = m_pndAddrSpecNumShortAddr & (0x07); // Bits 0-2
346  // Bit 3 (Reserved)
347  pndAddrSpecField |= (m_pndAddrSpecNumExtAddr << 4) & (0x07 << 4); // Bits 4-6
348  // Bit 7 (Reserved)
349 
350  return pndAddrSpecField;
351 }
352 
353 void
355 {
356  uint8_t totalPendAddr = m_pndAddrSpecNumShortAddr + m_pndAddrSpecNumExtAddr;
357 
358  if (totalPendAddr == 7)
359  {
360  return;
361  }
362  else
363  {
366  }
367 }
368 
369 void
371 {
372  uint8_t totalPendAddr = m_pndAddrSpecNumShortAddr + m_pndAddrSpecNumExtAddr;
373 
374  if (totalPendAddr == 7)
375  {
376  return;
377  }
378  else
379  {
382  }
383 }
384 
385 bool
387 {
388  for (int j = 0; j <= m_pndAddrSpecNumShortAddr; j++)
389  {
390  if (shortAddr == m_shortAddrList[j])
391  {
392  return true;
393  }
394  }
395 
396  return false;
397 }
398 
399 
400 bool
402 {
403  for (int j = 0; j <= m_pndAddrSpecNumExtAddr; j++)
404  {
405  if (extAddr == m_extAddrList[j])
406  {
407  return true;
408  }
409  }
410 
411  return false;
412 }
413 
414 
415 void
416 PendingAddrFields::SetPndAddrSpecField (uint8_t pndAddrSpecField)
417 {
418  m_pndAddrSpecNumShortAddr = (pndAddrSpecField) & (0x07); // Bit 0-2
419  // Bit 3
420  m_pndAddrSpecNumExtAddr = (pndAddrSpecField >> 4) & (0x07); // Bit 4-6
421  // Bit 7
422 }
423 
424 
425 uint32_t
427 {
428  uint32_t size;
429 
430  size = 1; // 1 octet (Pending Address Specification Field)
431  size = size + (m_pndAddrSpecNumShortAddr * 2); // X octets (Short Pending Address List)
432  size = size + (m_pndAddrSpecNumExtAddr * 8); // X octets (Extended Pending Address List)
433 
434  return size;
435 }
436 
439 {
441 
442  for (int j = 0; j < m_pndAddrSpecNumShortAddr; j++)
443  {
444  WriteTo (i,m_shortAddrList[j]);
445  }
446 
447  for (int k = 0; k < m_pndAddrSpecNumExtAddr; k++ )
448  {
449  WriteTo (i,m_extAddrList[k]);
450  }
451 
452  return i;
453 }
454 
457 {
458  uint8_t pndAddrSpecField = i.ReadU8 ();
459 
460  SetPndAddrSpecField (pndAddrSpecField);
461 
462  for (int j = 0; j < m_pndAddrSpecNumShortAddr; j++)
463  {
464  ReadFrom (i, m_shortAddrList[j]);
465  }
466 
467  for (int k = 0; k < m_pndAddrSpecNumExtAddr; k++)
468  {
469  ReadFrom (i, m_extAddrList[k]);
470  }
471 
472  return i;
473 }
474 
483 std::ostream &
484 operator << (std::ostream &os, const PendingAddrFields &pendingAddrFields)
485 {
486  os << " Num. Short Addr = " << uint32_t (pendingAddrFields.GetNumShortAddr ())
487  << ", Num. Ext Addr = " << uint32_t (pendingAddrFields.GetNumExtAddr ());
488  return os;
489 }
490 
491 } // ns-3 namespace
Buffer::Iterator Deserialize(Buffer::Iterator i)
Deserialize the entire GTS fields.
void SetBeaconOrder(uint8_t bcnOrder)
Set the superframe specification Beacon Order field.
Buffer::Iterator Deserialize(Buffer::Iterator i)
Deserialize the all the Pending Address Fields.
Buffer::Iterator Serialize(Buffer::Iterator i) const
Serialize the entire Pending Address Fields.
uint32_t GetSerializedSize(void) const
Get the size of the serialized GTS fields.
uint8_t m_gtsDescStartSlot
GTS Descriptor GTS Starting Slot(Bit 16-19)
uint8_t GetFinalCapSlot(void) const
Check if the Final CAP Slot bit is enabled.
uint8_t GetBeaconOrder(void) const
Get the Superframe Specification Beacon Order field.
void SetGtsDirectionField(uint8_t gtsDir)
Set the GTS direction field to the GTS Fields gtsDir The GTS Direction Field to set.
uint8_t GetNumShortAddr(void) const
Get the number of Short Pending Address indicated in the Pending Address Specification Field...
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
uint8_t m_gtsDescLength
GTS Descriptor GTS Length (Bit 20-23)
std::array< Mac64Address, 7 > m_extAddrList
Pending Extended Address List.
std::array< Mac16Address, 7 > m_shortAddrList
Pending Short Address List.
uint16_t GetSuperframe(void) const
Get the Superframe specification information field.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
bool IsBattLifeExt(void) const
Check if the Battery Life Extension bit is enabled.
iterator in a Buffer instance
Definition: buffer.h:98
an EUI-64 address
Definition: mac64-address.h:43
void SetPndAddrSpecField(uint8_t pndAddrSpecField)
Set the whole Pending Address Specification field.
void SetSuperframeOrder(uint8_t frmOrder)
Set the superframe specification Superframe Order field.
Buffer::Iterator Deserialize(Buffer::Iterator i)
Deserialize the entire superframe specification field.
uint32_t GetSerializedSize(void) const
Get the size of the serialized Superframe specification information field.
uint8_t GetGtsSpecField(void) const
Get the GTS Specification Field from the GTS Fields.
bool m_sspecPanCoor
Superframe Specification field PAN Coordinator (Bit 14)
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t m_gtsDirMask
GTS Direction field Directions Mask (Bit 0-6) GTS Direction field Reserved (Not Necessary) (Bit 7) ...
uint8_t GetFrameOrder(void) const
Get the Superframe Specification Frame Order field.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
Represent the Superframe Specification information field.
uint8_t m_gtsSpecDescCount
GTS specification field Descriptor Count (Bit 0-2) GTS specification field Reserved (Not necessary) (...
uint8_t m_sspecSprFrmOrder
Superframe Specification field Superframe Order (Bit 4-7)
uint8_t m_sspecBcnOrder
Superframe Specification field Beacon Order (Bit 0-3)
void SetSuperframe(uint16_t superFrm)
Set the whole Superframe Specification Information field.
gtsDescriptor m_gtsList[6]
GTS List field (maximum descriptors stored == 7)
void WriteHtolsbU16(uint16_t data)
Definition: buffer.cc:910
void SetFinalCapSlot(uint8_t capSlot)
Set the superframe specification Final CAP slot field.
uint8_t GetPndAddrSpecField(void) const
Get the whole Pending Address Specification Field from the Pending Address Fields.
bool m_sspecBatLifeExt
Superframe Specification field Battery Life Extension (Bit 12) Superframe Specification field Reserve...
Buffer::Iterator Serialize(Buffer::Iterator i) const
Serialize the entire superframe specification field.
void SetBattLifeExt(bool battLifeExt)
Set the Superframe Specification Battery Life Extension (BLE).
void WriteU8(uint8_t data)
Definition: buffer.h:869
void SetGtsSpecField(uint8_t gtsSpec)
Set the GTS Specification Field to the GTS Fields gtsSpec The GTS Specification Field to set...
uint8_t m_pndAddrSpecNumShortAddr
Pending Address Specification field Number of Short Address (Bits 0-2) Pending Address Specification ...
uint8_t m_gtsSpecPermit
GTS specification field GTS Permit (Bit 7)
uint8_t GetNumExtAddr(void) const
Get the number of Extended Pending Address indicated in the Pending Address Specification Field...
uint8_t m_sspecFnlCapSlot
Superframe Specification field Final CAP slot (Bit 8-11)
bool m_sspecAssocPermit
Superframe Specification field Association Permit (Bit 15)
bool IsAssocPermit(void) const
Check if the Association Permit bit is enabled.
uint8_t GetGtsDirectionField(void) const
Get the GTS Direction Field from the GTS Fields.
void AddAddress(Mac16Address shortAddr)
Add a short Pending Address to the Address List.
uint8_t ReadU8(void)
Definition: buffer.h:1021
uint16_t ReadLsbtohU16(void)
Definition: buffer.cc:1065
uint32_t GetSerializedSize(void) const
Get the size of the serialized Pending Address Fields.
void SetPanCoor(bool panCoor)
Set the Superframe Specification PAN coordinator field.
void SetAssocPermit(bool assocPermit)
Set the Superframe Specification Association Permit field.
bool SearchAddress(Mac16Address shortAddr)
Search for the short Pending Address in the Address List.
Represent the Pending Address Specification field.
Buffer::Iterator Serialize(Buffer::Iterator i) const
Serialize the entire GTS fields.
uint8_t m_pndAddrSpecNumExtAddr
Pending Address Specification field Number of Extended Address (Bits 4-6) Pending Address Specificati...
bool IsPanCoor(void) const
Check if the PAN Coordinator bit is enabled.