A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-mac.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors:
7 * Gary Pei <guangyu.pei@boeing.com>
8 * kwong yin <kwong-sang.yin@boeing.com>
9 * Tom Henderson <thomas.r.henderson@boeing.com>
10 * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
11 * Erwan Livolant <erwan.livolant@inria.fr>
12 * Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
13 */
14#include "lr-wpan-mac.h"
15
16#include "lr-wpan-constants.h"
17#include "lr-wpan-csmaca.h"
18#include "lr-wpan-mac-header.h"
20#include "lr-wpan-mac-trailer.h"
21
22#include "ns3/double.h"
23#include "ns3/log.h"
24#include "ns3/node.h"
25#include "ns3/packet.h"
26#include "ns3/random-variable-stream.h"
27#include "ns3/simulator.h"
28#include "ns3/uinteger.h"
29
30#undef NS_LOG_APPEND_CONTEXT
31#define NS_LOG_APPEND_CONTEXT \
32 std::clog << "[" << m_shortAddress << " | " << m_macExtendedAddress << "] ";
33
34namespace ns3
35{
36namespace lrwpan
37{
38
39NS_LOG_COMPONENT_DEFINE("LrWpanMac");
41
42std::ostream&
43operator<<(std::ostream& os, const MacState& state)
44{
45 switch (state)
46 {
48 os << "MAC IDLE";
49 break;
51 os << "CSMA";
52 break;
54 os << "SENDING";
55 break;
57 os << "ACK PENDING";
58 break;
60 os << "CHANNEL_ACCESS_FAILURE";
61 break;
63 os << "CHANNEL IDLE";
64 break;
66 os << "SET PHY to TX ON";
67 break;
69 os << "MAC GTS PERIOD";
70 break;
72 os << "SUPERFRAME INACTIVE PERIOD";
73 break;
75 os << "CSMA DEFERRED TO NEXT PERIOD";
76 break;
77 }
78 return os;
79}
80
83{
84 static TypeId tid =
85 TypeId("ns3::lrwpan::LrWpanMac")
86 .AddDeprecatedName("ns3::LrWpanMac")
88 .SetGroupName("LrWpan")
89 .AddConstructor<LrWpanMac>()
90 .AddAttribute("PanId",
91 "16-bit identifier of the associated PAN",
95 .AddTraceSource("MacTxEnqueue",
96 "Trace source indicating a packet has been "
97 "enqueued in the transaction queue",
99 "ns3::Packet::TracedCallback")
100 .AddTraceSource("MacTxDequeue",
101 "Trace source indicating a packet has was "
102 "dequeued from the transaction queue",
104 "ns3::Packet::TracedCallback")
105 .AddTraceSource("MacIndTxEnqueue",
106 "Trace source indicating a packet has been "
107 "enqueued in the indirect transaction queue",
109 "ns3::Packet::TracedCallback")
110 .AddTraceSource("MacIndTxDequeue",
111 "Trace source indicating a packet has was "
112 "dequeued from the indirect transaction queue",
114 "ns3::Packet::TracedCallback")
115 .AddTraceSource("MacTx",
116 "Trace source indicating a packet has "
117 "arrived for transmission by this device",
119 "ns3::Packet::TracedCallback")
120 .AddTraceSource("MacTxOk",
121 "Trace source indicating a packet has been "
122 "successfully sent",
124 "ns3::Packet::TracedCallback")
125 .AddTraceSource("MacTxDrop",
126 "Trace source indicating a packet has been "
127 "dropped during transmission",
129 "ns3::Packet::TracedCallback")
130 .AddTraceSource("MacIndTxDrop",
131 "Trace source indicating a packet has been "
132 "dropped from the indirect transaction queue"
133 "(The pending transaction list)",
135 "ns3::Packet::TracedCallback")
136 .AddTraceSource("MacPromiscRx",
137 "A packet has been received by this device, "
138 "has been passed up from the physical layer "
139 "and is being forwarded up the local protocol stack. "
140 "This is a promiscuous trace,",
142 "ns3::Packet::TracedCallback")
143 .AddTraceSource("MacRx",
144 "A packet has been received by this device, "
145 "has been passed up from the physical layer "
146 "and is being forwarded up the local protocol stack. "
147 "This is a non-promiscuous trace,",
149 "ns3::Packet::TracedCallback")
150 .AddTraceSource("MacRxDrop",
151 "Trace source indicating a packet was received, "
152 "but dropped before being forwarded up the stack",
154 "ns3::Packet::TracedCallback")
155 .AddTraceSource("Sniffer",
156 "Trace source simulating a non-promiscuous "
157 "packet sniffer attached to the device",
159 "ns3::Packet::TracedCallback")
160 .AddTraceSource("PromiscSniffer",
161 "Trace source simulating a promiscuous "
162 "packet sniffer attached to the device",
164 "ns3::Packet::TracedCallback")
165 .AddTraceSource("MacStateValue",
166 "The state of LrWpan Mac",
168 "ns3::TracedValueCallback::LrWpanMacState")
169 .AddTraceSource("MacIncSuperframeStatus",
170 "The period status of the incoming superframe",
172 "ns3::TracedValueCallback::SuperframeState")
173 .AddTraceSource("MacOutSuperframeStatus",
174 "The period status of the outgoing superframe",
176 "ns3::TracedValueCallback::SuperframeState")
177 .AddTraceSource("MacState",
178 "The state of LrWpan Mac",
180 "ns3::lrwpan::LrWpanMac::StateTracedCallback")
181 .AddTraceSource("MacSentPkt",
182 "Trace source reporting some information about "
183 "the sent packet",
185 "ns3::lrwpan::LrWpanMac::SentTracedCallback")
186 .AddTraceSource("IfsEnd",
187 "Trace source reporting the end of an "
188 "Interframe space (IFS)",
190 "ns3::Packet::TracedCallback");
191 return tid;
192}
193
195{
196 // First set the state to a known value, call ChangeMacState to fire trace source.
198
200
203
204 m_macRxOnWhenIdle = true;
205 m_macPanId = 0xffff;
207 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
210 m_macPromiscuousMode = false;
214 m_txPkt = nullptr;
215 m_rxPkt = nullptr;
217 m_ifs = 0;
218
219 m_macLIFSPeriod = 40;
220 m_macSIFSPeriod = 12;
221
222 m_panCoor = false;
223 m_coor = false;
224 m_macBeaconOrder = 15;
226 m_macTransactionPersistenceTime = 500; // 0x01F5
228 m_macAutoRequest = true;
229
232 m_beaconTrackingOn = false;
234
238
241
242 m_maxTxQueueSize = m_txQueue.max_size();
244
246 m_macDsn = SequenceNumber8(m_uniformVar->GetInteger(0, 255));
247 m_macBsn = SequenceNumber8(m_uniformVar->GetInteger(0, 255));
250 m_shortAddress = Mac16Address("FF:FF"); // FF:FF = The address is not assigned.
251}
252
257
258void
260{
261 NS_LOG_FUNCTION(this);
262
264 {
265 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
266 }
267 else
268 {
269 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TRX_OFF);
270 }
271
273}
274
275void
277{
278 NS_LOG_FUNCTION(this);
279
280 if (m_csmaCa)
281 {
282 m_csmaCa->Dispose();
283 m_csmaCa = nullptr;
284 }
285 m_txPkt = nullptr;
286
287 for (uint32_t i = 0; i < m_txQueue.size(); i++)
288 {
289 m_txQueue[i]->txQPkt = nullptr;
290 }
291 m_txQueue.clear();
292
293 for (uint32_t i = 0; i < m_indTxQueue.size(); i++)
294 {
295 m_indTxQueue[i]->txQPkt = nullptr;
296 }
297 m_indTxQueue.clear();
298
299 m_uniformVar = nullptr;
300 m_phy = nullptr;
313
314 m_panDescriptorList.clear();
315 m_energyDetectList.clear();
316 m_unscannedChannels.clear();
317
323
325}
326
327bool
329{
330 return m_macRxOnWhenIdle;
331}
332
333void
335{
336 NS_LOG_FUNCTION(this << rxOnWhenIdle);
337 m_macRxOnWhenIdle = rxOnWhenIdle;
338
339 if (m_macState == MAC_IDLE)
340 {
342 {
343 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
344 }
345 else
346 {
347 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TRX_OFF);
348 }
349 }
350}
351
352void
357
358void
363
366{
367 return m_shortAddress;
368}
369
375
376void
378{
379 NS_LOG_FUNCTION(this << p);
380
381 McpsDataConfirmParams confirmParams;
382 confirmParams.m_msduHandle = params.m_msduHandle;
383
384 // TODO: We need a drop trace for the case that the packet is too large or the request
385 // parameters are maleformed.
386 // The current tx drop trace is not suitable, because packets dropped using this trace
387 // carry the mac header and footer, while packets being dropped here do not have them.
388
390 m_macDsn++;
391
393 {
394 // Note, this is just testing maximum theoretical frame size per the spec
395 // The frame could still be too large once headers are put on
396 // in which case the phy will reject it instead
397 NS_LOG_ERROR(this << " packet too big: " << p->GetSize());
398 confirmParams.m_status = MacStatus::FRAME_TOO_LONG;
400 {
401 m_mcpsDataConfirmCallback(confirmParams);
402 }
403 return;
404 }
405
406 if ((params.m_srcAddrMode == NO_PANID_ADDR) && (params.m_dstAddrMode == NO_PANID_ADDR))
407 {
408 NS_LOG_ERROR(this << " Can not send packet with no Address field");
409 confirmParams.m_status = MacStatus::INVALID_ADDRESS;
411 {
412 m_mcpsDataConfirmCallback(confirmParams);
413 }
414 return;
415 }
416 switch (params.m_srcAddrMode)
417 {
418 case NO_PANID_ADDR:
419 macHdr.SetSrcAddrMode(params.m_srcAddrMode);
420 macHdr.SetNoPanIdComp();
421 break;
423 NS_ABORT_MSG("Can not set source address type to ADDR_MODE_RESERVED. Aborting.");
424 break;
425 case SHORT_ADDR:
426 macHdr.SetSrcAddrMode(params.m_srcAddrMode);
428 break;
429 case EXT_ADDR:
430 macHdr.SetSrcAddrMode(params.m_srcAddrMode);
432 break;
433 default:
434 NS_LOG_ERROR(this << " Can not send packet with incorrect Source Address mode = "
435 << params.m_srcAddrMode);
436 confirmParams.m_status = MacStatus::INVALID_ADDRESS;
438 {
439 m_mcpsDataConfirmCallback(confirmParams);
440 }
441 return;
442 }
443 switch (params.m_dstAddrMode)
444 {
445 case NO_PANID_ADDR:
446 macHdr.SetDstAddrMode(params.m_dstAddrMode);
447 macHdr.SetNoPanIdComp();
448 break;
450 NS_ABORT_MSG("Can not set destination address type to ADDR_MODE_RESERVED. Aborting.");
451 break;
452 case SHORT_ADDR:
453 macHdr.SetDstAddrMode(params.m_dstAddrMode);
454 macHdr.SetDstAddrFields(params.m_dstPanId, params.m_dstAddr);
455 break;
456 case EXT_ADDR:
457 macHdr.SetDstAddrMode(params.m_dstAddrMode);
458 macHdr.SetDstAddrFields(params.m_dstPanId, params.m_dstExtAddr);
459 break;
460 default:
461 NS_LOG_ERROR(this << " Can not send packet with incorrect Destination Address mode = "
462 << params.m_dstAddrMode);
463 confirmParams.m_status = MacStatus::INVALID_ADDRESS;
465 {
466 m_mcpsDataConfirmCallback(confirmParams);
467 }
468 return;
469 }
470
471 // IEEE 802.15.4-2006 (7.5.6.1)
472 // Src & Dst PANs are identical, PAN compression is ON
473 // only the dst PAN is serialized making the MAC header 2 bytes smaller
474 if ((params.m_dstAddrMode != NO_PANID_ADDR && params.m_srcAddrMode != NO_PANID_ADDR) &&
475 (macHdr.GetDstPanId() == macHdr.GetSrcPanId()))
476 {
477 macHdr.SetPanIdComp();
478 }
479
480 macHdr.SetSecDisable();
481 // extract the first 3 bits in TxOptions
482 int b0 = params.m_txOptions & TX_OPTION_ACK;
483 int b1 = params.m_txOptions & TX_OPTION_GTS;
484 int b2 = params.m_txOptions & TX_OPTION_INDIRECT;
485
486 if (b0 == TX_OPTION_ACK)
487 {
488 // Set AckReq bit only if the destination is not the broadcast address.
489 if (macHdr.GetDstAddrMode() == SHORT_ADDR)
490 {
491 // short address and ACK requested.
492 Mac16Address shortAddr = macHdr.GetShortDstAddr();
493 if (shortAddr.IsBroadcast() || shortAddr.IsMulticast())
494 {
495 NS_LOG_LOGIC("LrWpanMac::McpsDataRequest: requested an ACK on broadcast or "
496 "multicast destination ("
497 << shortAddr << ") - forcefully removing it.");
498 macHdr.SetNoAckReq();
499 params.m_txOptions &= ~uint8_t(TX_OPTION_ACK);
500 }
501 else
502 {
503 macHdr.SetAckReq();
504 }
505 }
506 else
507 {
508 // other address (not short) and ACK requested
509 macHdr.SetAckReq();
510 }
511 }
512 else
513 {
514 macHdr.SetNoAckReq();
515 }
516
517 if (b1 == TX_OPTION_GTS)
518 {
519 // TODO:GTS Transmission
520 }
521 else if (b2 == TX_OPTION_INDIRECT)
522 {
523 // Indirect Tx
524 // A COORDINATOR will save the packet in the pending queue and await for data
525 // requests from its associated devices. The devices are aware of pending data,
526 // from the pending bit information extracted from the received beacon.
527 // A DEVICE must be tracking beacons (MLME-SYNC.request is running) before attempting
528 // request data from the coordinator.
529
530 // Indirect Transmission can only be done by PAN coordinator or coordinators.
532 p->AddHeader(macHdr);
533
534 LrWpanMacTrailer macTrailer;
535 // Calculate FCS if the global attribute ChecksumEnabled is set.
537 {
538 macTrailer.EnableFcs(true);
539 macTrailer.SetFcs(p);
540 }
541 p->AddTrailer(macTrailer);
542
543 NS_LOG_ERROR(this << " Indirect transmissions not currently supported");
544 // Note: The current Pending transaction list should work for indirect transmissions.
545 // However, this is not tested yet. For now, we block the use of indirect transmissions.
546 // TODO: Save packet in the Pending Transaction list.
547 // EnqueueInd (p);
548 }
549 else
550 {
551 // Direct Tx
552 // From this point the packet will be pushed to a Tx queue and immediately
553 // use a slotted (beacon-enabled) or unslotted (nonbeacon-enabled) version of CSMA/CA
554 // before sending the packet, depending on whether it has previously
555 // received a valid beacon or not.
556
557 p->AddHeader(macHdr);
558
559 LrWpanMacTrailer macTrailer;
560 // Calculate FCS if the global attribute ChecksumEnabled is set.
562 {
563 macTrailer.EnableFcs(true);
564 macTrailer.SetFcs(p);
565 }
566 p->AddTrailer(macTrailer);
567
569 txQElement->txQMsduHandle = params.m_msduHandle;
570 txQElement->txQPkt = p;
571 EnqueueTxQElement(txQElement);
572 CheckQueue();
573 }
574}
575
576void
578{
579 NS_LOG_FUNCTION(this);
581
582 MlmeStartConfirmParams confirmParams;
583
584 if (GetShortAddress() == Mac16Address("ff:ff"))
585 {
586 NS_LOG_ERROR(this << " Invalid MAC short address");
589 {
590 m_mlmeStartConfirmCallback(confirmParams);
591 }
592 return;
593 }
594
595 if ((params.m_bcnOrd > 15) || (params.m_sfrmOrd > params.m_bcnOrd))
596 {
599 {
600 m_mlmeStartConfirmCallback(confirmParams);
601 }
602 NS_LOG_ERROR(this << "Incorrect superframe order or beacon order.");
603 return;
604 }
605
606 // Mark primitive as pending and save the start params while the new page and channel is set.
608 m_startParams = params;
609
611 pibAttr->phyCurrentPage = m_startParams.m_logChPage;
612 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentPage, pibAttr);
613}
614
615void
617{
618 NS_LOG_FUNCTION(this);
619
620 MlmeScanConfirmParams confirmParams;
621 confirmParams.m_scanType = params.m_scanType;
622 confirmParams.m_chPage = params.m_chPage;
623
625 {
627 {
629 m_mlmeScanConfirmCallback(confirmParams);
630 }
631 NS_LOG_ERROR(this << " A channel scan is already in progress");
632 return;
633 }
634
635 if (params.m_scanDuration > 14 || params.m_scanType > MLMESCAN_ORPHAN)
636 {
638 {
640 m_mlmeScanConfirmCallback(confirmParams);
641 }
642 NS_LOG_ERROR(this << "Invalid scan duration or unsupported scan type");
643 return;
644 }
645 // Temporary store macPanId and set macPanId to 0xFFFF to accept all beacons.
647 m_macPanId = 0xFFFF;
648
649 m_panDescriptorList.clear();
650 m_energyDetectList.clear();
651 m_unscannedChannels.clear();
652
653 // TODO: stop beacon transmission
654
655 // Cancel any ongoing CSMA/CA operations and set to unslotted mode for scan
656 m_csmaCa->Cancel();
662 m_csmaCa->SetUnSlottedCsmaCa();
663
665
666 // Mark primitive as pending and save the scan params while the new page and/or channel is set.
667 m_scanParams = params;
669
671 pibAttr->phyCurrentPage = params.m_chPage;
672 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentPage, pibAttr);
673}
674
675void
677{
678 NS_LOG_FUNCTION(this);
679
680 // Association is typically preceded by beacon reception and a MLME-SCAN.request, therefore,
681 // the values of the Associate.request params usually come from the information
682 // obtained from those operations.
684 m_associateParams = params;
685 m_ignoreDataCmdAck = false;
686
687 bool invalidRequest = false;
688
689 if (params.m_coordPanId == 0xffff)
690 {
691 invalidRequest = true;
692 }
693
694 if (!invalidRequest && params.m_coordAddrMode == SHORT_ADDR)
695 {
696 if (params.m_coordShortAddr == Mac16Address("ff:ff") ||
697 params.m_coordShortAddr == Mac16Address("ff:fe"))
698 {
699 invalidRequest = true;
700 }
701 }
702 else if (!invalidRequest && params.m_coordAddrMode == EXT_ADDR)
703 {
704 if (params.m_coordExtAddr == Mac64Address("ff:ff:ff:ff:ff:ff:ff:ff") ||
705 params.m_coordExtAddr == Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed"))
706 {
707 invalidRequest = true;
708 }
709 }
710
711 if (invalidRequest)
712 {
715 NS_LOG_ERROR(this << " Invalid PAN id in Association request");
717 {
718 MlmeAssociateConfirmParams confirmParams;
719 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
721 m_mlmeAssociateConfirmCallback(confirmParams);
722 }
723 }
724 else
725 {
727 pibAttr->phyCurrentPage = params.m_chPage;
728 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentPage, pibAttr);
729 }
730}
731
732void
734{
735 // the primitive is no longer pending (channel & page are set)
737 // As described in IEEE 802.15.4-2011 (Section 5.1.3.1)
740 {
742 }
743 else
744 {
747 }
748
750}
751
752void
754{
755 // Associate Short Address (m_assocShortAddr)
756 // FF:FF = Association Request failed
757 // FF:FE = The association request is accepted, but the device should use its extended address
758 // Other = The assigned short address by the coordinator
759
760 NS_LOG_FUNCTION(this);
761
763 m_macDsn++;
764 LrWpanMacTrailer macTrailer;
765 Ptr<Packet> commandPacket = Create<Packet>();
766
767 // Mac header Assoc. Response Comm. See 802.15.4-2011 (Section 5.3.2.1)
770 macHdr.SetPanIdComp();
771 macHdr.SetDstAddrFields(m_macPanId, params.m_extDevAddr);
772 macHdr.SetSrcAddrFields(0xffff, GetExtendedAddress());
773
775 macPayload.SetShortAddr(params.m_assocShortAddr);
776 macPayload.SetAssociationStatus(static_cast<uint8_t>(params.m_status));
777
778 macHdr.SetSecDisable();
779 macHdr.SetAckReq();
780
781 commandPacket->AddHeader(macPayload);
782 commandPacket->AddHeader(macHdr);
783
784 // Calculate FCS if the global attribute ChecksumEnabled is set.
786 {
787 macTrailer.EnableFcs(true);
788 macTrailer.SetFcs(commandPacket);
789 }
790
791 commandPacket->AddTrailer(macTrailer);
792
793 // Save packet in the Pending Transaction list.
794 EnqueueInd(commandPacket);
795}
796
797void
799{
800 NS_LOG_FUNCTION(this);
801 // Mac header Coordinator realigment Command
802 // See 802.15.4-2011 (Section 6.2.7.2)
804 m_macDsn++;
805 LrWpanMacTrailer macTrailer;
806 Ptr<Packet> commandPacket = Create<Packet>();
807 macHdr.SetPanIdComp();
809 macHdr.SetDstAddrFields(0xffff, params.m_orphanAddr);
810
813 macHdr.SetSrcAddrFields(m_macPanId, Mac16Address("FF:FF"));
814
815 macHdr.SetFrameVer(0x01);
816 macHdr.SetSecDisable();
817 macHdr.SetAckReq();
818
820 macPayload.SetPanId(m_macPanId);
822 macPayload.SetChannel(m_phy->GetCurrentChannelNum());
823 macPayload.SetPage(m_phy->GetCurrentPage());
824
825 if (params.m_assocMember)
826 {
827 // The orphan device was associated with the coordinator
828
829 // Either FF:FE for extended address mode
830 // or the short address assigned by the coord.
831 macPayload.SetShortAddr(params.m_shortAddr);
832 }
833 else
834 {
835 // The orphan device was NOT associated with the coordinator
836 macPayload.SetShortAddr(Mac16Address("FF:FF"));
837 }
838
839 commandPacket->AddHeader(macPayload);
840 commandPacket->AddHeader(macHdr);
841
842 // Calculate FCS if the global attribute ChecksumEnabled is set.
844 {
845 macTrailer.EnableFcs(true);
846 macTrailer.SetFcs(commandPacket);
847 }
848
849 commandPacket->AddTrailer(macTrailer);
850
852 txQElement->txQPkt = commandPacket;
853 EnqueueTxQElement(txQElement);
854 CheckQueue();
855}
856
857void
859{
860 NS_LOG_FUNCTION(this);
861 NS_ASSERT(params.m_logCh <= 26 && m_macPanId != 0xffff);
862
863 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
864 // change phy current logical channel
866 pibAttr->phyCurrentChannel = params.m_logCh;
867 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
868
869 // Enable Phy receiver
870 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
871
872 uint64_t searchSymbols;
873 Time searchBeaconTime;
874
876 {
878 }
879
880 if (params.m_trackBcn)
881 {
883 // search for a beacon for a time = incomingSuperframe symbols + 960 symbols
884 searchSymbols =
886 searchBeaconTime = Seconds((double)searchSymbols / symbolRate);
887 m_beaconTrackingOn = true;
889 Simulator::Schedule(searchBeaconTime, &LrWpanMac::BeaconSearchTimeout, this);
890 }
891 else
892 {
893 m_beaconTrackingOn = false;
894 }
895}
896
897void
899{
900 NS_LOG_FUNCTION(this);
901
903 m_macBsn++;
904
906
907 Ptr<Packet> beaconPacket = Create<Packet>();
908 // TODO: complete poll request (part of indirect transmissions)
909 NS_FATAL_ERROR(this << " Poll request currently not supported");
910}
911
912void
914{
915 MlmeSetConfirmParams confirmParams;
916 confirmParams.m_status = MacStatus::SUCCESS;
917
918 switch (id)
919 {
921 m_macAssociationPermit = attribute->macAssociationPermit;
922 break;
923 case macBeaconPayload:
924 if (attribute->macBeaconPayload.size() > aMaxBeaconPayloadLength)
925 {
927 }
928 else
929 {
930 m_macBeaconPayload = attribute->macBeaconPayload;
931 }
932 break;
934 if (attribute->macBeaconPayloadLength > aMaxBeaconPayloadLength)
935 {
937 }
938 else
939 {
940 m_macBeaconPayloadLength = attribute->macBeaconPayloadLength;
941 }
942 break;
943 case macShortAddress:
944 m_shortAddress = attribute->macShortAddress;
945 break;
947 confirmParams.m_status = MacStatus::READ_ONLY;
948 break;
949 case macPanId:
951 break;
953 m_macPromiscuousMode = attribute->macPromiscuousMode;
954 break;
955 case macRxOnWhenIdle:
956 m_macRxOnWhenIdle = attribute->macRxOnWhenIdle;
957 break;
958 case pCurrentChannel: {
960 pibAttr->phyCurrentChannel = attribute->pCurrentChannel;
961 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
962 break;
963 }
964 case pCurrentPage: {
966 pibAttr->phyCurrentPage = attribute->pCurrentPage;
967 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentPage, pibAttr);
968 break;
969 }
970 default:
971 // TODO: Add support for setting other attributes
973 break;
974 }
975
977 {
978 confirmParams.id = id;
979 m_mlmeSetConfirmCallback(confirmParams);
980 }
981}
982
983void
985{
988
989 switch (id)
990 {
992 attributes->macAssociationPermit = m_macAssociationPermit;
993 break;
994 case macBeaconPayload:
995 attributes->macBeaconPayload = m_macBeaconPayload;
996 break;
998 attributes->macBeaconPayloadLength = m_macBeaconPayloadLength;
999 break;
1000 case macPromiscuousMode:
1001 attributes->macPromiscuousMode = m_macPromiscuousMode;
1002 break;
1003 case macRxOnWhenIdle:
1004 attributes->macRxOnWhenIdle = m_macRxOnWhenIdle;
1005 break;
1006 case macShortAddress:
1007 attributes->macShortAddress = m_shortAddress;
1008 break;
1009 case macExtendedAddress:
1010 attributes->macExtendedAddress = m_macExtendedAddress;
1011 break;
1012 case macPanId:
1013 attributes->macPanId = m_macPanId;
1014 break;
1015 case pCurrentChannel:
1016 attributes->pCurrentChannel = m_phy->GetCurrentChannelNum();
1017 break;
1018 case pCurrentPage:
1019 attributes->pCurrentPage = m_phy->GetCurrentPage();
1020 break;
1021 default:
1023 break;
1024 }
1025
1027 {
1028 m_mlmeGetConfirmCallback(status, id, attributes);
1029 }
1030}
1031
1032void
1034{
1035 NS_LOG_FUNCTION(this);
1037
1038 m_macBsn++;
1039
1040 Ptr<Packet> beaconPacket;
1041 if (m_macBeaconPayload.empty())
1042 {
1043 beaconPacket = Create<Packet>();
1044 }
1045 else
1046 {
1047 // Extract the octets from m_macBeaconPayload and place them in a packet
1048 beaconPacket = Create<Packet>(m_macBeaconPayload.data(), m_macBeaconPayload.size());
1049 }
1050
1053 macHdr.SetDstAddrFields(GetPanId(), Mac16Address("ff:ff"));
1054
1055 // see IEEE 802.15.4-2011 Section 5.1.2.4
1056 if (GetShortAddress() == Mac16Address("ff:fe"))
1057 {
1060 }
1061 else
1062 {
1065 }
1066
1067 macHdr.SetSecDisable();
1068 macHdr.SetNoAckReq();
1069
1070 BeaconPayloadHeader macPayload;
1072 macPayload.SetGtsFields(GetGtsFields());
1074
1075 beaconPacket->AddHeader(macPayload);
1076 beaconPacket->AddHeader(macHdr);
1077
1078 // Calculate FCS if the global attribute ChecksumEnabled is set.
1079 LrWpanMacTrailer macTrailer;
1081 {
1082 macTrailer.EnableFcs(true);
1083 macTrailer.SetFcs(beaconPacket);
1084 }
1085
1086 beaconPacket->AddTrailer(macTrailer);
1087
1088 if (m_csmaCa->IsSlottedCsmaCa())
1089 {
1090 // Beacon in beacon-enabled mode
1091 // Transmit beacon immediately (i.e. Without CSMA/CA)
1092 m_txPkt = beaconPacket;
1094 NS_LOG_DEBUG("Outgoing superframe Active Portion (Beacon + CAP + CFP): "
1095 << m_superframeDuration << " symbols");
1096
1098 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
1099 }
1100 else
1101 {
1102 // Beacon as a result of a beacon request
1103 // The beacon shall be transmitted using CSMA/CA
1104 // IEEE 802.15.4-2011 (Section 5.1.2.1.2)
1106 txQElement->txQPkt = beaconPacket;
1107 EnqueueTxQElement(txQElement);
1108 CheckQueue();
1109 }
1110}
1111
1112void
1114{
1115 NS_LOG_FUNCTION(this);
1116
1118 m_macDsn++;
1119 LrWpanMacTrailer macTrailer;
1120 Ptr<Packet> commandPacket = Create<Packet>();
1121
1122 // Beacon Request Command Mac header values See IEEE 802.15.4-2011 (Section 5.3.7)
1123 macHdr.SetNoPanIdComp();
1126
1127 // Not associated PAN, broadcast dst address
1128 macHdr.SetDstAddrFields(0xFFFF, Mac16Address("FF:FF"));
1129
1130 macHdr.SetSecDisable();
1131 macHdr.SetNoAckReq();
1132
1133 CommandPayloadHeader macPayload;
1135
1136 commandPacket->AddHeader(macPayload);
1137 commandPacket->AddHeader(macHdr);
1138
1139 // Calculate FCS if the global attribute ChecksumEnabled is set.
1141 {
1142 macTrailer.EnableFcs(true);
1143 macTrailer.SetFcs(commandPacket);
1144 }
1145
1146 commandPacket->AddTrailer(macTrailer);
1147
1149 txQElement->txQPkt = commandPacket;
1150 EnqueueTxQElement(txQElement);
1151 CheckQueue();
1152}
1153
1154void
1156{
1158 m_macDsn++;
1159 LrWpanMacTrailer macTrailer;
1160 Ptr<Packet> commandPacket = Create<Packet>();
1161
1162 // See IEEE 802.15.4-2011 (5.3.6)
1163 macHdr.SetPanIdComp();
1164
1166 macHdr.SetSrcAddrFields(0xFFFF, GetExtendedAddress());
1167
1169 macHdr.SetDstAddrFields(0xFFFF, Mac16Address("FF:FF"));
1170
1171 macHdr.SetSecDisable();
1172 macHdr.SetNoAckReq();
1173
1174 CommandPayloadHeader macPayload;
1176
1177 commandPacket->AddHeader(macPayload);
1178 commandPacket->AddHeader(macHdr);
1179
1180 // Calculate FCS if the global attribute ChecksumEnabled is set.
1182 {
1183 macTrailer.EnableFcs(true);
1184 macTrailer.SetFcs(commandPacket);
1185 }
1186
1187 commandPacket->AddTrailer(macTrailer);
1188
1190 txQElement->txQPkt = commandPacket;
1191 EnqueueTxQElement(txQElement);
1192 CheckQueue();
1193}
1194
1195void
1197{
1198 NS_LOG_FUNCTION(this);
1199
1201 m_macDsn++;
1202 LrWpanMacTrailer macTrailer;
1203 Ptr<Packet> commandPacket = Create<Packet>();
1204
1205 // Assoc. Req. Comm. Mac header values See IEEE 802.15.4-2011 (Section 5.3.1.1)
1207 macHdr.SetSrcAddrFields(0xffff, GetExtendedAddress());
1208
1210 {
1213 }
1214 else
1215 {
1218 }
1219
1220 macHdr.SetSecDisable();
1221 macHdr.SetAckReq();
1222
1225
1226 commandPacket->AddHeader(macPayload);
1227 commandPacket->AddHeader(macHdr);
1228
1229 // Calculate FCS if the global attribute ChecksumEnabled is set.
1231 {
1232 macTrailer.EnableFcs(true);
1233 macTrailer.SetFcs(commandPacket);
1234 }
1235
1236 commandPacket->AddTrailer(macTrailer);
1237
1239 txQElement->txQPkt = commandPacket;
1240 EnqueueTxQElement(txQElement);
1241 CheckQueue();
1242}
1243
1244void
1246{
1247 // See IEEE 802.15.4-2011 (Section 5.3.5)
1248 // This command can be sent for 3 different situations:
1249 // a) In response to a beacon indicating that there is data for the device.
1250 // b) Triggered by MLME-POLL.request.
1251 // c) To follow an ACK of an Association Request command and continue the associate process.
1252
1253 // TODO: Implementation of a) and b) will be done when Indirect transmissions are fully
1254 // supported.
1255 // for now, only case c) is considered.
1256
1257 NS_LOG_FUNCTION(this);
1258
1260 m_macDsn++;
1261 LrWpanMacTrailer macTrailer;
1262 Ptr<Packet> commandPacket = Create<Packet>();
1263
1264 // Mac Header values (Section 5.3.5)
1266 macHdr.SetSrcAddrFields(0xffff, m_macExtendedAddress);
1267
1268 if (m_macCoordShortAddress == Mac16Address("ff:fe"))
1269 {
1272 }
1273 else
1274 {
1277 }
1278
1279 macHdr.SetSecDisable();
1280 macHdr.SetAckReq();
1281
1283
1284 commandPacket->AddHeader(macPayload);
1285 commandPacket->AddHeader(macHdr);
1286
1287 // Calculate FCS if the global attribute ChecksumEnabled is set.
1289 {
1290 macTrailer.EnableFcs(true);
1291 macTrailer.SetFcs(commandPacket);
1292 }
1293
1294 commandPacket->AddTrailer(macTrailer);
1295
1296 // Set the Command packet to be transmitted
1298 txQElement->txQPkt = commandPacket;
1299 EnqueueTxQElement(txQElement);
1300 CheckQueue();
1301}
1302
1303void
1305{
1306 LrWpanMacHeader receivedMacHdr;
1307 rxDataReqPkt->RemoveHeader(receivedMacHdr);
1308 CommandPayloadHeader receivedMacPayload;
1309 rxDataReqPkt->RemoveHeader(receivedMacPayload);
1310
1312
1314 bool elementFound;
1315 elementFound = DequeueInd(receivedMacHdr.GetExtSrcAddr(), indTxQElement);
1316 if (elementFound)
1317 {
1319 txQElement->txQPkt = indTxQElement->txQPkt;
1320 m_txQueue.emplace_back(txQElement);
1321 }
1322 else
1323 {
1324 NS_LOG_DEBUG("Requested element not found in pending list");
1325 }
1326}
1327
1328void
1330{
1331 // Association response command was not received, return to default values.
1332 m_macPanId = 0xffff;
1334 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
1335
1337 {
1338 MlmeAssociateConfirmParams confirmParams;
1339 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
1340 confirmParams.m_status = MacStatus::NO_DATA;
1341 m_mlmeAssociateConfirmCallback(confirmParams);
1342 }
1343}
1344
1345void
1347{
1348 NS_LOG_FUNCTION(this);
1349 // The primitive is no longer pending (Channel & Page have been set)
1351
1352 if (m_startParams.m_coorRealgn) // Coordinator Realignment
1353 {
1354 // TODO: Send realignment request command frame in CSMA/CA
1355 NS_LOG_ERROR(this << " Coordinator realignment request not supported");
1356 return;
1357 }
1358 else
1359 {
1361 {
1362 m_panCoor = true;
1363 }
1364
1365 m_coor = true;
1367
1368 NS_ASSERT(m_startParams.m_PanId != 0xffff);
1369
1371 if (m_macBeaconOrder == 15)
1372 {
1373 // Non-beacon enabled PAN
1374 // Cancel any ongoing events and CSMA-CA process
1376 m_fnlCapSlot = 15;
1377 m_beaconInterval = 0;
1378
1379 m_csmaCa->Cancel();
1388
1389 m_csmaCa->SetUnSlottedCsmaCa();
1390
1392 {
1393 MlmeStartConfirmParams confirmParams;
1394 confirmParams.m_status = MacStatus::SUCCESS;
1395 m_mlmeStartConfirmCallback(confirmParams);
1396 }
1397
1398 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
1399 }
1400 else
1401 {
1403 m_csmaCa->SetBatteryLifeExtension(m_startParams.m_battLifeExt);
1404
1405 m_csmaCa->SetSlottedCsmaCa();
1406
1407 // TODO: Calculate the real Final CAP slot (requires GTS implementation)
1408 // FinalCapSlot = Superframe duration slots - CFP slots.
1409 // In the current implementation the value of the final cap slot is equal to
1410 // the total number of possible slots in the superframe (15).
1411 m_fnlCapSlot = 15;
1412
1415 m_superframeDuration = (static_cast<uint32_t>(1 << m_macSuperframeOrder)) *
1417
1418 // TODO: change the beacon sending according to the startTime parameter (if not PAN
1419 // coordinator)
1420
1422 }
1423 }
1424}
1425
1426void
1428{
1429 NS_LOG_FUNCTION(this);
1430
1432
1433 bool channelFound = false;
1434
1435 for (int i = m_channelScanIndex; i <= 26; i++)
1436 {
1438 {
1439 channelFound = true;
1440 break;
1441 }
1443 }
1444
1445 if (channelFound)
1446 {
1447 // Switch to the next channel in the list and restart scan
1449 pibAttr->phyCurrentChannel = m_channelScanIndex;
1450 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
1451 }
1452 else
1453 {
1454 // All channels in the list scan completed.
1455 // Return variables to the values before the scan and return the status to the next layer.
1457 m_macPanIdScan = 0;
1458
1459 // TODO: restart beacon transmissions that were active before the beginning of the scan
1460 // (i.e when a coordinator perform a scan and it was already transmitting beacons)
1461 MlmeScanConfirmParams confirmParams;
1462 confirmParams.m_chPage = m_scanParams.m_chPage;
1463 confirmParams.m_scanType = m_scanParams.m_scanType;
1464 confirmParams.m_energyDetList = {};
1465 confirmParams.m_unscannedCh = m_unscannedChannels;
1466 confirmParams.m_resultListSize = m_panDescriptorList.size();
1467
1468 // See IEEE 802.15.4-2011, Table 31 (panDescriptorList value on macAutoRequest)
1469 // and Section 6.2.10.2
1470 switch (confirmParams.m_scanType)
1471 {
1472 case MLMESCAN_PASSIVE:
1473 if (m_macAutoRequest)
1474 {
1475 confirmParams.m_panDescList = m_panDescriptorList;
1476 }
1477 confirmParams.m_status = MacStatus::SUCCESS;
1478 break;
1479 case MLMESCAN_ACTIVE:
1480 if (m_panDescriptorList.empty())
1481 {
1482 confirmParams.m_status = MacStatus::NO_BEACON;
1483 }
1484 else
1485 {
1486 if (m_macAutoRequest)
1487 {
1488 confirmParams.m_panDescList = m_panDescriptorList;
1489 }
1490 confirmParams.m_status = MacStatus::SUCCESS;
1491 }
1492 break;
1493 case MLMESCAN_ORPHAN:
1494 confirmParams.m_panDescList = {};
1495 confirmParams.m_status = MacStatus::NO_BEACON;
1496 confirmParams.m_resultListSize = 0;
1497 // The device lost track of the coordinator and was unable
1498 // to locate it, disassociate from the network.
1499 m_macPanId = 0xffff;
1500 m_shortAddress = Mac16Address("FF:FF");
1502 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
1503 break;
1504 default:
1505 NS_LOG_ERROR(this << " Invalid scan type");
1506 }
1507
1510 m_scanParams = {};
1511
1513 {
1514 m_mlmeScanConfirmCallback(confirmParams);
1515 }
1516 }
1517}
1518
1519void
1521{
1522 NS_LOG_FUNCTION(this);
1523 // Add the results of channel energy scan to the detectList
1525 m_maxEnergyLevel = 0;
1526
1528
1529 bool channelFound = false;
1530 for (int i = m_channelScanIndex; i <= 26; i++)
1531 {
1533 {
1534 channelFound = true;
1535 break;
1536 }
1538 }
1539
1540 if (channelFound)
1541 {
1542 // switch to the next channel in the list and restart scan
1544 pibAttr->phyCurrentChannel = m_channelScanIndex;
1545 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
1546 }
1547 else
1548 {
1549 // Scan on all channels on the list completed
1550 // Return to the MAC values previous to start of the first scan.
1552 m_macPanIdScan = 0;
1553
1554 // TODO: restart beacon transmissions that were active before the beginning of the scan
1555 // (i.e when a coordinator perform a scan and it was already transmitting beacons)
1556
1557 // All channels scanned, report success
1558 MlmeScanConfirmParams confirmParams;
1559 confirmParams.m_status = MacStatus::SUCCESS;
1560 confirmParams.m_chPage = m_phy->GetCurrentPage();
1561 confirmParams.m_scanType = m_scanParams.m_scanType;
1562 confirmParams.m_energyDetList = m_energyDetectList;
1563 confirmParams.m_resultListSize = m_energyDetectList.size();
1564
1567 m_scanParams = {};
1568
1570 {
1571 m_mlmeScanConfirmCallback(confirmParams);
1572 }
1573 }
1574}
1575
1576void
1578{
1579 uint32_t activeSlot;
1580 uint64_t capDuration;
1581 Time endCapTime;
1582 uint64_t symbolRate;
1583
1584 symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1585
1586 if (superframeType == OUTGOING)
1587 {
1589 activeSlot = m_superframeDuration / 16;
1590 capDuration = activeSlot * (m_fnlCapSlot + 1);
1591 endCapTime = Seconds((double)capDuration / symbolRate);
1592 // Obtain the end of the CAP by adjust the time it took to send the beacon
1593 endCapTime -= (Simulator::Now() - m_macBeaconTxTime);
1594
1595 NS_LOG_DEBUG("Outgoing superframe CAP duration " << (endCapTime.GetSeconds() * symbolRate)
1596 << " symbols (" << endCapTime.As(Time::S)
1597 << ")");
1598 NS_LOG_DEBUG("Active Slots duration " << activeSlot << " symbols");
1599
1600 m_capEvent =
1602 }
1603 else
1604 {
1606 activeSlot = m_incomingSuperframeDuration / 16;
1607 capDuration = activeSlot * (m_incomingFnlCapSlot + 1);
1608 endCapTime = Seconds((double)capDuration / symbolRate);
1609 // Obtain the end of the CAP by adjust the time it took to receive the beacon
1610 endCapTime -= (Simulator::Now() - m_macBeaconRxTime);
1611
1612 NS_LOG_DEBUG("Incoming superframe CAP duration " << (endCapTime.GetSeconds() * symbolRate)
1613 << " symbols (" << endCapTime.As(Time::S)
1614 << ")");
1615 NS_LOG_DEBUG("Active Slots duration " << activeSlot << " symbols");
1616
1617 m_capEvent =
1619 }
1620
1621 CheckQueue();
1622}
1623
1624void
1626{
1627 uint32_t activeSlot;
1628 uint64_t cfpDuration;
1629 Time endCfpTime;
1630 uint64_t symbolRate;
1631
1632 symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1633
1634 if (superframeType == INCOMING)
1635 {
1636 activeSlot = m_incomingSuperframeDuration / 16;
1637 cfpDuration = activeSlot * (15 - m_incomingFnlCapSlot);
1638 endCfpTime = Seconds((double)cfpDuration / symbolRate);
1639 if (cfpDuration > 0)
1640 {
1642 }
1643
1644 NS_LOG_DEBUG("Incoming superframe CFP duration " << cfpDuration << " symbols ("
1645 << endCfpTime.As(Time::S) << ")");
1646
1649 this,
1651 }
1652 else
1653 {
1654 activeSlot = m_superframeDuration / 16;
1655 cfpDuration = activeSlot * (15 - m_fnlCapSlot);
1656 endCfpTime = Seconds((double)cfpDuration / symbolRate);
1657
1658 if (cfpDuration > 0)
1659 {
1661 }
1662
1663 NS_LOG_DEBUG("Outgoing superframe CFP duration " << cfpDuration << " symbols ("
1664 << endCfpTime.As(Time::S) << ")");
1665
1666 m_cfpEvent = Simulator::Schedule(endCfpTime,
1668 this,
1670 }
1671 // TODO: Start transmit or receive GTS here.
1672}
1673
1674void
1676{
1677 uint64_t inactiveDuration;
1678 Time endInactiveTime;
1679 uint64_t symbolRate;
1680
1681 symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1682
1683 if (superframeType == INCOMING)
1684 {
1686 endInactiveTime = Seconds((double)inactiveDuration / symbolRate);
1687
1688 if (inactiveDuration > 0)
1689 {
1691 }
1692
1693 NS_LOG_DEBUG("Incoming superframe Inactive Portion duration "
1694 << inactiveDuration << " symbols (" << endInactiveTime.As(Time::S) << ")");
1695 m_beaconEvent = Simulator::Schedule(endInactiveTime, &LrWpanMac::AwaitBeacon, this);
1696 }
1697 else
1698 {
1699 inactiveDuration = m_beaconInterval - m_superframeDuration;
1700 endInactiveTime = Seconds((double)inactiveDuration / symbolRate);
1701
1702 if (inactiveDuration > 0)
1703 {
1705 }
1706
1707 NS_LOG_DEBUG("Outgoing superframe Inactive Portion duration "
1708 << inactiveDuration << " symbols (" << endInactiveTime.As(Time::S) << ")");
1710 }
1711}
1712
1713void
1715{
1717
1718 // TODO: If the device waits more than the expected time to receive the beacon (wait = 46
1719 // symbols for default beacon size)
1720 // it should continue with the start of the incoming CAP even if it did not receive the
1721 // beacon. At the moment, the start of the incoming CAP is only triggered if the beacon is
1722 // received. See MLME-SyncLoss for details.
1723}
1724
1725void
1727{
1728 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1729
1731 {
1732 MlmeSyncLossIndicationParams syncLossParams;
1733 // syncLossParams.m_logCh =
1734 syncLossParams.m_lossReason = MacStatus::BEACON_LOSS;
1735 syncLossParams.m_panId = m_macPanId;
1736 m_mlmeSyncLossIndicationCallback(syncLossParams);
1737
1738 m_beaconTrackingOn = false;
1739 m_numLostBeacons = 0;
1740 }
1741 else
1742 {
1744
1745 // Search for one more beacon
1746 uint64_t searchSymbols;
1747 Time searchBeaconTime;
1748 searchSymbols =
1750 searchBeaconTime = Seconds((double)searchSymbols / symbolRate);
1752 Simulator::Schedule(searchBeaconTime, &LrWpanMac::BeaconSearchTimeout, this);
1753 }
1754}
1755
1756void
1758{
1759 NS_LOG_FUNCTION(this << lqi << p);
1760 // The received beacon size in symbols
1761 // Beacon = Sync Header (SHR)[5 bytes] +
1762 // PHY header (PHR) [1 byte] +
1763 // PSDU (MAC header + beacon payload) [default 17 bytes]
1764 m_rxBeaconSymbols = m_phy->GetPhySHRDuration() + 1 * m_phy->GetPhySymbolsPerOctet() +
1765 (p->GetSize() * m_phy->GetPhySymbolsPerOctet());
1766
1767 // The start of Rx beacon time and start of the Incoming superframe Active Period
1768 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1769 m_macBeaconRxTime = Simulator::Now() - Seconds(double(m_rxBeaconSymbols) / symbolRate);
1770
1771 NS_LOG_DEBUG("Beacon Received; forwarding up (m_macBeaconRxTime: "
1772 << m_macBeaconRxTime.As(Time::S) << ")");
1773
1774 // Strip the MAC header, the trailer and the Beacon Payload
1775 LrWpanMacTrailer receivedMacTrailer;
1776 p->RemoveTrailer(receivedMacTrailer);
1777
1778 LrWpanMacHeader receivedMacHdr;
1779 p->RemoveHeader(receivedMacHdr);
1780
1781 BeaconPayloadHeader receivedMacPayload;
1782 p->RemoveHeader(receivedMacPayload);
1783
1784 // Fill the PAN descriptor
1785 PanDescriptor panDescriptor;
1786
1787 if (receivedMacHdr.GetSrcAddrMode() == SHORT_ADDR)
1788 {
1789 panDescriptor.m_coorAddrMode = SHORT_ADDR;
1790 panDescriptor.m_coorShortAddr = receivedMacHdr.GetShortSrcAddr();
1791 }
1792 else
1793 {
1794 panDescriptor.m_coorAddrMode = EXT_ADDR;
1795 panDescriptor.m_coorExtAddr = receivedMacHdr.GetExtSrcAddr();
1796 }
1797
1798 panDescriptor.m_coorPanId = receivedMacHdr.GetSrcPanId();
1799 panDescriptor.m_gtsPermit = receivedMacPayload.GetGtsFields().GetGtsPermit();
1800 panDescriptor.m_linkQuality = lqi;
1801 panDescriptor.m_logChPage = m_phy->GetCurrentPage();
1802 panDescriptor.m_logCh = m_phy->GetCurrentChannelNum();
1803 panDescriptor.m_superframeSpec = receivedMacPayload.GetSuperframeSpecField();
1804 panDescriptor.m_timeStamp = m_macBeaconRxTime;
1805
1806 // Process beacon when device belongs to a PAN (associated device)
1807 if (!m_scanEvent.IsPending() && m_macPanId == receivedMacHdr.GetDstPanId())
1808 {
1809 // We need to make sure to cancel any possible ongoing unslotted CSMA/CA
1810 // operations when receiving a beacon (e.g. Those taking place at the
1811 // beginning of an Association).
1812 m_csmaCa->Cancel();
1813
1814 SuperframeField incomingSuperframe(receivedMacPayload.GetSuperframeSpecField());
1815
1816 m_incomingBeaconOrder = incomingSuperframe.GetBeaconOrder();
1817 m_incomingSuperframeOrder = incomingSuperframe.GetFrameOrder();
1818 m_incomingFnlCapSlot = incomingSuperframe.GetFinalCapSlot();
1819
1820 if (m_incomingBeaconOrder < 15)
1821 {
1822 // Start Beacon-enabled mode
1823 m_csmaCa->SetSlottedCsmaCa();
1827 (static_cast<uint32_t>(1 << m_incomingSuperframeOrder));
1828
1829 if (incomingSuperframe.IsBattLifeExt())
1830 {
1831 m_csmaCa->SetBatteryLifeExtension(true);
1832 }
1833 else
1834 {
1835 m_csmaCa->SetBatteryLifeExtension(false);
1836 }
1837
1838 // TODO: get Incoming frame GTS Fields here
1839
1840 // Begin CAP on the current device using info from
1841 // the Incoming superframe
1842 NS_LOG_DEBUG("Incoming superframe Active Portion "
1843 << "(Beacon + CAP + CFP): " << m_incomingSuperframeDuration << " symbols");
1844
1847 }
1848 else
1849 {
1850 // Start non-beacon enabled mode
1851 m_csmaCa->SetUnSlottedCsmaCa();
1852 }
1853
1855 }
1856 else if (!m_scanEvent.IsPending() && m_macPanId == 0xFFFF)
1857 {
1858 NS_LOG_DEBUG(this << " Device not associated, cannot process beacon");
1859 }
1860
1861 if (m_macAutoRequest)
1862 {
1863 if (p->GetSize() > 0)
1864 {
1866 {
1867 // The beacon contains payload, send the beacon notification.
1869 beaconParams.m_bsn = receivedMacHdr.GetSeqNum();
1870 beaconParams.m_panDescriptor = panDescriptor;
1871 beaconParams.m_sduLength = p->GetSize();
1872 beaconParams.m_sdu = p;
1874 }
1875 }
1876
1877 if (m_scanEvent.IsPending())
1878 {
1879 // Channel scanning is taking place, save only unique PAN descriptors
1880 bool descriptorExists = false;
1881
1882 for (const auto& descriptor : m_panDescriptorList)
1883 {
1884 if (descriptor.m_coorAddrMode == SHORT_ADDR)
1885 {
1886 // Found a coordinator in PAN descriptor list with the same
1887 // registered short address
1888 descriptorExists =
1889 (descriptor.m_coorShortAddr == panDescriptor.m_coorShortAddr &&
1890 descriptor.m_coorPanId == panDescriptor.m_coorPanId);
1891 }
1892 else
1893 {
1894 // Found a coordinator in PAN descriptor list with the same
1895 // registered extended address
1896 descriptorExists = (descriptor.m_coorExtAddr == panDescriptor.m_coorExtAddr &&
1897 descriptor.m_coorPanId == panDescriptor.m_coorPanId);
1898 }
1899
1900 if (descriptorExists)
1901 {
1902 break;
1903 }
1904 }
1905
1906 if (!descriptorExists)
1907 {
1908 m_panDescriptorList.emplace_back(panDescriptor);
1909 }
1910 return;
1911 }
1912 else if (m_trackingEvent.IsPending())
1913 {
1914 // check if MLME-SYNC.request was previously issued and running
1915 // Sync. is necessary to handle pending messages (indirect
1916 // transmissions)
1918 m_numLostBeacons = 0;
1919
1921 {
1922 // if tracking option is on keep tracking the next beacon
1923 uint64_t searchSymbols;
1924 Time searchBeaconTime;
1925
1926 searchSymbols = (static_cast<uint64_t>(1 << m_incomingBeaconOrder)) +
1928 searchBeaconTime = Seconds(static_cast<double>(searchSymbols / symbolRate));
1930 Simulator::Schedule(searchBeaconTime, &LrWpanMac::BeaconSearchTimeout, this);
1931 }
1932
1933 PendingAddrFields pndAddrFields;
1934 pndAddrFields = receivedMacPayload.GetPndAddrFields();
1935
1936 // TODO: Ignore pending data, and do not send data command request if
1937 // the address is in the GTS list.
1938 // If the address is not in the GTS list, then check if the
1939 // address is in the short address pending list or in the extended
1940 // address pending list and send a data command request.
1941 }
1942 }
1943 else
1944 {
1945 // m_macAutoRequest is FALSE
1946 // Data command request are not send, only the beacon notification.
1947 // see IEEE 802.15.4-2011 Section 6.2.4.1
1949 {
1951 beaconParams.m_bsn = receivedMacHdr.GetSeqNum();
1952 beaconParams.m_panDescriptor = panDescriptor;
1953 beaconParams.m_sduLength = p->GetSize();
1954 beaconParams.m_sdu = p;
1956 }
1957 }
1958}
1959
1960void
1962{
1963 NS_LOG_FUNCTION(this);
1964 // Pull a packet from the queue and start sending if we are not already sending.
1965 if (m_macState == MAC_IDLE && !m_txQueue.empty() && !m_setMacState.IsPending())
1966 {
1967 if (m_csmaCa->IsUnSlottedCsmaCa() || (m_outSuperframeStatus == CAP && m_coor) ||
1969 {
1970 // check MAC is not in a IFS
1971 if (!m_ifsEvent.IsPending())
1972 {
1973 Ptr<TxQueueElement> txQElement = m_txQueue.front();
1974 m_txPkt = txQElement->txQPkt;
1975
1978 }
1979 }
1980 }
1981}
1982
1983uint16_t
1985{
1986 SuperframeField sfrmSpec;
1987
1990 sfrmSpec.SetFinalCapSlot(m_fnlCapSlot);
1991
1992 if (m_csmaCa->GetBatteryLifeExtension())
1993 {
1994 sfrmSpec.SetBattLifeExt(true);
1995 }
1996
1997 if (m_panCoor)
1998 {
1999 sfrmSpec.SetPanCoor(true);
2000 }
2001
2002 // used to associate devices via Beacons
2004 {
2005 sfrmSpec.SetAssocPermit(true);
2006 }
2007
2008 return sfrmSpec.GetSuperframe();
2009}
2010
2013{
2014 GtsFields gtsFields;
2015
2016 // TODO: Logic to populate the GTS Fields from local information here
2017
2018 return gtsFields;
2019}
2020
2023{
2024 PendingAddrFields pndAddrFields;
2025
2026 // TODO: Logic to populate the Pending Address Fields from local information here
2027 return pndAddrFields;
2028}
2029
2030void
2032{
2033 m_csmaCa = csmaCa;
2034}
2035
2036void
2038{
2039 m_phy = phy;
2040}
2041
2044{
2045 return m_phy;
2046}
2047
2048void
2050{
2052 NS_LOG_FUNCTION(this << psduLength << p << (uint16_t)lqi);
2053
2054 bool acceptFrame;
2055
2056 // from sec 7.5.6.2 Reception and rejection, Std802.15.4-2006
2057 // level 1 filtering, test FCS field and reject if frame fails
2058 // level 2 filtering if promiscuous mode pass frame to higher layer otherwise perform level 3
2059 // filtering level 3 filtering accept frame if Frame type and version is not reserved, and if
2060 // there is a dstPanId then dstPanId=m_macPanId or broadcastPanId, and if there is a
2061 // shortDstAddr then shortDstAddr =shortMacAddr or broadcastAddr, and if beacon frame then
2062 // srcPanId = m_macPanId if only srcAddr field in Data or Command frame,accept frame if
2063 // srcPanId=m_macPanId
2064
2065 Ptr<Packet> originalPkt = p->Copy(); // because we will strip headers
2066 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
2067 m_promiscSnifferTrace(originalPkt);
2068
2069 m_macPromiscRxTrace(originalPkt);
2070 // XXX no rejection tracing (to macRxDropTrace) being performed below
2071
2072 LrWpanMacTrailer receivedMacTrailer;
2073 p->RemoveTrailer(receivedMacTrailer);
2075 {
2076 receivedMacTrailer.EnableFcs(true);
2077 }
2078
2079 // level 1 filtering
2080 if (!receivedMacTrailer.CheckFcs(p))
2081 {
2082 m_macRxDropTrace(originalPkt);
2083 }
2084 else
2085 {
2086 LrWpanMacHeader receivedMacHdr;
2087 p->RemoveHeader(receivedMacHdr);
2088
2090 params.m_dsn = receivedMacHdr.GetSeqNum();
2091 params.m_mpduLinkQuality = lqi;
2092 params.m_srcPanId = receivedMacHdr.GetSrcPanId();
2093 params.m_srcAddrMode = receivedMacHdr.GetSrcAddrMode();
2094 switch (params.m_srcAddrMode)
2095 {
2096 case SHORT_ADDR:
2097 params.m_srcAddr = receivedMacHdr.GetShortSrcAddr();
2098 break;
2099 case EXT_ADDR:
2100 params.m_srcExtAddr = receivedMacHdr.GetExtSrcAddr();
2101 break;
2102 default:
2103 break;
2104 }
2105 params.m_dstPanId = receivedMacHdr.GetDstPanId();
2106 params.m_dstAddrMode = receivedMacHdr.GetDstAddrMode();
2107 switch (params.m_dstAddrMode)
2108 {
2109 case SHORT_ADDR:
2110 params.m_dstAddr = receivedMacHdr.GetShortDstAddr();
2111 break;
2112 case EXT_ADDR:
2113 params.m_dstExtAddr = receivedMacHdr.GetExtDstAddr();
2114 break;
2115 default:
2116 break;
2117 }
2118
2120 {
2121 // level 2 filtering
2122 if (receivedMacHdr.GetSrcAddrMode() == SHORT_ADDR &&
2123 receivedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2124 {
2125 NS_LOG_DEBUG("Packet from [" << params.m_srcAddr << "] to [" << params.m_dstAddr
2126 << "]");
2127 }
2128 else if (receivedMacHdr.GetSrcAddrMode() == EXT_ADDR &&
2129 receivedMacHdr.GetDstAddrMode() == EXT_ADDR)
2130 {
2131 NS_LOG_DEBUG("Packet from [" << params.m_srcExtAddr << "] to ["
2132 << params.m_dstExtAddr << "]");
2133 }
2134 else if (receivedMacHdr.GetSrcAddrMode() == SHORT_ADDR &&
2135 receivedMacHdr.GetDstAddrMode() == EXT_ADDR)
2136 {
2137 NS_LOG_DEBUG("Packet from [" << params.m_srcAddr << "] to [" << params.m_dstExtAddr
2138 << "]");
2139 }
2140 else if (receivedMacHdr.GetSrcAddrMode() == EXT_ADDR &&
2141 receivedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2142 {
2143 NS_LOG_DEBUG("Packet from [" << params.m_srcExtAddr << "] to [" << params.m_dstAddr
2144 << "]");
2145 }
2146
2147 // TODO: Fix here, this should trigger different Indication Callbacks
2148 // depending the type of frame received (data,command, beacon)
2150 {
2151 NS_LOG_DEBUG("promiscuous mode, forwarding up");
2153 }
2154 else
2155 {
2156 NS_LOG_ERROR(this << " Data Indication Callback not initialized");
2157 }
2158 }
2159 else
2160 {
2161 // level 3 frame filtering
2162 acceptFrame = (receivedMacHdr.GetType() != LrWpanMacHeader::LRWPAN_MAC_RESERVED);
2163
2164 if (acceptFrame)
2165 {
2166 acceptFrame = (receivedMacHdr.GetFrameVer() <= 1);
2167 }
2168
2169 if (acceptFrame && (receivedMacHdr.GetDstAddrMode() > 1))
2170 {
2171 // Accept frame if one of the following is true:
2172
2173 // 1) Have the same macPanId
2174 // 2) Is Message to all PANs
2175 // 3) Is a beacon or command frame and the macPanId is not present (bootstrap)
2176 acceptFrame = ((receivedMacHdr.GetDstPanId() == m_macPanId ||
2177 receivedMacHdr.GetDstPanId() == 0xffff) ||
2178 (m_macPanId == 0xffff && receivedMacHdr.IsBeacon())) ||
2179 (m_macPanId == 0xffff && receivedMacHdr.IsCommand());
2180 }
2181
2182 if (acceptFrame && (receivedMacHdr.GetDstAddrMode() == SHORT_ADDR))
2183 {
2184 if (receivedMacHdr.GetShortDstAddr() == m_shortAddress)
2185 {
2186 // unicast, for me
2187 acceptFrame = true;
2188 }
2189 else if (receivedMacHdr.GetShortDstAddr().IsBroadcast() ||
2190 receivedMacHdr.GetShortDstAddr().IsMulticast())
2191 {
2192 // Broadcast or multicast.
2193 // Discard broadcast/multicast with the ACK bit set.
2194 acceptFrame = !receivedMacHdr.IsAckReq();
2195 }
2196 else
2197 {
2198 acceptFrame = false;
2199 }
2200 }
2201
2202 if (acceptFrame && (receivedMacHdr.GetDstAddrMode() == EXT_ADDR))
2203 {
2204 acceptFrame = (receivedMacHdr.GetExtDstAddr() == m_macExtendedAddress);
2205 }
2206
2207 if (acceptFrame && m_scanEvent.IsPending())
2208 {
2209 if (!receivedMacHdr.IsBeacon())
2210 {
2211 acceptFrame = false;
2212 }
2213 }
2214 else if (acceptFrame && m_scanOrphanEvent.IsPending())
2215 {
2216 if (!receivedMacHdr.IsCommand())
2217 {
2218 acceptFrame = false;
2219 }
2220 }
2221 else if (m_scanEnergyEvent.IsPending())
2222 {
2223 // Reject any frames if energy scan is running
2224 acceptFrame = false;
2225 }
2226
2227 // Check device is panCoor with association permit when receiving Association Request
2228 // Commands.
2229 // TODO:: Simple coordinators should also be able to receive it (currently only Pan
2230 // Coordinators are checked)
2231 if (acceptFrame && (receivedMacHdr.IsCommand() && receivedMacHdr.IsAckReq()))
2232 {
2233 CommandPayloadHeader receivedMacPayload;
2234 p->PeekHeader(receivedMacPayload);
2235
2236 if (receivedMacPayload.GetCommandFrameType() ==
2239 {
2240 acceptFrame = false;
2241 }
2242
2243 // Although ACKs do not use CSMA to to be transmitted, we need to make sure
2244 // that the transmitted ACK will not collide with the transmission of a beacon
2245 // when beacon-enabled mode is running in the coordinator.
2246 if (acceptFrame && (m_csmaCa->IsSlottedCsmaCa() && m_capEvent.IsPending()))
2247 {
2248 Time timeLeftInCap = Simulator::GetDelayLeft(m_capEvent);
2249 uint64_t ackSymbols = lrwpan::aTurnaroundTime + m_phy->GetPhySHRDuration() +
2250 ceil(6 * m_phy->GetPhySymbolsPerOctet());
2251 Time ackTime = Seconds((double)ackSymbols / symbolRate);
2252
2253 if (ackTime >= timeLeftInCap)
2254 {
2255 NS_LOG_DEBUG("Command frame received but not enough time to transmit ACK "
2256 "before the end of CAP ");
2257 acceptFrame = false;
2258 }
2259 }
2260 }
2261
2262 if (acceptFrame)
2263 {
2264 m_macRxTrace(originalPkt);
2265 // \todo: What should we do if we receive a frame while waiting for an ACK?
2266 // Especially if this frame has the ACK request bit set, should we reply with
2267 // an ACK, possibly missing the pending ACK?
2268
2269 // If the received frame is a frame with the ACK request bit set, we immediately
2270 // send back an ACK. If we are currently waiting for a pending ACK, we assume the
2271 // ACK was lost and trigger a retransmission after sending the ACK.
2272 if ((receivedMacHdr.IsData() || receivedMacHdr.IsCommand()) &&
2273 receivedMacHdr.IsAckReq() &&
2274 !(receivedMacHdr.GetDstAddrMode() == SHORT_ADDR &&
2275 (receivedMacHdr.GetShortDstAddr().IsBroadcast() ||
2276 receivedMacHdr.GetShortDstAddr().IsMulticast())))
2277 {
2278 // If this is a data or mac command frame, which is not a broadcast or
2279 // multicast, with ack req set, generate and send an ack frame. If there is a
2280 // CSMA medium access in progress we cancel the medium access for sending the
2281 // ACK frame. A new transmission attempt will be started after the ACK was send.
2283 {
2286 }
2287 else if (m_macState == MAC_CSMA)
2288 {
2289 // \todo: If we receive a packet while doing CSMA/CA, should we drop the
2290 // packet because of channel busy,
2291 // or should we restart CSMA/CA for the packet after sending the ACK?
2292 // Currently we simply restart CSMA/CA after sending the ACK.
2293 NS_LOG_DEBUG("Received a packet with ACK required while in CSMA. Cancel "
2294 "current CSMA-CA");
2295 m_csmaCa->Cancel();
2296 }
2297 // Cancel any pending MAC state change, ACKs have higher priority.
2300
2301 // save received packet and LQI to process the appropriate indication/response
2302 // after sending ACK (PD-DATA.confirm)
2303 m_rxPkt = originalPkt->Copy();
2304 m_lastRxFrameLqi = lqi;
2305
2306 // LOG Commands with ACK required.
2307 CommandPayloadHeader receivedMacPayload;
2308 p->PeekHeader(receivedMacPayload);
2309 switch (receivedMacPayload.GetCommandFrameType())
2310 {
2312 NS_LOG_DEBUG("Data Request Command Received; processing ACK");
2313 break;
2315 NS_LOG_DEBUG("Association Request Command Received; processing ACK");
2316 break;
2319 {
2321 .Cancel(); // cancel event to a lost assoc resp cmd.
2322 NS_LOG_DEBUG("Association Response Command Received; processing ACK");
2323 }
2324 else
2325 {
2326 // Association response command was received before (or never received)
2327 // a Data request command ACK. This is an extreme case and it is
2328 // essentially caused by saturation in the network.
2329 // We turn a flag ON to not react once
2330 // we finally receive the Data request command ACK. This behavior is not
2331 // standard, but necessary to address this flaw in design of the
2332 // original association process.
2333 m_ignoreDataCmdAck = true;
2334 NS_LOG_DEBUG("Assoc. Resp Cmd received before Data Req. Cmd. in "
2335 "Association request");
2336 }
2337 break;
2338 }
2339 default:
2340 break;
2341 }
2342
2344 this,
2345 receivedMacHdr.GetSeqNum());
2346 }
2347
2348 if (receivedMacHdr.GetSrcAddrMode() == SHORT_ADDR &&
2349 receivedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2350 {
2351 NS_LOG_DEBUG("Packet from [" << params.m_srcAddr << "] to [" << params.m_dstAddr
2352 << "]");
2353 }
2354 else if (receivedMacHdr.GetSrcAddrMode() == EXT_ADDR &&
2355 receivedMacHdr.GetDstAddrMode() == EXT_ADDR)
2356 {
2357 NS_LOG_DEBUG("Packet from [" << params.m_srcExtAddr << "] to ["
2358 << params.m_dstExtAddr << "]");
2359 }
2360 else if (receivedMacHdr.GetSrcAddrMode() == SHORT_ADDR &&
2361 receivedMacHdr.GetDstAddrMode() == EXT_ADDR)
2362 {
2363 NS_LOG_DEBUG("Packet from [" << params.m_srcAddr << "] to ["
2364 << params.m_dstExtAddr << "]");
2365 }
2366 else if (receivedMacHdr.GetSrcAddrMode() == EXT_ADDR &&
2367 receivedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2368 {
2369 NS_LOG_DEBUG("Packet from [" << params.m_srcExtAddr << "] to ["
2370 << params.m_dstAddr << "]");
2371 }
2372
2373 if (receivedMacHdr.IsBeacon())
2374 {
2375 ReceiveBeacon(lqi, originalPkt);
2376 }
2377 else if (receivedMacHdr.IsCommand())
2378 {
2379 // Handle the reception of frame commands that do not require ACK
2380 // (i.e. Beacon Request, Orphan notification, Coordinator Realigment)
2381 CommandPayloadHeader receivedMacPayload;
2382 p->PeekHeader(receivedMacPayload);
2383
2384 switch (receivedMacPayload.GetCommandFrameType())
2385 {
2387 if (m_csmaCa->IsUnSlottedCsmaCa() && m_coor)
2388 {
2389 // Jitter = Between 0 and 2 aUnitBackoffPeriods
2390 // (0, 320us or 640us in 2.4Ghz O-QPSK)
2391 // While this jitter is not described by the standard,
2392 // it reduces the probability of collisions in beacons
2393 // transmitted as a result of a beacon request
2394 Time jitter =
2395 Seconds(static_cast<double>(m_uniformVar->GetInteger(0, 3) *
2397 symbolRate);
2398
2400 }
2401 else
2402 {
2403 m_macRxDropTrace(originalPkt);
2404 }
2405 break;
2408 {
2409 if (m_coor)
2410 {
2411 MlmeOrphanIndicationParams orphanParams;
2412 orphanParams.m_orphanAddr = receivedMacHdr.GetExtSrcAddr();
2413 m_mlmeOrphanIndicationCallback(orphanParams);
2414 }
2415 }
2416 break;
2419 {
2420 // Coordinator located, no need to keep scanning other channels
2422
2423 m_macPanIdScan = 0;
2426
2427 // Update the device information with the received information
2428 // from the Coordinator Realigment command.
2429 m_macPanId = receivedMacPayload.GetPanId();
2430 m_shortAddress = receivedMacPayload.GetShortAddr();
2431 m_macCoordExtendedAddress = receivedMacHdr.GetExtSrcAddr();
2432 m_macCoordShortAddress = receivedMacPayload.GetCoordShortAddr();
2433
2435 {
2436 MlmeScanConfirmParams confirmParams;
2437 confirmParams.m_scanType = m_scanParams.m_scanType;
2438 confirmParams.m_chPage = m_scanParams.m_chPage;
2439 confirmParams.m_status = MacStatus::SUCCESS;
2440 m_mlmeScanConfirmCallback(confirmParams);
2441 }
2442 m_scanParams = {};
2443 }
2444 // TODO: handle Coordinator realignment when not
2445 // used during an orphan scan.
2446 break;
2447 default:
2448 m_macRxDropTrace(originalPkt);
2449 break;
2450 }
2451 }
2452 else if (receivedMacHdr.IsData() && !m_mcpsDataIndicationCallback.IsNull())
2453 {
2454 // If it is a data frame, push it up the stack.
2455 NS_LOG_DEBUG("Data Packet is for me; forwarding up");
2457 }
2458 else if (receivedMacHdr.IsAcknowledgment() && m_txPkt &&
2460 {
2461 LrWpanMacHeader peekedMacHdr;
2462 m_txPkt->PeekHeader(peekedMacHdr);
2463 // If it is an ACK with the expected sequence number, finish the transmission
2464 if (receivedMacHdr.GetSeqNum() == peekedMacHdr.GetSeqNum())
2465 {
2468
2469 // TODO: check if the IFS is the correct size after ACK.
2470 Time ifsWaitTime = Seconds((double)GetIfsSize() / symbolRate);
2471
2472 // We received an ACK to a command
2473 if (peekedMacHdr.IsCommand())
2474 {
2475 // check the original sent command frame which belongs to this received
2476 // ACK
2477 Ptr<Packet> pkt = m_txPkt->Copy();
2478 LrWpanMacHeader macHdr;
2479 CommandPayloadHeader cmdPayload;
2480 pkt->RemoveHeader(macHdr);
2481 pkt->RemoveHeader(cmdPayload);
2482
2483 switch (cmdPayload.GetCommandFrameType())
2484 {
2486 double symbolRate = m_phy->GetDataOrSymbolRate(false);
2487 Time waitTime = Seconds(static_cast<double>(m_macResponseWaitTime) /
2488 symbolRate);
2489 if (!m_beaconTrackingOn)
2490 {
2492 Simulator::Schedule(waitTime,
2494 this);
2495 }
2496 else
2497 {
2498 // TODO: The data must be extracted by the coordinator within
2499 // macResponseWaitTime on timeout, MLME-ASSOCIATE.confirm is set
2500 // with status NO_DATA, and this should trigger the cancellation
2501 // of the beacon tracking (MLME-SYNC.request trackBeacon
2502 // =FALSE)
2503 }
2504 break;
2505 }
2506
2508 // MLME-comm-status.Indication generated as a result of an
2509 // association response command, therefore src and dst address use
2510 // extended mode (see 5.3.2.1)
2512 {
2513 MlmeCommStatusIndicationParams commStatusParams;
2514 commStatusParams.m_panId = m_macPanId;
2515 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2516 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
2517 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2518 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
2519 commStatusParams.m_status = MacStatus::SUCCESS;
2520 m_mlmeCommStatusIndicationCallback(commStatusParams);
2521 }
2522 // Remove element from Pending Transaction List
2524 break;
2525 }
2526
2528 if (!m_ignoreDataCmdAck)
2529 {
2530 // Schedule an event in case the Association Response Command
2531 // never reached this device during an association process.
2532 double symbolRate = m_phy->GetDataOrSymbolRate(false);
2533 Time waitTime = Seconds(
2534 static_cast<double>(m_assocRespCmdWaitTime) / symbolRate);
2536 Simulator::Schedule(waitTime,
2538 this);
2539 }
2540
2542 {
2543 MlmePollConfirmParams pollConfirmParams;
2544 pollConfirmParams.m_status = MacStatus::SUCCESS;
2545 m_mlmePollConfirmCallback(pollConfirmParams);
2546 }
2547 break;
2548 }
2549
2551 // ACK of coordinator realigment commands is not specified in the
2552 // standard, in here, we assume they are required as in other
2553 // commands.
2555 {
2556 MlmeCommStatusIndicationParams commStatusParams;
2557 commStatusParams.m_panId = m_macPanId;
2558 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2559 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
2560 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2561 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
2562 commStatusParams.m_status = MacStatus::SUCCESS;
2563 m_mlmeCommStatusIndicationCallback(commStatusParams);
2564 }
2565 }
2566
2567 default: {
2568 // TODO: add response to other request commands (e.g. Orphan)
2569 break;
2570 }
2571 }
2572 }
2573 else
2574 {
2576 {
2577 Ptr<TxQueueElement> txQElement = m_txQueue.front();
2578 McpsDataConfirmParams confirmParams;
2579 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
2580 confirmParams.m_status = MacStatus::SUCCESS;
2581 m_mcpsDataConfirmCallback(confirmParams);
2582 }
2583 }
2584
2585 // Ack was successfully received, wait for the Interframe Space (IFS) and
2586 // then proceed
2591 m_ifsEvent = Simulator::Schedule(ifsWaitTime,
2593 this,
2594 ifsWaitTime);
2595 }
2596 else
2597 {
2598 // If it is an ACK with an unexpected sequence number, mark the current
2599 // transmission as failed and start a retransmit. (cf 7.5.6.4.3)
2601 if (!PrepareRetransmission())
2602 {
2605 this,
2606 MAC_IDLE);
2607 }
2608 else
2609 {
2612 this,
2613 MAC_CSMA);
2614 }
2615 }
2616 }
2617 }
2618 else
2619 {
2620 m_macRxDropTrace(originalPkt);
2621 }
2622 }
2623 }
2624}
2625
2626void
2628{
2629 NS_LOG_FUNCTION(this << static_cast<uint32_t>(seqno));
2630
2632
2633 // Generate a corresponding ACK Frame.
2635 LrWpanMacTrailer macTrailer;
2636 Ptr<Packet> ackPacket = Create<Packet>(0);
2637 ackPacket->AddHeader(macHdr);
2638 // Calculate FCS if the global attribute ChecksumEnabled is set.
2640 {
2641 macTrailer.EnableFcs(true);
2642 macTrailer.SetFcs(ackPacket);
2643 }
2644 ackPacket->AddTrailer(macTrailer);
2645
2646 // Enqueue the ACK packet for further processing
2647 // when the transmitter is activated.
2648 m_txPkt = ackPacket;
2649
2650 // Switch transceiver to TX mode. Proceed sending the Ack on confirm.
2652 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
2653}
2654
2655void
2657{
2658 if (m_txQueue.size() < m_maxTxQueueSize)
2659 {
2660 m_txQueue.emplace_back(txQElement);
2661 m_macTxEnqueueTrace(txQElement->txQPkt);
2662 }
2663 else
2664 {
2666 {
2667 McpsDataConfirmParams confirmParams;
2668 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
2670 m_mcpsDataConfirmCallback(confirmParams);
2671 }
2672 NS_LOG_DEBUG("TX Queue with size " << m_txQueue.size() << " is full, dropping packet");
2673 m_macTxDropTrace(txQElement->txQPkt);
2674 }
2675}
2676
2677void
2679{
2680 Ptr<TxQueueElement> txQElement = m_txQueue.front();
2681 Ptr<const Packet> p = txQElement->txQPkt;
2682 m_numCsmacaRetry += m_csmaCa->GetNB() + 1;
2683
2684 Ptr<Packet> pkt = p->Copy();
2685 LrWpanMacHeader hdr;
2686 pkt->RemoveHeader(hdr);
2687 if (!hdr.GetShortDstAddr().IsBroadcast() && !hdr.GetShortDstAddr().IsMulticast())
2688 {
2690 }
2691
2692 txQElement->txQPkt = nullptr;
2693 txQElement = nullptr;
2694 m_txQueue.pop_front();
2695 m_txPkt = nullptr;
2696 m_retransmission = 0;
2697 m_numCsmacaRetry = 0;
2699}
2700
2701void
2703{
2704 NS_LOG_FUNCTION(this);
2705
2706 // TODO: If we are a PAN coordinator and this was an indirect transmission,
2707 // we will not initiate a retransmission. Instead we wait for the data
2708 // being extracted after a new data request command.
2709 if (!PrepareRetransmission())
2710 {
2712 }
2713 else
2714 {
2716 }
2717}
2718
2719void
2721{
2722 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false);
2723 Time lifsTime = Seconds((double)m_macLIFSPeriod / symbolRate);
2724 Time sifsTime = Seconds((double)m_macSIFSPeriod / symbolRate);
2725
2726 if (ifsTime == lifsTime)
2727 {
2728 NS_LOG_DEBUG("LIFS of " << m_macLIFSPeriod << " symbols (" << ifsTime.As(Time::S)
2729 << ") completed ");
2730 }
2731 else if (ifsTime == sifsTime)
2732 {
2733 NS_LOG_DEBUG("SIFS of " << m_macSIFSPeriod << " symbols (" << ifsTime.As(Time::S)
2734 << ") completed ");
2735 }
2736 else
2737 {
2738 NS_LOG_DEBUG("Unknown IFS size (" << ifsTime.As(Time::S) << ") completed ");
2739 }
2740
2741 m_macIfsEndTrace(ifsTime);
2742 CheckQueue();
2743}
2744
2745bool
2747{
2748 NS_LOG_FUNCTION(this);
2749
2750 // Max retransmissions reached without receiving ACK,
2751 // send the proper indication/confirmation
2752 // according to the frame type and call drop trace.
2754 {
2755 LrWpanMacHeader peekedMacHdr;
2756 m_txPkt->PeekHeader(peekedMacHdr);
2757
2758 if (peekedMacHdr.IsCommand())
2759 {
2761
2762 Ptr<Packet> pkt = m_txPkt->Copy();
2763 LrWpanMacHeader macHdr;
2764 CommandPayloadHeader cmdPayload;
2765 pkt->RemoveHeader(macHdr);
2766 pkt->RemoveHeader(cmdPayload);
2767
2768 switch (cmdPayload.GetCommandFrameType())
2769 {
2771 m_macPanId = 0xffff;
2773 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
2776 m_csmaCa->SetUnSlottedCsmaCa();
2779
2781 {
2782 MlmeAssociateConfirmParams confirmParams;
2783 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
2784 confirmParams.m_status = MacStatus::NO_ACK;
2785 m_mlmeAssociateConfirmCallback(confirmParams);
2786 }
2787 break;
2788 }
2790 // IEEE 802.15.4-2006 (Section 7.1.3.3.3 and 7.1.8.2.3)
2792 {
2793 MlmeCommStatusIndicationParams commStatusParams;
2794 commStatusParams.m_panId = m_macPanId;
2795 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2796 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
2797 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2798 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
2799 commStatusParams.m_status = MacStatus::NO_ACK;
2800 m_mlmeCommStatusIndicationCallback(commStatusParams);
2801 }
2803 break;
2804 }
2806 // IEEE 802.15.4-2006 (Section 7.1.16.1.3)
2807 m_macPanId = 0xffff;
2809 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
2812 m_csmaCa->SetUnSlottedCsmaCa();
2815
2817 {
2818 MlmePollConfirmParams pollConfirmParams;
2819 pollConfirmParams.m_status = MacStatus::NO_ACK;
2820 m_mlmePollConfirmCallback(pollConfirmParams);
2821 }
2822 break;
2823 }
2824 default: {
2825 // TODO: Specify other indications according to other commands
2826 break;
2827 }
2828 }
2829 }
2830 else
2831 {
2832 // Maximum number of retransmissions has been reached.
2833 // remove the copy of the DATA packet that was just sent
2834 Ptr<TxQueueElement> txQElement = m_txQueue.front();
2835 m_macTxDropTrace(txQElement->txQPkt);
2837 {
2838 McpsDataConfirmParams confirmParams;
2839 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
2840 confirmParams.m_status = MacStatus::NO_ACK;
2841 m_mcpsDataConfirmCallback(confirmParams);
2842 }
2843 }
2844
2846 return false;
2847 }
2848 else
2849 {
2851 m_numCsmacaRetry += m_csmaCa->GetNB() + 1;
2852 // Start next CCA process for this packet.
2853 return true;
2854 }
2855}
2856
2857void
2859{
2861 LrWpanMacHeader peekedMacHdr;
2862 p->PeekHeader(peekedMacHdr);
2863
2864 PurgeInd();
2865
2866 NS_ASSERT(peekedMacHdr.GetDstAddrMode() == SHORT_ADDR ||
2867 peekedMacHdr.GetDstAddrMode() == EXT_ADDR);
2868
2869 if (peekedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2870 {
2871 indTxQElement->dstShortAddress = peekedMacHdr.GetShortDstAddr();
2872 }
2873 else
2874 {
2875 indTxQElement->dstExtAddress = peekedMacHdr.GetExtDstAddr();
2876 }
2877
2878 indTxQElement->seqNum = peekedMacHdr.GetSeqNum();
2879
2880 // See IEEE 802.15.4-2006, Table 86
2881 uint32_t unit = 0; // The persistence time in symbols
2882 if (m_macBeaconOrder == 15)
2883 {
2884 // Non-beacon enabled mode
2886 }
2887 else
2888 {
2889 // Beacon-enabled mode
2890 unit = ((static_cast<uint32_t>(1) << m_macBeaconOrder) * lrwpan::aBaseSuperframeDuration) *
2892 }
2893
2894 if (m_indTxQueue.size() < m_maxIndTxQueueSize)
2895 {
2896 double symbolRate = m_phy->GetDataOrSymbolRate(false);
2897 Time expireTime = Seconds(unit / symbolRate);
2898 expireTime += Simulator::Now();
2899 indTxQElement->expireTime = expireTime;
2900 indTxQElement->txQPkt = p;
2901 m_indTxQueue.emplace_back(indTxQElement);
2903 }
2904 else
2905 {
2907 {
2908 LrWpanMacHeader peekedMacHdr;
2909 indTxQElement->txQPkt->PeekHeader(peekedMacHdr);
2910 MlmeCommStatusIndicationParams commStatusParams;
2911 commStatusParams.m_panId = m_macPanId;
2912 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2913 commStatusParams.m_srcExtAddr = peekedMacHdr.GetExtSrcAddr();
2914 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2915 commStatusParams.m_dstExtAddr = peekedMacHdr.GetExtDstAddr();
2916 commStatusParams.m_status = MacStatus::TRANSACTION_OVERFLOW;
2917 m_mlmeCommStatusIndicationCallback(commStatusParams);
2918 }
2920 }
2921}
2922
2923bool
2925{
2926 PurgeInd();
2927
2928 for (auto iter = m_indTxQueue.begin(); iter != m_indTxQueue.end(); iter++)
2929 {
2930 if ((*iter)->dstExtAddress == dst)
2931 {
2932 *entry = **iter;
2933 m_macIndTxDequeueTrace((*iter)->txQPkt->Copy());
2934 m_indTxQueue.erase(iter);
2935 return true;
2936 }
2937 }
2938 return false;
2939}
2940
2941void
2943{
2944 for (uint32_t i = 0; i < m_indTxQueue.size();)
2945 {
2946 if (Simulator::Now() > m_indTxQueue[i]->expireTime)
2947 {
2948 // Transaction expired, remove and send proper confirmation/indication to a higher layer
2949 LrWpanMacHeader peekedMacHdr;
2950 m_indTxQueue[i]->txQPkt->PeekHeader(peekedMacHdr);
2951
2952 if (peekedMacHdr.IsCommand())
2953 {
2954 // IEEE 802.15.4-2006 (Section 7.1.3.3.3)
2956 {
2957 MlmeCommStatusIndicationParams commStatusParams;
2958 commStatusParams.m_panId = m_macPanId;
2959 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2960 commStatusParams.m_srcExtAddr = peekedMacHdr.GetExtSrcAddr();
2961 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2962 commStatusParams.m_dstExtAddr = peekedMacHdr.GetExtDstAddr();
2963 commStatusParams.m_status = MacStatus::TRANSACTION_EXPIRED;
2964 m_mlmeCommStatusIndicationCallback(commStatusParams);
2965 }
2966 }
2967 else if (peekedMacHdr.IsData())
2968 {
2969 // IEEE 802.15.4-2006 (Section 7.1.1.1.3)
2971 {
2972 McpsDataConfirmParams confParams;
2974 m_mcpsDataConfirmCallback(confParams);
2975 }
2976 }
2977 m_macIndTxDropTrace(m_indTxQueue[i]->txQPkt->Copy());
2978 m_indTxQueue.erase(m_indTxQueue.begin() + i);
2979 }
2980 else
2981 {
2982 i++;
2983 }
2984 }
2985}
2986
2987void
2988LrWpanMac::PrintPendingTxQueue(std::ostream& os) const
2989{
2990 LrWpanMacHeader peekedMacHdr;
2991
2992 os << "Pending Transaction List [" << GetShortAddress() << " | " << GetExtendedAddress()
2993 << "] | CurrentTime: " << Simulator::Now().As(Time::S) << "\n"
2994 << " Destination |"
2995 << " Sequence Number |"
2996 << " Frame type |"
2997 << " Expire time\n";
2998
2999 for (auto transaction : m_indTxQueue)
3000 {
3001 transaction->txQPkt->PeekHeader(peekedMacHdr);
3002 os << transaction->dstExtAddress << " "
3003 << static_cast<uint32_t>(transaction->seqNum) << " ";
3004
3005 if (peekedMacHdr.IsCommand())
3006 {
3007 os << " Command Frame ";
3008 }
3009 else if (peekedMacHdr.IsData())
3010 {
3011 os << " Data Frame ";
3012 }
3013 else
3014 {
3015 os << " Unknown Frame ";
3016 }
3017
3018 os << transaction->expireTime.As(Time::S) << "\n";
3019 }
3020}
3021
3022void
3023LrWpanMac::PrintTxQueue(std::ostream& os) const
3024{
3025 LrWpanMacHeader peekedMacHdr;
3026
3027 os << "\nTx Queue [" << GetShortAddress() << " | " << GetExtendedAddress()
3028 << "] | CurrentTime: " << Simulator::Now().As(Time::S) << "\n"
3029 << " Destination |"
3030 << " Sequence Number |"
3031 << " Dst PAN id |"
3032 << " Frame type |\n";
3033
3034 for (auto transaction : m_txQueue)
3035 {
3036 transaction->txQPkt->PeekHeader(peekedMacHdr);
3037
3038 os << "[" << peekedMacHdr.GetShortDstAddr() << "]"
3039 << ", [" << peekedMacHdr.GetExtDstAddr() << "] "
3040 << static_cast<uint32_t>(peekedMacHdr.GetSeqNum()) << " "
3041 << peekedMacHdr.GetDstPanId() << " ";
3042
3043 if (peekedMacHdr.IsCommand())
3044 {
3045 os << " Command Frame ";
3046 }
3047 else if (peekedMacHdr.IsData())
3048 {
3049 os << " Data Frame ";
3050 }
3051 else
3052 {
3053 os << " Unknown Frame ";
3054 }
3055
3056 os << "\n";
3057 }
3058 os << "\n";
3059}
3060
3061int64_t
3063{
3064 NS_LOG_FUNCTION(this);
3065 m_uniformVar->SetStream(stream);
3066 m_csmaCa->AssignStreams(stream + 1);
3067 return 2;
3068}
3069
3070void
3072{
3073 LrWpanMacHeader peekedMacHdr;
3074 p->PeekHeader(peekedMacHdr);
3075
3076 for (auto it = m_indTxQueue.begin(); it != m_indTxQueue.end(); it++)
3077 {
3078 if (peekedMacHdr.GetDstAddrMode() == EXT_ADDR)
3079 {
3080 if (((*it)->dstExtAddress == peekedMacHdr.GetExtDstAddr()) &&
3081 ((*it)->seqNum == peekedMacHdr.GetSeqNum()))
3082 {
3084 m_indTxQueue.erase(it);
3085 break;
3086 }
3087 }
3088 else if (peekedMacHdr.GetDstAddrMode() == SHORT_ADDR)
3089 {
3090 if (((*it)->dstShortAddress == peekedMacHdr.GetShortDstAddr()) &&
3091 ((*it)->seqNum == peekedMacHdr.GetSeqNum()))
3092 {
3094 m_indTxQueue.erase(it);
3095 break;
3096 }
3097 }
3098 }
3099
3100 p = nullptr;
3101}
3102
3103void
3105{
3107 NS_LOG_FUNCTION(this << status << m_txQueue.size());
3108
3109 LrWpanMacHeader macHdr;
3110 Time ifsWaitTime;
3111 double symbolRate;
3112
3113 symbolRate = m_phy->GetDataOrSymbolRate(false); // symbols per second
3114
3115 m_txPkt->PeekHeader(macHdr);
3116
3117 if (status == IEEE_802_15_4_PHY_SUCCESS)
3118 {
3119 if (!macHdr.IsAcknowledgment())
3120 {
3121 if (macHdr.IsBeacon())
3122 {
3123 // Start CAP only if we are in beacon mode (i.e. if slotted csma-ca is running)
3124 if (m_csmaCa->IsSlottedCsmaCa())
3125 {
3126 // The Tx Beacon in symbols
3127 // Beacon = 5 bytes Sync Header (SHR) + 1 byte PHY header (PHR) + PSDU (default
3128 // 17 bytes)
3129 uint64_t beaconSymbols = m_phy->GetPhySHRDuration() +
3130 1 * m_phy->GetPhySymbolsPerOctet() +
3131 (m_txPkt->GetSize() * m_phy->GetPhySymbolsPerOctet());
3132
3133 // The beacon Tx time and start of the Outgoing superframe Active Period
3135 Simulator::Now() - Seconds(static_cast<double>(beaconSymbols) / symbolRate);
3136
3138 this,
3140 NS_LOG_DEBUG("Beacon Sent (m_macBeaconTxTime: " << m_macBeaconTxTime.As(Time::S)
3141 << ")");
3142
3144 {
3145 MlmeStartConfirmParams mlmeConfirmParams;
3146 mlmeConfirmParams.m_status = MacStatus::SUCCESS;
3147 m_mlmeStartConfirmCallback(mlmeConfirmParams);
3148 }
3149 }
3150
3151 ifsWaitTime = Seconds(static_cast<double>(GetIfsSize()) / symbolRate);
3152
3153 if (m_csmaCa->IsSlottedCsmaCa())
3154 {
3155 // The beacon was sent immediately in beacon-enabled mode
3156 m_txPkt = nullptr;
3157 }
3158 else
3159 {
3160 // The beacon was sent using CSMA/CA as a result of a beacon request
3161 // therefore, remove it from TX Queue
3163 }
3164 }
3165 else if (macHdr.IsAckReq()) // We have sent a regular data packet, check if we have to
3166 // wait for an ACK.
3167 {
3168 // we sent a regular data frame or command frame (e.g. AssocReq command) that
3169 // require ACK wait for the ack or the next retransmission timeout start
3170 // retransmission timer
3171 Time waitTime = Seconds(static_cast<double>(GetMacAckWaitDuration()) / symbolRate);
3177 return;
3178 }
3179 else if (macHdr.IsCommand())
3180 {
3181 // We handle commands that do not require ACK
3182 // (e.g. Coordinator realigment command in an orphan response)
3183 // Other command with ACK required are handle by the previous if statement.
3184
3185 // Check the transmitted packet command type and issue the appropriate indication.
3186 Ptr<Packet> txOriginalPkt = m_txPkt->Copy();
3187 LrWpanMacHeader txMacHdr;
3188 txOriginalPkt->RemoveHeader(txMacHdr);
3189 CommandPayloadHeader txMacPayload;
3190 txOriginalPkt->RemoveHeader(txMacPayload);
3191
3193 {
3195 {
3196 MlmeCommStatusIndicationParams commStatusParams;
3197 commStatusParams.m_panId = m_macPanId;
3198
3199 commStatusParams.m_srcAddrMode = macHdr.GetSrcAddrMode();
3200 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
3201 commStatusParams.m_srcShortAddr = macHdr.GetShortSrcAddr();
3202
3203 commStatusParams.m_dstAddrMode = macHdr.GetDstAddrMode();
3204 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
3205 commStatusParams.m_dstShortAddr = macHdr.GetShortDstAddr();
3206
3207 commStatusParams.m_status = MacStatus::SUCCESS;
3208 m_mlmeCommStatusIndicationCallback(commStatusParams);
3209 }
3210 }
3211
3212 ifsWaitTime = Seconds(static_cast<double>(GetIfsSize()) / symbolRate);
3214 }
3215 else
3216 {
3218 // remove the copy of the packet that was just sent
3220 {
3221 McpsDataConfirmParams confirmParams;
3222 NS_ASSERT_MSG(!m_txQueue.empty(), "TxQsize = 0");
3223 Ptr<TxQueueElement> txQElement = m_txQueue.front();
3224 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
3225 confirmParams.m_status = MacStatus::SUCCESS;
3226 m_mcpsDataConfirmCallback(confirmParams);
3227 }
3228 ifsWaitTime = Seconds(static_cast<double>(GetIfsSize()) / symbolRate);
3230 }
3231 }
3232 else
3233 {
3234 // The packet sent was a successful ACK
3235
3236 // Check the received frame before the transmission of the ACK,
3237 // and send the appropriate Indication or Confirmation
3238 Ptr<Packet> recvOriginalPkt = m_rxPkt->Copy();
3239 LrWpanMacHeader receivedMacHdr;
3240 recvOriginalPkt->RemoveHeader(receivedMacHdr);
3241
3242 if (receivedMacHdr.IsCommand())
3243 {
3244 CommandPayloadHeader receivedMacPayload;
3245 recvOriginalPkt->RemoveHeader(receivedMacPayload);
3246
3247 if (receivedMacPayload.GetCommandFrameType() ==
3249 {
3251 {
3252 // NOTE: The LQI parameter is not part of the standard but found
3253 // in some implementations as is required for higher layers (See Zboss
3254 // implementation).
3255 MlmeAssociateIndicationParams associateParams;
3256 associateParams.capabilityInfo = receivedMacPayload.GetCapabilityField();
3257 associateParams.m_extDevAddr = receivedMacHdr.GetExtSrcAddr();
3258 associateParams.lqi = m_lastRxFrameLqi;
3259 m_mlmeAssociateIndicationCallback(associateParams);
3260 }
3261
3262 // Clear the packet buffer for the packet request received.
3263 m_rxPkt = nullptr;
3264 }
3265 else if (receivedMacPayload.GetCommandFrameType() ==
3267 {
3268 MlmeAssociateConfirmParams confirmParams;
3269
3270 switch (static_cast<MacStatus>(receivedMacPayload.GetAssociationStatus()))
3271 {
3272 case MacStatus::SUCCESS:
3273 // The assigned short address by the coordinator
3274 SetShortAddress(receivedMacPayload.GetShortAddr());
3275 m_macPanId = receivedMacHdr.GetSrcPanId();
3276
3277 confirmParams.m_status = MacStatus::SUCCESS;
3278 confirmParams.m_assocShortAddr = GetShortAddress();
3279 break;
3281 confirmParams.m_status = MacStatus::FULL_CAPACITY;
3282 m_macPanId = 0xffff;
3284 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3287 m_csmaCa->SetUnSlottedCsmaCa();
3290 break;
3292 default:
3293 confirmParams.m_status = MacStatus::ACCESS_DENIED;
3294 m_macPanId = 0xffff;
3296 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3299 m_csmaCa->SetUnSlottedCsmaCa();
3302 break;
3303 }
3304
3306 {
3307 m_mlmeAssociateConfirmCallback(confirmParams);
3308 }
3309 }
3310 else if (receivedMacPayload.GetCommandFrameType() == CommandPayloadHeader::DATA_REQ)
3311 {
3312 // We enqueue the the Assoc Response command frame in the Tx queue
3313 // and the packet is transmitted as soon as the PHY is free and the IFS have
3314 // taken place.
3316 }
3317 }
3318
3319 // Clear the packet buffer for the ACK packet sent.
3320 m_txPkt = nullptr;
3321 }
3322 }
3323 else if (status == IEEE_802_15_4_PHY_UNSPECIFIED)
3324 {
3325 if (!macHdr.IsAcknowledgment())
3326 {
3327 NS_ASSERT_MSG(!m_txQueue.empty(), "TxQsize = 0");
3328 Ptr<TxQueueElement> txQElement = m_txQueue.front();
3329 m_macTxDropTrace(txQElement->txQPkt);
3331 {
3332 McpsDataConfirmParams confirmParams;
3333 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
3334 confirmParams.m_status = MacStatus::FRAME_TOO_LONG;
3335 m_mcpsDataConfirmCallback(confirmParams);
3336 }
3338 }
3339 else
3340 {
3341 NS_LOG_ERROR("Unable to send ACK");
3342 }
3343 }
3344 else
3345 {
3346 // Something went really wrong. The PHY is not in the correct state for
3347 // data transmission.
3348 NS_FATAL_ERROR("Transmission attempt failed with PHY status " << status);
3349 }
3350
3351 if (!ifsWaitTime.IsZero())
3352 {
3353 m_ifsEvent =
3354 Simulator::Schedule(ifsWaitTime, &LrWpanMac::IfsWaitTimeout, this, ifsWaitTime);
3355 }
3356
3359}
3360
3361void
3363{
3364 NS_LOG_FUNCTION(this << status);
3365 // Direct this call through the csmaCa object
3366 m_csmaCa->PlmeCcaConfirm(status);
3367}
3368
3369void
3370LrWpanMac::PlmeEdConfirm(PhyEnumeration status, uint8_t energyLevel)
3371{
3372 NS_LOG_FUNCTION(this << status << energyLevel);
3373
3374 if (energyLevel > m_maxEnergyLevel)
3375 {
3376 m_maxEnergyLevel = energyLevel;
3377 }
3378
3380 Seconds(8.0 / m_phy->GetDataOrSymbolRate(false)))
3381 {
3382 m_phy->PlmeEdRequest();
3383 }
3384}
3385
3386void
3389 Ptr<PhyPibAttributes> attribute)
3390{
3391 NS_LOG_FUNCTION(this << status << id << attribute);
3392}
3393
3394void
3396{
3397 NS_LOG_FUNCTION(this << status);
3398
3399 if (m_macState == MAC_SENDING &&
3400 (status == IEEE_802_15_4_PHY_TX_ON || status == IEEE_802_15_4_PHY_SUCCESS))
3401 {
3403
3404 // Start sending if we are in state SENDING and the PHY transmitter was enabled.
3408 m_phy->PdDataRequest(m_txPkt->GetSize(), m_txPkt);
3409 }
3410 else if (m_macState == MAC_CSMA &&
3411 (status == IEEE_802_15_4_PHY_RX_ON || status == IEEE_802_15_4_PHY_SUCCESS))
3412 {
3413 // Start the CSMA algorithm as soon as the receiver is enabled.
3414 m_csmaCa->Start();
3415 }
3416 else if (m_macState == MAC_IDLE)
3417 {
3419 status == IEEE_802_15_4_PHY_TRX_OFF);
3420
3422 {
3423 // Kick start Energy Detection Scan
3424 m_phy->PlmeEdRequest();
3425 }
3426 else if (status == IEEE_802_15_4_PHY_RX_ON || status == IEEE_802_15_4_PHY_SUCCESS)
3427 {
3428 // Check if there is not messages to transmit when going idle
3429 CheckQueue();
3430 }
3431 }
3432 else if (m_macState == MAC_ACK_PENDING)
3433 {
3435 }
3436 else
3437 {
3438 // TODO: What to do when we receive an error?
3439 // If we want to transmit a packet, but switching the transceiver on results
3440 // in an error, we have to recover somehow (and start sending again).
3441 NS_FATAL_ERROR("Error changing transceiver state");
3442 }
3443}
3444
3445void
3447{
3448 NS_LOG_FUNCTION(this << status << id);
3450 {
3452 {
3453 // get the first channel to scan from scan channel list
3454 bool channelFound = false;
3455 for (int i = m_channelScanIndex; i <= 26; i++)
3456 {
3458 {
3459 channelFound = true;
3460 break;
3461 }
3463 }
3464
3465 if (channelFound)
3466 {
3468 pibAttr->phyCurrentChannel = m_channelScanIndex;
3469 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel,
3470 pibAttr);
3471 }
3472 }
3473 else
3474 {
3476 {
3477 MlmeScanConfirmParams confirmParams;
3478 confirmParams.m_scanType = m_scanParams.m_scanType;
3479 confirmParams.m_chPage = m_scanParams.m_chPage;
3480 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3481 m_mlmeScanConfirmCallback(confirmParams);
3482 }
3483 NS_LOG_ERROR(this << "Channel Scan: Invalid channel page");
3484 }
3485 }
3487 {
3489 {
3490 auto symbolRate = static_cast<uint64_t>(m_phy->GetDataOrSymbolRate(false));
3491 Time nextScanTime;
3492
3494 {
3495 nextScanTime = Seconds(static_cast<double>(m_macResponseWaitTime) / symbolRate);
3496 }
3497 else
3498 {
3499 uint64_t scanDurationSym =
3501
3502 nextScanTime = Seconds(static_cast<double>(scanDurationSym) / symbolRate);
3503 }
3504
3505 switch (m_scanParams.m_scanType)
3506 {
3507 case MLMESCAN_ED:
3508 m_maxEnergyLevel = 0;
3511 // set phy to RX_ON and kick start the first PLME-ED.request
3512 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3513 break;
3514 case MLMESCAN_ACTIVE:
3517 break;
3518 case MLMESCAN_PASSIVE:
3520 // turn back the phy to RX_ON after setting Page/channel
3521 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3522 break;
3523 case MLMESCAN_ORPHAN:
3525 Simulator::Schedule(nextScanTime, &LrWpanMac::EndChannelScan, this);
3527 break;
3528
3529 default:
3530 MlmeScanConfirmParams confirmParams;
3531 confirmParams.m_scanType = m_scanParams.m_scanType;
3532 confirmParams.m_chPage = m_scanParams.m_chPage;
3533 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3535 {
3536 m_mlmeScanConfirmCallback(confirmParams);
3537 }
3538 NS_LOG_ERROR("Scan Type currently not supported");
3539 return;
3540 }
3541 }
3542 else
3543 {
3545 {
3546 MlmeScanConfirmParams confirmParams;
3547 confirmParams.m_scanType = m_scanParams.m_scanType;
3548 confirmParams.m_chPage = m_scanParams.m_chPage;
3549 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3550 m_mlmeScanConfirmCallback(confirmParams);
3551 }
3552 NS_LOG_ERROR("Channel " << m_channelScanIndex
3553 << " could not be set in the current page");
3554 }
3555 }
3557 {
3559 {
3561 pibAttr->phyCurrentChannel = m_startParams.m_logCh;
3562 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
3563 }
3564 else
3565 {
3567 {
3568 MlmeStartConfirmParams confirmParams;
3569 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3570 m_mlmeStartConfirmCallback(confirmParams);
3571 }
3572 NS_LOG_ERROR("Invalid page parameter in MLME-start");
3573 }
3574 }
3577 {
3579 {
3581 }
3582 else
3583 {
3585 {
3586 MlmeStartConfirmParams confirmParams;
3587 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3588 m_mlmeStartConfirmCallback(confirmParams);
3589 }
3590 NS_LOG_ERROR("Invalid channel parameter in MLME-start");
3591 }
3592 }
3594 {
3596 {
3598 pibAttr->phyCurrentChannel = m_associateParams.m_chNum;
3599 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
3600 }
3601 else
3602 {
3603 m_macPanId = 0xffff;
3605 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3608 m_csmaCa->SetUnSlottedCsmaCa();
3611
3613 {
3614 MlmeAssociateConfirmParams confirmParams;
3615 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
3616 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3617 m_mlmeAssociateConfirmCallback(confirmParams);
3618 }
3619 NS_LOG_ERROR("Invalid page parameter in MLME-associate");
3620 }
3621 }
3624 {
3626 {
3628 }
3629 else
3630 {
3631 m_macPanId = 0xffff;
3633 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3636 m_csmaCa->SetUnSlottedCsmaCa();
3639
3641 {
3642 MlmeAssociateConfirmParams confirmParams;
3643 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
3644 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3645 m_mlmeAssociateConfirmCallback(confirmParams);
3646 }
3647 NS_LOG_ERROR("Invalid channel parameter in MLME-associate");
3648 }
3649 }
3650 else
3651 {
3653 {
3654 MlmeSetConfirmParams confirmParams;
3656 {
3657 confirmParams.m_status = MacStatus::SUCCESS;
3658 }
3659 else
3660 {
3662 }
3663
3665 {
3667 }
3669 {
3671 }
3672
3673 m_mlmeSetConfirmCallback(confirmParams);
3674 }
3675 }
3676}
3677
3678void
3680{
3681 NS_LOG_FUNCTION(this << "mac state = " << macState);
3682
3683 if (macState == MAC_IDLE)
3684 {
3687 {
3688 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3689 }
3690 else
3691 {
3692 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TRX_OFF);
3693 }
3694 }
3695 else if (macState == MAC_ACK_PENDING)
3696 {
3698 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3699 }
3700 else if (macState == MAC_CSMA)
3701 {
3704 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3705 }
3706 else if (m_macState == MAC_CSMA && macState == CHANNEL_IDLE)
3707 {
3708 // Channel is idle, set transmitter to TX_ON
3710 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
3711 }
3712 else if (m_macState == MAC_CSMA && macState == CHANNEL_ACCESS_FAILURE)
3713 {
3715
3716 // Cannot find a clear channel, drop the current packet
3717 // and send the proper confirm/indication according to the packet type
3718 NS_LOG_DEBUG(this << " cannot find clear channel");
3719
3721
3722 Ptr<Packet> pkt = m_txPkt->Copy();
3723 LrWpanMacHeader macHdr;
3724 pkt->RemoveHeader(macHdr);
3725
3726 if (macHdr.IsCommand())
3727 {
3728 CommandPayloadHeader cmdPayload;
3729 pkt->RemoveHeader(cmdPayload);
3730
3731 switch (cmdPayload.GetCommandFrameType())
3732 {
3734 m_macPanId = 0xffff;
3736 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3739 m_csmaCa->SetUnSlottedCsmaCa();
3742
3744 {
3745 MlmeAssociateConfirmParams confirmParams;
3746 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
3748 m_mlmeAssociateConfirmCallback(confirmParams);
3749 }
3750 break;
3751 }
3754 {
3755 MlmeCommStatusIndicationParams commStatusParams;
3756 commStatusParams.m_panId = m_macPanId;
3757 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
3758 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
3759 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
3760 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
3762 m_mlmeCommStatusIndicationCallback(commStatusParams);
3763 }
3765 break;
3766 }
3768 m_macPanId = 0xffff;
3770 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3773 m_csmaCa->SetUnSlottedCsmaCa();
3776
3778 {
3779 MlmePollConfirmParams pollConfirmParams;
3780 pollConfirmParams.m_status = MacStatus::CHANNEL_ACCESS_FAILURE;
3781 m_mlmePollConfirmCallback(pollConfirmParams);
3782 }
3783 break;
3784 }
3787 {
3788 MlmeCommStatusIndicationParams commStatusParams;
3789 commStatusParams.m_panId = m_macPanId;
3790 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
3791 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
3792 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
3793 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
3795 m_mlmeCommStatusIndicationCallback(commStatusParams);
3796 }
3797 break;
3798 }
3801 {
3802 m_unscannedChannels.emplace_back(m_phy->GetCurrentChannelNum());
3803 }
3804 // TODO: Handle orphan notification command during a
3805 // channel access failure when not is not scanning.
3806 break;
3807 }
3809 if (m_scanEvent.IsPending())
3810 {
3811 m_unscannedChannels.emplace_back(m_phy->GetCurrentChannelNum());
3812 }
3813 // TODO: Handle beacon request command during a
3814 // channel access failure when not scanning.
3815 break;
3816 }
3817 default: {
3818 // TODO: Other commands(e.g. Disassociation notification, etc)
3819 break;
3820 }
3821 }
3823 }
3824 else if (macHdr.IsData())
3825 {
3827 {
3828 McpsDataConfirmParams confirmParams;
3829 confirmParams.m_msduHandle = m_txQueue.front()->txQMsduHandle;
3831 m_mcpsDataConfirmCallback(confirmParams);
3832 }
3833 // remove the copy of the packet that was just sent
3835 }
3836 else
3837 {
3838 // TODO:: specify behavior for other packets
3839 m_txPkt = nullptr;
3840 m_retransmission = 0;
3841 m_numCsmacaRetry = 0;
3842 }
3843
3846 {
3847 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3848 }
3849 else
3850 {
3851 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TRX_OFF);
3852 }
3853 }
3854 else if (m_macState == MAC_CSMA && macState == MAC_CSMA_DEFERRED)
3855 {
3857 m_txPkt = nullptr;
3858 // The MAC is running on beacon mode and the current packet could not be sent in the
3859 // current CAP. The packet will be send on the next CAP after receiving the beacon.
3860 // The PHY state does not change from its current form. The PHY change (RX_ON) will be
3861 // triggered by the scheduled beacon event.
3862
3863 NS_LOG_DEBUG("****** PACKET DEFERRED to the next superframe *****");
3864 }
3865}
3866
3867void
3872
3873void
3878
3879uint16_t
3881{
3882 return m_macPanId;
3883}
3884
3890
3896
3897void
3898LrWpanMac::SetPanId(uint16_t panId)
3899{
3900 m_macPanId = panId;
3901}
3902
3903void
3905{
3906 NS_LOG_LOGIC(this << " change lrwpan mac state from " << m_macState << " to " << newState);
3907 m_macStateLogger(m_macState, newState);
3908 m_macState = newState;
3909}
3910
3911uint64_t
3913{
3914 return lrwpan::aUnitBackoffPeriod + lrwpan::aTurnaroundTime + m_phy->GetPhySHRDuration() +
3915 ceil(6 * m_phy->GetPhySymbolsPerOctet());
3916}
3917
3918uint8_t
3923
3924void
3926{
3927 NS_LOG_DEBUG("Transmit Queue Size: " << m_txQueue.size());
3928}
3929
3930void
3932{
3933 m_macMaxFrameRetries = retries;
3934}
3935
3936bool
3938{
3940 LrWpanMacHeader macHdr;
3941 m_txPkt->PeekHeader(macHdr);
3942
3943 if (m_coor)
3944 {
3945 // The device is its coordinator and the packet is not to itself
3946 return false;
3947 }
3948 else if (m_macCoordShortAddress == macHdr.GetShortDstAddr() ||
3950 {
3951 return true;
3952 }
3953 else
3954 {
3955 NS_LOG_DEBUG("ERROR: Packet not for the coordinator!");
3956 return false;
3957 }
3958}
3959
3962{
3964
3966 {
3967 return m_macSIFSPeriod;
3968 }
3969 else
3970 {
3971 return m_macLIFSPeriod;
3972 }
3973}
3974
3975void
3980
3981void
3986
3987uint64_t
3989{
3991 // Sync Header (SHR) + 8 bits PHY header (PHR) + PSDU
3992 return (m_phy->GetPhySHRDuration() + 1 * m_phy->GetPhySymbolsPerOctet() +
3993 (m_txPkt->GetSize() * m_phy->GetPhySymbolsPerOctet()));
3994}
3995
3996bool
3998{
4000 LrWpanMacHeader macHdr;
4001 m_txPkt->PeekHeader(macHdr);
4002
4003 return macHdr.IsAckReq();
4004}
4005
4006} // namespace lrwpan
4007} // namespace ns3
bool IsNull() const
Check for null implementation.
Definition callback.h:555
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
bool IsPending() const
This method is syntactic sugar for !IsExpired().
Definition event-id.cc:65
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
This class can contain 16 bit addresses.
bool IsMulticast() const
Checks if the address is a multicast address according to RFC 4944 Section 9 (i.e....
bool IsBroadcast() const
Checks if the address is a broadcast address according to 802.15.4 scheme (i.e., 0xFFFF).
an EUI-64 address
static Mac64Address Allocate()
Allocate a new Mac64Address.
static bool ChecksumEnabled()
Definition node.cc:267
virtual void DoInitialize()
Initialize() implementation.
Definition object.cc:440
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition packet.cc:120
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition packet.cc:294
Smart pointer class similar to boost::intrusive_ptr.
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition simulator.cc:206
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:403
@ S
second
Definition nstime.h:105
bool IsZero() const
Exactly equivalent to t == 0.
Definition nstime.h:304
a unique identifier for an interface.
Definition type-id.h:49
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:862
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
Implements the header for the MAC payload beacon frame according to the IEEE 802.15....
void SetSuperframeSpecField(uint16_t sfrmField)
Set the superframe specification field to the beacon payload header.
uint16_t GetSuperframeSpecField() const
Get the superframe specification field from the beacon payload header.
GtsFields GetGtsFields() const
Get the Guaranteed Time Slots (GTS) fields from the beacon payload header.
void SetGtsFields(GtsFields gtsFields)
Set the superframe Guaranteed Time Slot (GTS) fields to the beacon payload header.
PendingAddrFields GetPndAddrFields() const
Get the pending address fields from the beacon payload header.
void SetPndAddrFields(PendingAddrFields pndAddrFields)
Set the superframe Pending Address fields to the beacon payload header.
Implements the header for the MAC payload command frame according to the IEEE 802....
MacCommand GetCommandFrameType() const
Get the command frame type ID.
Mac16Address GetShortAddr() const
Get the Short address assigned by the coordinator (Association Response and Coordinator Realigment co...
uint16_t GetPanId() const
Get the PAN identifier.
void SetPage(uint8_t page)
Set the logical channel page number.
void SetShortAddr(Mac16Address shortAddr)
Set the Short Address Assigned by the coordinator (Association Response and Coordinator Realigment Co...
uint8_t GetCapabilityField() const
Get the Capability Information Field from the command payload header.
void SetCommandFrameType(MacCommand macCmd)
Set the command frame type.
Mac16Address GetCoordShortAddr() const
Get the coordinator short address.
void SetCapabilityField(uint8_t cap)
Set the Capability Information Field to the command payload header (Association Request Command).
void SetCoordShortAddr(Mac16Address addr)
Set the coordinator short address (16 bit address).
@ ASSOCIATION_RESP
Association response (RFD true: Rx)
@ DATA_REQ
Data Request (RFD true: Tx)
@ COOR_REALIGN
Coordinator Realignment (RFD true: Rx)
@ ORPHAN_NOTIF
Orphan Notification (RFD true: Tx)
@ ASSOCIATION_REQ
Association request (RFD true: Tx)
@ BEACON_REQ
Beacon Request (RFD true: none )
void SetAssociationStatus(uint8_t status)
Set status resulting from the association attempt (Association Response Command).
uint8_t GetAssociationStatus() const
Get the status resulting from an association request (Association Response Command).
void SetPanId(uint16_t id)
Get the PAN identifier.
void SetChannel(uint8_t channel)
Set the logical channel number.
Represent the GTS information fields.
bool GetGtsPermit() const
Get the GTS Specification Permit.
Lr-wpan MAC layer abstraction.
MlmeOrphanIndicationCallback m_mlmeOrphanIndicationCallback
This callback is used to indicate the reception of a orphan notification command.
MlmeGetConfirmCallback m_mlmeGetConfirmCallback
This callback is used to report the result of an attribute read request to the upper layers.
MlmeAssociateIndicationCallback m_mlmeAssociateIndicationCallback
This callback is used to indicate the reception of an association request command.
MlmeSetConfirmCallback m_mlmeSetConfirmCallback
This callback is used to report the result of an attribute writing request to the upper layers.
MlmePollConfirmCallback m_mlmePollConfirmCallback
This callback is used to report the status after a device send data command request to the coordinato...
MlmeCommStatusIndicationCallback m_mlmeCommStatusIndicationCallback
This callback is instigated through a response primitive.
McpsDataConfirmCallback m_mcpsDataConfirmCallback
This callback is used to report data transmission request status to the upper layers.
McpsDataIndicationCallback m_mcpsDataIndicationCallback
This callback is used to notify incoming packets to the upper layers.
MlmeScanConfirmCallback m_mlmeScanConfirmCallback
This callback is used to report the result of a scan on a group of channels for the selected channel ...
MlmeAssociateConfirmCallback m_mlmeAssociateConfirmCallback
This callback is used to report the status after a device request an association with a coordinator.
MlmeSyncLossIndicationCallback m_mlmeSyncLossIndicationCallback
This callback is used to indicate the loss of synchronization with a coordinator.
MlmeBeaconNotifyIndicationCallback m_mlmeBeaconNotifyIndicationCallback
This callback is used to notify incoming beacon packets to the upper layers.
MlmeStartConfirmCallback m_mlmeStartConfirmCallback
This callback is used to report the start of a new PAN or the begin of a new superframe configuration...
Represent the Mac Header with the Frame Control and Sequence Number fields.
LrWpanMacType GetType() const
Get the header type.
Mac16Address GetShortSrcAddr() const
Get the Source Short address.
void SetDstAddrMode(uint8_t addrMode)
Set the Destination address mode.
void SetNoAckReq()
Set the Frame Control field "Ack. Request" bit to false.
void SetDstAddrFields(uint16_t panId, Mac16Address addr)
Set Destination address fields.
Mac64Address GetExtSrcAddr() const
Get the Source Extended address.
bool IsData() const
Returns true if the header is a data.
bool IsBeacon() const
Returns true if the header is a beacon.
@ LRWPAN_MAC_ACKNOWLEDGMENT
LRWPAN_MAC_ACKNOWLEDGMENT.
@ LRWPAN_MAC_RESERVED
LRWPAN_MAC_RESERVED.
@ LRWPAN_MAC_COMMAND
LRWPAN_MAC_COMMAND.
@ LRWPAN_MAC_BEACON
LRWPAN_MAC_BEACON.
void SetPanIdComp()
Set the Frame Control field "PAN ID Compression" bit to true.
Mac16Address GetShortDstAddr() const
Get the Destination Short address.
void SetSrcAddrMode(uint8_t addrMode)
Set the Source address mode.
bool IsCommand() const
Returns true if the header is a command.
uint8_t GetSeqNum() const
Get the frame Sequence number.
void SetFrameVer(uint8_t ver)
Set the Frame version.
uint8_t GetFrameVer() const
Get the Frame Version of Frame control field.
void SetSrcAddrFields(uint16_t panId, Mac16Address addr)
Set Source address fields.
uint16_t GetSrcPanId() const
Get the Source PAN ID.
void SetNoPanIdComp()
Set the Frame Control field "PAN ID Compression" bit to false.
uint16_t GetDstPanId() const
Get the Destination PAN ID.
void SetAckReq()
Set the Frame Control field "Ack. Request" bit to true.
bool IsAckReq() const
Check if Ack.
uint8_t GetDstAddrMode() const
Get the Dest.
void SetSecDisable()
Set the Frame Control field "Security Enabled" bit to false.
Mac64Address GetExtDstAddr() const
Get the Destination Extended address.
bool IsAcknowledgment() const
Returns true if the header is an ack.
uint8_t GetSrcAddrMode() const
Get the Source Addressing Mode of Frame control field.
Class that implements the LR-WPAN MAC state machine.
Ptr< Packet > m_rxPkt
The command request packet received.
TracedCallback< Ptr< const Packet > > m_macTxDequeueTrace
The trace source fired when packets are dequeued from the L3/l2 transmission queue.
void RemovePendTxQElement(Ptr< Packet > p)
Remove an element from the pending transaction list.
TracedCallback< Ptr< const Packet > > m_macRxDropTrace
The trace source fired for packets successfully received by the device but dropped before being forwa...
uint8_t m_deviceCapability
Indication of current device capability (FFD or RFD)
std::vector< uint8_t > m_macBeaconPayload
The set with the contents of the beacon payload.
void SetExtendedAddress(Mac64Address address)
Set the extended address of this MAC.
TracedCallback< Ptr< const Packet > > m_macIndTxDequeueTrace
The trace source fired when packets are dequeued from the L3/l2 indirect transmission queue (Pending ...
Ptr< LrWpanPhy > GetPhy()
Get the underlying PHY of the MAC.
uint32_t m_superframeDuration
Indication of the superframe duration in symbols.
void AwaitBeacon()
Called after the end of an INCOMING superframe to start the moment a device waits for a new incoming ...
void EndStartRequest()
Called to end a MLME-START.request after changing the page and channel number.
void MlmeAssociateResponse(MlmeAssociateResponseParams params) override
IEEE 802.15.4-2011, section 6.2.2.3 MLME-ASSOCIATE.response Primitive used to initiate a response to ...
bool m_macPromiscuousMode
Indicates if MAC sublayer is in receive all mode.
Ptr< LrWpanCsmaCa > m_csmaCa
The CSMA/CA implementation used by this MAC.
void SendAck(uint8_t seqno)
Send an acknowledgment packet for the given sequence number.
uint8_t m_numCsmacaRetry
The number of CSMA/CA retries used for sending the current packet.
Ptr< Packet > m_txPkt
The packet which is currently being sent by the MAC layer.
MlmeStartRequestParams m_startParams
The parameters used during a MLME-START.request.
void PurgeInd()
Purge expired transactions from the pending transactions list.
void SetCsmaCa(Ptr< LrWpanCsmaCa > csmaCa)
Set the CSMA/CA implementation to be used by the MAC.
EventId m_scanEvent
Scheduler event for the end of an ACTIVE or PASSIVE channel scan.
void PdDataConfirm(PhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.1.2 Confirm the end of transmission of an MPDU to MAC.
void SendOrphanNotificationCommand()
Called to send a orphan notification command.
Mac64Address m_macExtendedAddress
The extended 64 address (IEEE EUI-64) used by this MAC.
EventId m_scanEnergyEvent
Scheduler event for the end of a ED channel scan.
uint16_t m_macPanIdScan
Temporarily stores the value of the current m_macPanId when a MLME-SCAN.request is performed.
EventId m_setMacState
Scheduler event for a deferred MAC state change.
uint32_t m_macBeaconPayloadLength
The length, in octets, of the beacon payload.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
A trace source that emulates a promiscuous mode protocol sniffer connected to the device.
TracedCallback< Ptr< const Packet >, uint8_t, uint8_t > m_sentPktTrace
The trace source fired when packets are considered as successfully sent or the transmission has been ...
uint64_t m_assocRespCmdWaitTime
The maximum wait time for an association response command after the reception of data request command...
GtsFields GetGtsFields()
Constructs the Guaranteed Time Slots (GTS) Fields from local information.
void SetIndTxQMaxSize(uint32_t queueSize)
Set the max size of the indirect transmit queue (Pending Transaction list)
std::deque< Ptr< IndTxQueueElement > > m_indTxQueue
The indirect transmit queue used by the MAC pending messages (The pending transaction list).
uint8_t m_incomingSuperframeOrder
Used by all devices that have a parent.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
uint32_t m_macLIFSPeriod
The minimum time forming a Long InterFrame Spacing (LIFS) period.
TracedCallback< Ptr< const Packet > > m_macRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
std::vector< uint8_t > m_unscannedChannels
The list of unscanned channels during a scan operation.
bool m_macRxOnWhenIdle
Indication of whether the MAC sublayer is to enable its receiver during idle periods.
void PrintTransmitQueueSize()
Print the number of elements in the packet transmit queue.
uint16_t m_channelScanIndex
The channel list index used to obtain the current scanned channel.
void SendAssocRequestCommand()
Called to send an associate request command.
EventId m_beaconEvent
Scheduler event for generation of one beacon.
void BeaconSearchTimeout()
Called if the device is unable to locate a beacon in the time set by MLME-SYNC.request.
uint32_t m_macSIFSPeriod
The minimum time forming a Short InterFrame Spacing (SIFS) period.
TracedCallback< Ptr< const Packet > > m_macTxTrace
The trace source fired when packets are being sent down to L1.
void MlmeSyncRequest(MlmeSyncRequestParams params) override
IEEE 802.15.4-2011, section 6.2.13.1 MLME-SYNC.request Request to synchronize with the coordinator by...
SequenceNumber8 m_macDsn
Sequence number added to transmitted data or MAC command frame, 00-ff.
void ChangeMacState(MacState newState)
Change the current MAC state to the given new state.
Mac16Address m_macCoordShortAddress
The short address of the coordinator through which the device is associated.
Mac64Address GetCoordExtAddress() const
Get the coordinator extended address currently associated to this device.
void MlmeGetRequest(MacPibAttributeIdentifier id) override
IEEE 802.15.4-2006, section 7.1.6.1 MLME-GET.request Request information about a given PIB attribute.
std::deque< Ptr< TxQueueElement > > m_txQueue
The transmit queue used by the MAC.
uint8_t m_macSuperframeOrder
Used by a PAN coordinator or coordinator.
TracedValue< SuperframeStatus > m_incSuperframeStatus
The current period of the incoming superframe.
void SetPanId(uint16_t panId)
Set the PAN id used by this MAC.
void MlmeStartRequest(MlmeStartRequestParams params) override
IEEE 802.15.4-2006, section 7.1.14.1 MLME-START.request Request to allow a PAN coordinator to initiat...
MlmeScanRequestParams m_scanParams
The parameters used during a MLME-SCAN.request.
void SetLrWpanMacState(MacState macState)
CSMA-CA algorithm calls back the MAC after executing channel assessment.
void SetShortAddress(Mac16Address address)
Set the short address of this MAC.
uint8_t m_incomingBeaconOrder
The beaconOrder value of the INCOMING frame.
void PrintTxQueue(std::ostream &os) const
Print the Transmit Queue.
void CheckQueue()
Check the transmission queue.
uint32_t m_incomingSuperframeDuration
Indication of the superframe duration in symbols (e.g.
TracedCallback< Ptr< const Packet > > m_macIndTxEnqueueTrace
The trace source fired when packets come into the "top" of the device at the L3/L2 transition,...
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
The trace source fired when packets are dropped due to missing ACKs or because of transmission failur...
void SetMacMaxFrameRetries(uint8_t retries)
Set the macMaxFrameRetries attribute value.
uint16_t m_macTransactionPersistenceTime
The maximum time (in UNIT periods) that a transaction is stored by a coordinator and indicated in its...
void MlmeOrphanResponse(MlmeOrphanResponseParams params) override
IEEE 802.15.4-2011, section 6.2.7.2 MLME-ORPHAN.response Primitive used to initiatte a response to an...
void AckWaitTimeout()
Handle an ACK timeout with a packet retransmission, if there are retransmission left,...
uint32_t m_maxTxQueueSize
The maximum size of the transmit queue.
TracedCallback< Ptr< const Packet > > m_macIndTxDropTrace
The trace source fired when packets are dropped due to indirect Tx queue overflows or expiration.
uint8_t m_retransmission
The number of already used retransmission for the currently transmitted packet.
LrWpanMac()
Default constructor.
EventId m_incCfpEvent
Scheduler event for the end of the incoming superframe CFP.
std::vector< uint8_t > m_energyDetectList
The list of energy measurements, one for each channel searched during an ED scan.
SequenceNumber8 m_macBsn
Sequence number added to transmitted beacon frame, 00-ff.
TracedCallback< MacState, MacState > m_macStateLogger
A trace source that fires when the MAC changes states.
TracedCallback< Ptr< const Packet > > m_snifferTrace
A trace source that emulates a non-promiscuous protocol sniffer connected to the device.
Mac64Address GetExtendedAddress() const
Get the extended address of this MAC.
void DoDispose() override
Destructor implementation.
uint8_t m_fnlCapSlot
Indication of the Slot where the CAP portion of the OUTGOING Superframe ends.
MlmeAssociateRequestParams m_associateParams
The parameters used during a MLME-ASSOCIATE.request.
uint32_t m_incomingBeaconInterval
Indication of the interval a node should receive a superframe expressed in symbols.
bool m_macAssociationPermit
Indication of whether a coordinator is currently allowing association.
static TypeId GetTypeId()
Get the type ID.
uint64_t GetMacAckWaitDuration() const
Get the macAckWaitDuration attribute value.
void SetTxQMaxSize(uint32_t queueSize)
Set the max size of the transmit queue.
TracedCallback< Time > m_macIfsEndTrace
The trace source is fired at the end of any Interframe Space (IFS).
void MlmeAssociateRequest(MlmeAssociateRequestParams params) override
IEEE 802.15.4-2011, section 6.2.2.1 MLME-ASSOCIATE.request Request primitive used by a device to requ...
bool GetRxOnWhenIdle() const
Check if the receiver will be enabled when the MAC is idle.
void ReceiveBeacon(uint8_t lqi, Ptr< Packet > p)
Used to process the reception of a beacon packet.
void EnqueueInd(Ptr< Packet > p)
Adds a packet to the pending transactions list (Indirect transmissions).
Time m_macBeaconTxTime
The time that the device transmitted its last beacon frame.
std::vector< PanDescriptor > m_panDescriptorList
The list of PAN descriptors accumulated during channel scans, used to select a PAN to associate.
uint8_t m_maxEnergyLevel
The maximum energy level detected during ED scan on the current channel.
Mac16Address GetShortAddress() const
Get the short address of this MAC.
bool m_macAutoRequest
Indication of whether a device automatically sends data request command if its address is listed in t...
PendingAddrFields GetPendingAddrFields()
Constructs Pending Address Fields from the local information, the Pending Address Fields are part of ...
uint64_t m_rxBeaconSymbols
The total size of the received beacon in symbols.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< LrWpanPhy > m_phy
The PHY associated with this MAC.
EventId m_cfpEvent
Scheduler event for the end of the outgoing superframe CFP.
uint16_t GetPanId() const
Get the PAN id used by this MAC.
TracedCallback< Ptr< const Packet > > m_macTxOkTrace
The trace source fired when packets where successfully transmitted, that is an acknowledgment was rec...
bool m_panCoor
Indication of whether the current device is the PAN coordinator.
void SendAssocResponseCommand(Ptr< Packet > rxDataReqPkt)
Called to send an associate response command.
Time m_macBeaconRxTime
The time that the device received its last bit of the beacon frame.
void SetAssociatedCoor(Mac16Address mac)
Check if the packet destination is its coordinator.
void StartCFP(SuperframeType superframeType)
Called to begin the Contention Free Period (CFP) in a beacon-enabled mode.
uint16_t m_macPanId
16 bits id of PAN on which this device is operating.
bool PrepareRetransmission()
Check for remaining retransmissions for the packet currently being sent.
void PrintPendingTxQueue(std::ostream &os) const
Print the Pending transaction list.
uint32_t m_ifs
The value of the necessary InterFrame Space after the transmission of a packet.
void MlmeSetRequest(MacPibAttributeIdentifier id, Ptr< MacPibAttributes > attribute) override
IEEE 802.15.4-2011, section 6.2.11.1 MLME-SET.request Attempts to write the given value to the indica...
bool m_beaconTrackingOn
Indication of whether the current device is tracking incoming beacons.
void SendBeaconRequestCommand()
Called to send a beacon request command.
uint32_t m_maxIndTxQueueSize
The maximum size of the indirect transmit queue (The pending transaction list).
void IfsWaitTimeout(Time ifsTime)
After a successful transmission of a frame (beacon, data) or an ack frame reception,...
EventId m_ifsEvent
Scheduler event for Interframe spacing wait time.
void MlmeScanRequest(MlmeScanRequestParams params) override
IEEE 802.15.4-2011, section 6.2.10.1 MLME-SCAN.request Request primitive used to initiate a channel s...
Ptr< UniformRandomVariable > m_uniformVar
The uniform random variable used in this mac layer.
void PdDataIndication(uint32_t psduLength, Ptr< Packet > p, uint8_t lqi)
IEEE 802.15.4-2006 section 6.2.1.3 PD-DATA.indication Indicates the transfer of an MPDU from PHY to M...
uint8_t m_macMaxFrameRetries
The maximum number of retries allowed after a transmission failure.
PendingPrimitiveStatus m_pendPrimitive
Indicates the pending primitive when PLME.SET operation (page or channel switch) is called from withi...
EventId m_respWaitTimeout
Scheduler event for a response to a request command frame.
uint16_t GetSuperframeField()
Constructs a Superframe specification field from the local information, the superframe Specification ...
bool IsCoordDest()
Check if the packet destination is its coordinator.
EventId m_ackWaitTimeout
Scheduler event for the ACK timeout of the currently transmitted data packet.
void StartCAP(SuperframeType superframeType)
Called to begin the Contention Access Period (CAP) in a beacon-enabled mode.
Mac64Address m_macCoordExtendedAddress
The extended address of the coordinator through which the device is associated.
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
EventId m_incCapEvent
Scheduler event for the end of the incoming superframe CAP.
Mac16Address GetCoordShortAddress() const
Get the coordinator short address currently associated to this device.
void SendDataRequestCommand()
Used to send a data request command (i.e.
bool DequeueInd(Mac64Address dst, Ptr< IndTxQueueElement > entry)
Extracts a packet from pending transactions list (Indirect transmissions).
EventId m_trackingEvent
Scheduler event to track the incoming beacons.
uint64_t GetTxPacketSymbols()
Obtain the number of symbols in the packet which is currently being sent by the MAC layer.
uint32_t m_beaconInterval
Indication of the Interval used by the coordinator to transmit beacon frames expressed in symbols.
Mac16Address m_shortAddress
The short address (16 bit address) used by this MAC.
EventId m_assocResCmdWaitTimeout
Scheduler event for the lost of a association response command frame.
void PlmeGetAttributeConfirm(PhyEnumeration status, PhyPibAttributeIdentifier id, Ptr< PhyPibAttributes > attribute)
IEEE 802.15.4-2006 section 6.2.2.6 PLME-GET.confirm Get attributes per definition from Table 23 in se...
uint8_t m_macBeaconOrder
Used by a PAN coordinator or coordinator.
void SetPhy(Ptr< LrWpanPhy > phy)
Set the underlying PHY for the MAC.
void MlmePollRequest(MlmePollRequestParams params) override
IEEE 802.15.4-2011, section 6.2.14.2 MLME-POLL.request Prompts the device to request data from the co...
void RemoveFirstTxQElement()
Remove the tip of the transmission queue, including clean up related to the last packet transmission.
TracedValue< SuperframeStatus > m_outSuperframeStatus
The current period of the outgoing superframe.
void SetRxOnWhenIdle(bool rxOnWhenIdle)
Set if the receiver should be enabled when the MAC is idle.
void PlmeCcaConfirm(PhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
void PlmeSetAttributeConfirm(PhyEnumeration status, PhyPibAttributeIdentifier id)
IEEE 802.15.4-2006 section 6.2.2.10 PLME-SET.confirm Set attributes per definition from Table 23 in s...
EventId m_scanOrphanEvent
Scheduler event for the end of an ORPHAN channel scan.
void StartInactivePeriod(SuperframeType superframeType)
Start the Inactive Period in a beacon-enabled mode.
uint32_t GetIfsSize()
Get the size of the Interframe Space according to MPDU size (m_txPkt).
EventId m_capEvent
Scheduler event for the end of the outgoing superframe CAP.
void PlmeSetTRXStateConfirm(PhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.8 PLME-SET-TRX-STATE.confirm Set PHY state.
TracedValue< MacState > m_macState
The current state of the MAC layer.
void EndChannelEnergyScan()
Called at the end of one ED channel scan.
uint8_t m_incomingFnlCapSlot
Indication of the Slot where the CAP portion of the INCOMING Superframe ends.
bool m_ignoreDataCmdAck
This flag informs the MAC that an association response command was received before the acknowledgment...
TracedCallback< Ptr< const Packet > > m_macTxEnqueueTrace
The trace source fired when packets come into the "top" of the device at the L3/L2 transition,...
uint8_t m_numLostBeacons
The number of consecutive loss beacons in a beacon tracking operation.
uint8_t GetMacMaxFrameRetries() const
Get the macMaxFrameRetries attribute value.
bool IsTxAckReq()
Check if the packet to transmit requires acknowledgment.
void SendOneBeacon()
Called to send a single beacon frame.
void EnqueueTxQElement(Ptr< TxQueueElement > txQElement)
Add an element to the transmission queue.
void LostAssocRespCommand()
Called after m_assocRespCmdWaitTime timeout while waiting for an association response command.
void PlmeEdConfirm(PhyEnumeration status, uint8_t energyLevel)
IEEE 802.15.4-2006 section 6.2.2.4 PLME-ED.confirm status and energy level.
bool m_coor
Indicates if the current device is a coordinator type.
uint8_t m_lastRxFrameLqi
Keep track of the last received frame Link Quality Indicator.
void EndChannelScan()
Called at the end of the current channel scan (Active or Passive) for a given duration.
void EndAssociateRequest()
Called to end an MLME-ASSOCIATE.request after changing the page and channel number.
uint64_t m_macResponseWaitTime
The maximum time, in multiples of aBaseSuperframeDuration, a device shall wait for a response command...
void DoInitialize() override
Initialize() implementation.
Represent the Mac Trailer with the Frame Check Sequence field.
void EnableFcs(bool enable)
Enable or disable FCS calculation for this trailer.
void SetFcs(Ptr< const Packet > p)
Calculate and set the FCS value based on the given packet.
bool CheckFcs(Ptr< const Packet > p)
Check the FCS of a given packet against the FCS value stored in the trailer.
Represent the Pending Address Specification field.
Represent the Superframe Specification information field.
void SetPanCoor(bool panCoor)
Set the Superframe Specification PAN coordinator field.
uint8_t GetFinalCapSlot() const
Get the the Final CAP Slot.
void SetAssocPermit(bool assocPermit)
Set the Superframe Specification Association Permit field.
void SetBattLifeExt(bool battLifeExt)
Set the Superframe Specification Battery Life Extension (BLE).
bool IsBattLifeExt() const
Check if the Battery Life Extension bit is enabled.
uint8_t GetBeaconOrder() const
Get the Superframe Specification Beacon Order field.
uint8_t GetFrameOrder() const
Get the Superframe Specification Frame Order field.
uint16_t GetSuperframe() const
Get the Superframe specification information field.
void SetFinalCapSlot(uint8_t capSlot)
Set the superframe specification Final CAP slot field.
void SetBeaconOrder(uint8_t bcnOrder)
Set the superframe specification Beacon Order field.
void SetSuperframeOrder(uint8_t frmOrder)
Set the superframe specification Superframe Order field.
constexpr uint32_t aMaxSIFSFrameSize
The maximum size of an MPDU, in octets, that can be followed by a Short InterFrame Spacing (SIFS) per...
constexpr uint32_t aMaxLostBeacons
The number of consecutive lost beacons that will cause the MAC sublayer of a receiving device to decl...
constexpr uint32_t aMaxBeaconPayloadLength
The maximum size, in octets, of a beacon payload.
constexpr uint32_t aMaxPhyPacketSize
The maximum packet size accepted by the PHY.
constexpr uint32_t aUnitBackoffPeriod
Number of symbols per CSMA/CA time unit, default 20 symbols.
constexpr uint32_t aTurnaroundTime
The turnaround time in symbol periods for switching the transceiver from RX to TX or vice-versa.
constexpr uint32_t aBaseSuperframeDuration
Length of a superframe in symbols.
constexpr uint32_t aMinMPDUOverhead
The minimum number of octets added by the MAC sublayer to the PSDU.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition uinteger.h:35
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
MacPibAttributeIdentifier
IEEE 802.15.4-2006 PHY and MAC PIB Attribute Identifiers Table 23 and Table 86.
MacState
MAC states.
Definition lr-wpan-mac.h:64
SuperframeType
Superframe type.
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
MacStatus
The status of a confirm or an indication primitive as a result of a previous request.
PhyPibAttributeIdentifier
IEEE802.15.4-2006 PHY PIB Attribute Identifiers Table 23 in section 6.4.2.
@ macPanId
The 16-bit identifier of the Personal Area Network (PAN).
@ pCurrentChannel
RF channel used for transmissions and receptions.
@ macShortAddress
The short address of the device (16 bit address).
@ macRxOnWhenIdle
Indication of whether the MAC is enabled during idle periods.
@ macBeaconPayloadLength
The length in octets of the beacon payload.
@ macPromiscuousMode
Indication of whether the MAC sublayer is in a promiscuous mode.
@ pCurrentPage
The current channel page.
@ macExtendedAddress
The extended address of the device (64 bit address).
@ macAssociationPermit
Indication of whether a coordinator is allowing association.
@ macBeaconPayload
The contents of the beacon payload.
@ MAC_CSMA
MAC_CSMA.
Definition lr-wpan-mac.h:66
@ MAC_GTS
MAC_GTS.
Definition lr-wpan-mac.h:72
@ CHANNEL_ACCESS_FAILURE
CHANNEL_ACCESS_FAILURE.
Definition lr-wpan-mac.h:69
@ CHANNEL_IDLE
CHANNEL_IDLE.
Definition lr-wpan-mac.h:70
@ SET_PHY_TX_ON
SET_PHY_TX_ON.
Definition lr-wpan-mac.h:71
@ MAC_CSMA_DEFERRED
MAC_CSMA_DEFERRED.
Definition lr-wpan-mac.h:74
@ MAC_IDLE
MAC_IDLE.
Definition lr-wpan-mac.h:65
@ MAC_INACTIVE
MAC_INACTIVE.
Definition lr-wpan-mac.h:73
@ MAC_SENDING
MAC_SENDING.
Definition lr-wpan-mac.h:67
@ MAC_ACK_PENDING
MAC_ACK_PENDING.
Definition lr-wpan-mac.h:68
@ BEACON
The Beacon transmission or reception Period.
Definition lr-wpan-mac.h:93
@ INACTIVE
Inactive Period or unslotted CSMA-CA.
Definition lr-wpan-mac.h:96
@ CAP
Contention Access Period.
Definition lr-wpan-mac.h:94
@ CFP
Contention Free Period.
Definition lr-wpan-mac.h:95
@ MLME_SCAN_REQ
Pending MLME-SCAN.request primitive.
@ MLME_NONE
No pending primitive.
@ MLME_START_REQ
Pending MLME-START.request primitive.
@ MLME_ASSOC_REQ
Pending MLME-ASSOCIATION.request primitive.
@ FFD
Full Functional Device (FFD)
@ INCOMING
Incoming Superframe.
@ OUTGOING
Outgoing Superframe.
@ IEEE_802_15_4_PHY_RX_ON
@ IEEE_802_15_4_PHY_TRX_OFF
@ IEEE_802_15_4_PHY_TX_ON
@ IEEE_802_15_4_PHY_SUCCESS
@ IEEE_802_15_4_PHY_UNSPECIFIED
@ TX_OPTION_GTS
TX_OPTION_GTS.
Definition lr-wpan-mac.h:54
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:53
@ TX_OPTION_INDIRECT
TX_OPTION_INDIRECT.
Definition lr-wpan-mac.h:55
@ TRANSACTION_OVERFLOW
There is no capacity to store the transaction.
@ NO_BEACON
A scan operation failed to find any network beacons.
@ UNSUPPORTED_ATTRIBUTE
SET/GET request issued with a non supported ID.
@ NO_SHORT_ADDRESS
Failure due to unallocated 16-bit short address.
@ ACCESS_DENIED
PAN access denied.
@ BEACON_LOSS
The beacon was lost following a synchronization request.
@ CHANNEL_ACCESS_FAILURE
A Tx could not take place due to activity in the CH.
@ READ_ONLY
SET/GET request issued for a read only attribute.
@ TRANSACTION_EXPIRED
The transaction expired and its information discarded.
@ SCAN_IN_PROGRESS
Scan failed because already performing another scan.
@ FRAME_TOO_LONG
Frame more than aMaxPHYPacketSize or too large for CAP or GTS.
@ INVALID_ADDRESS
Invalid source or destination address.
@ FULL_CAPACITY
PAN at capacity.
@ SUCCESS
The operation was completed successfully.
@ NO_ACK
No acknowledgment was received after macMaxFrameRetries.
@ NO_DATA
No response data were available following a request.
@ INVALID_PARAMETER
Primitive parameter not supported or out of range.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
SequenceNumber< uint8_t, int8_t > SequenceNumber8
8 bit Sequence number.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
std::ostream & operator<<(std::ostream &os, const SuperframeField &superframeField)
Stream insertion operator.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MacStatus m_status
The status of the last MSDU transmission.
MLME-ASSOCIATE.confirm params.
Mac16Address m_assocShortAddr
The short address used in the association request.
MacStatus m_status
The status of a MLME-associate.request.
MLME-ASSOCIATE.indication params.
uint8_t lqi
The link quality indicator of the received associate request command (Not officially supported in the...
Mac64Address m_extDevAddr
The extended address of the device requesting association.
uint8_t capabilityInfo
The operational capabilities of the device requesting association.
MLME-ASSOCIATE.request params.
Mac64Address m_coordExtAddr
The extended address of the coordinator with which to associate.
uint8_t m_coordAddrMode
The coordinator addressing mode for this primitive and subsequent MPDU.
uint8_t m_capabilityInfo
Specifies the operational capabilities of the associating device (bitmap).
Mac16Address m_coordShortAddr
The short address of the coordinator with which to associate.
uint8_t m_chNum
The channel number on which to attempt association.
uint16_t m_coordPanId
The identifier of the PAN with which to associate.
MLME-ASSOCIATE.response params.
MLME-BEACON-NOTIFY.indication params.
PanDescriptor m_panDescriptor
The PAN descriptor for the received beacon.
uint32_t m_sduLength
The number of octets contained in the beacon payload.
uint8_t m_bsn
The beacon sequence number.
Ptr< Packet > m_sdu
The set of octets comprising the beacon payload.
MLME-COMM-STATUS.indication params.
uint8_t m_dstAddrMode
The destination addressing mode for this primitive.
uint16_t m_panId
The PAN identifier of the device from which the frame was received or to which the frame was being se...
Mac64Address m_srcExtAddr
The extended address of the entity from which the frame causing the error originated.
MacStatus m_status
The communication status.
Mac16Address m_srcShortAddr
The short address of the entity from which the frame causing the error originated.
Mac16Address m_dstShortAddr
The short address of the device for which the frame was intended.
uint8_t m_srcAddrMode
The source addressing mode for this primitive.
Mac64Address m_dstExtAddr
The extended address of the device for which the frame was intended.
MLME-ORPHAN.indication params.
Mac64Address m_orphanAddr
The address of the orphaned device.
MacStatus m_status
The confirmation status resulting from a MLME-poll.request.
std::vector< uint8_t > m_unscannedCh
A list of channels given in the request which were not scanned (Not valid for ED scans).
uint32_t m_chPage
The channel page on which the scan was performed.
std::vector< uint8_t > m_energyDetList
A list of energy measurements, one for each channel searched during ED scan (Not valid for Active,...
MacStatus m_status
The status of the scan request.
std::vector< PanDescriptor > m_panDescList
A list of PAN descriptor, one for each beacon found (Not valid for ED and Orphan scans).
uint8_t m_resultListSize
The number of elements returned in the appropriate result list.
uint8_t m_scanType
Indicates the type of scan performed (ED,ACTIVE,PASSIVE,ORPHAN).
MlmeScanType m_scanType
Indicates the type of scan performed as described in IEEE 802.15.4-2011 (5.1.2.1).
uint32_t m_scanChannels
The channel numbers to be scanned.
uint8_t m_scanDuration
The factor (0-14) used to calculate the length of time to spend scanning.
uint32_t m_chPage
The channel page on which to perform scan.
MacStatus m_status
The result of the request to write the PIB attribute.
MacPibAttributeIdentifier id
The id of the PIB attribute that was written.
MacStatus m_status
The status of a MLME-start.request.
bool m_battLifeExt
Flag indicating whether or not the Battery life extension (BLE) features are used.
uint8_t m_logCh
Logical channel on which to start using the new superframe configuration.
uint32_t m_logChPage
Logical channel page on which to start using the new superframe configuration.
bool m_coorRealgn
True if a realignment request command is to be transmitted prior changing the superframe.
uint8_t m_bcnOrd
Beacon Order, Used to calculate the beacon interval, a value of 15 indicates no periodic beacons will...
bool m_panCoor
On true this device will become coordinator.
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
uint16_t m_PanId
Pan Identifier used by the device.
MLME-SYNC-LOSS.indication params.
uint16_t m_panId
The PAN identifier with which the device lost synchronization or to which it was realigned.
MacStatus m_lossReason
The reason for the lost of synchronization.
PAN Descriptor, Table 17 IEEE 802.15.4-2011.
uint16_t m_coorPanId
The PAN ID of the coordinator as specified in the received beacon frame.
uint8_t m_logCh
The current channel number occupied by the network.
Time m_timeStamp
Beacon frame reception time.
bool m_gtsPermit
TRUE if the beacon is from the PAN coordinator that is accepting GTS requests.
Mac16Address m_coorShortAddr
The coordinator short address as specified in the coordinator address mode.
uint16_t m_superframeSpec
The superframe specification as specified in the received beacon frame.
AddressMode m_coorAddrMode
The coordinator addressing mode corresponding to the received beacon frame.
uint8_t m_linkQuality
The LQI at which the network beacon was received.
uint8_t m_logChPage
The current channel page occupied by the network.
Mac64Address m_coorExtAddr
The coordinator extended address as specified in the coordinator address mode.
std::ofstream queueSize