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);
1036
1037 // Strip the MAC header and MAC trailer
1038 LrWpanMacTrailer receivedMacTrailer;
1039 p->RemoveTrailer(receivedMacTrailer);
1040
1041 LrWpanMacHeader receivedMacHdr;
1042 p->RemoveHeader(receivedMacHdr);
1043
1044 NS_LOG_DEBUG("promiscuous mode, forwarding up");
1045
1046 // TODO: Fix here, this should trigger different Indication Callbacks
1047 // depending the type of frame received (data,command, beacon)
1049 {
1051 params.m_dsn = receivedMacHdr.GetSeqNum();
1052 params.m_mpduLinkQuality = lqi;
1053 params.m_srcPanId = receivedMacHdr.GetSrcPanId();
1054 params.m_srcAddrMode = receivedMacHdr.GetSrcAddrMode();
1055
1056 switch (params.m_srcAddrMode)
1057 {
1058 case SHORT_ADDR:
1059 params.m_srcAddr = receivedMacHdr.GetShortSrcAddr();
1060 break;
1061 case EXT_ADDR:
1062 params.m_srcExtAddr = receivedMacHdr.GetExtSrcAddr();
1063 break;
1064 default:
1065 break;
1066 }
1067
1068 params.m_dstPanId = receivedMacHdr.GetDstPanId();
1069 params.m_dstAddrMode = receivedMacHdr.GetDstAddrMode();
1070
1071 switch (params.m_dstAddrMode)
1072 {
1073 case SHORT_ADDR:
1074 params.m_dstAddr = receivedMacHdr.GetShortDstAddr();
1075 break;
1076 case EXT_ADDR:
1077 params.m_dstExtAddr = receivedMacHdr.GetExtDstAddr();
1078 break;
1079 default:
1080 break;
1081 }
1083 }
1084}
1085
1086void
1088{
1089 NS_LOG_FUNCTION(this);
1091
1092 m_macBsn++;
1093
1094 Ptr<Packet> beaconPacket;
1095 if (m_macBeaconPayload.empty())
1096 {
1097 beaconPacket = Create<Packet>();
1098 }
1099 else
1100 {
1101 // Extract the octets from m_macBeaconPayload and place them in a packet
1102 beaconPacket = Create<Packet>(m_macBeaconPayload.data(), m_macBeaconPayload.size());
1103 }
1104
1107 macHdr.SetDstAddrFields(GetPanId(), Mac16Address("ff:ff"));
1108
1109 // see IEEE 802.15.4-2011 Section 5.1.2.4
1110 if (GetShortAddress() == Mac16Address("ff:fe"))
1111 {
1114 }
1115 else
1116 {
1119 }
1120
1121 macHdr.SetSecDisable();
1122 macHdr.SetNoAckReq();
1123
1124 BeaconPayloadHeader macPayload;
1126 macPayload.SetGtsFields(GetGtsFields());
1128
1129 beaconPacket->AddHeader(macPayload);
1130 beaconPacket->AddHeader(macHdr);
1131
1132 // Calculate FCS if the global attribute ChecksumEnabled is set.
1133 LrWpanMacTrailer macTrailer;
1135 {
1136 macTrailer.EnableFcs(true);
1137 macTrailer.SetFcs(beaconPacket);
1138 }
1139
1140 beaconPacket->AddTrailer(macTrailer);
1141
1142 if (m_csmaCa->IsSlottedCsmaCa())
1143 {
1144 // Beacon in beacon-enabled mode
1145 // Transmit beacon immediately (i.e. Without CSMA/CA)
1146 m_txPkt = beaconPacket;
1148 NS_LOG_DEBUG("Outgoing superframe Active Portion (Beacon + CAP + CFP): "
1149 << m_superframeDuration << " symbols");
1150
1152 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
1153 }
1154 else
1155 {
1156 // Beacon as a result of a beacon request
1157 // The beacon shall be transmitted using CSMA/CA
1158 // IEEE 802.15.4-2011 (Section 5.1.2.1.2)
1160 txQElement->txQPkt = beaconPacket;
1161 EnqueueTxQElement(txQElement);
1162 CheckQueue();
1163 }
1164}
1165
1166void
1168{
1169 NS_LOG_FUNCTION(this);
1170
1172 m_macDsn++;
1173 LrWpanMacTrailer macTrailer;
1174 Ptr<Packet> commandPacket = Create<Packet>();
1175
1176 // Beacon Request Command Mac header values See IEEE 802.15.4-2011 (Section 5.3.7)
1177 macHdr.SetNoPanIdComp();
1180
1181 // Not associated PAN, broadcast dst address
1182 macHdr.SetDstAddrFields(0xFFFF, Mac16Address("FF:FF"));
1183
1184 macHdr.SetSecDisable();
1185 macHdr.SetNoAckReq();
1186
1187 CommandPayloadHeader macPayload;
1189
1190 commandPacket->AddHeader(macPayload);
1191 commandPacket->AddHeader(macHdr);
1192
1193 // Calculate FCS if the global attribute ChecksumEnabled is set.
1195 {
1196 macTrailer.EnableFcs(true);
1197 macTrailer.SetFcs(commandPacket);
1198 }
1199
1200 commandPacket->AddTrailer(macTrailer);
1201
1203 txQElement->txQPkt = commandPacket;
1204 EnqueueTxQElement(txQElement);
1205 CheckQueue();
1206}
1207
1208void
1210{
1212 m_macDsn++;
1213 LrWpanMacTrailer macTrailer;
1214 Ptr<Packet> commandPacket = Create<Packet>();
1215
1216 // See IEEE 802.15.4-2011 (5.3.6)
1217 macHdr.SetPanIdComp();
1218
1220 macHdr.SetSrcAddrFields(0xFFFF, GetExtendedAddress());
1221
1223 macHdr.SetDstAddrFields(0xFFFF, Mac16Address("FF:FF"));
1224
1225 macHdr.SetSecDisable();
1226 macHdr.SetNoAckReq();
1227
1228 CommandPayloadHeader macPayload;
1230
1231 commandPacket->AddHeader(macPayload);
1232 commandPacket->AddHeader(macHdr);
1233
1234 // Calculate FCS if the global attribute ChecksumEnabled is set.
1236 {
1237 macTrailer.EnableFcs(true);
1238 macTrailer.SetFcs(commandPacket);
1239 }
1240
1241 commandPacket->AddTrailer(macTrailer);
1242
1244 txQElement->txQPkt = commandPacket;
1245 EnqueueTxQElement(txQElement);
1246 CheckQueue();
1247}
1248
1249void
1251{
1252 NS_LOG_FUNCTION(this);
1253
1255 m_macDsn++;
1256 LrWpanMacTrailer macTrailer;
1257 Ptr<Packet> commandPacket = Create<Packet>();
1258
1259 // Assoc. Req. Comm. Mac header values See IEEE 802.15.4-2011 (Section 5.3.1.1)
1261 macHdr.SetSrcAddrFields(0xffff, GetExtendedAddress());
1262
1264 {
1267 }
1268 else
1269 {
1272 }
1273
1274 macHdr.SetSecDisable();
1275 macHdr.SetAckReq();
1276
1279
1280 commandPacket->AddHeader(macPayload);
1281 commandPacket->AddHeader(macHdr);
1282
1283 // Calculate FCS if the global attribute ChecksumEnabled is set.
1285 {
1286 macTrailer.EnableFcs(true);
1287 macTrailer.SetFcs(commandPacket);
1288 }
1289
1290 commandPacket->AddTrailer(macTrailer);
1291
1293 txQElement->txQPkt = commandPacket;
1294 EnqueueTxQElement(txQElement);
1295 CheckQueue();
1296}
1297
1298void
1300{
1301 // See IEEE 802.15.4-2011 (Section 5.3.5)
1302 // This command can be sent for 3 different situations:
1303 // a) In response to a beacon indicating that there is data for the device.
1304 // b) Triggered by MLME-POLL.request.
1305 // c) To follow an ACK of an Association Request command and continue the associate process.
1306
1307 // TODO: Implementation of a) and b) will be done when Indirect transmissions are fully
1308 // supported.
1309 // for now, only case c) is considered.
1310
1311 NS_LOG_FUNCTION(this);
1312
1314 m_macDsn++;
1315 LrWpanMacTrailer macTrailer;
1316 Ptr<Packet> commandPacket = Create<Packet>();
1317
1318 // Mac Header values (Section 5.3.5)
1320 macHdr.SetSrcAddrFields(0xffff, m_macExtendedAddress);
1321
1322 if (m_macCoordShortAddress == Mac16Address("ff:fe"))
1323 {
1326 }
1327 else
1328 {
1331 }
1332
1333 macHdr.SetSecDisable();
1334 macHdr.SetAckReq();
1335
1337
1338 commandPacket->AddHeader(macPayload);
1339 commandPacket->AddHeader(macHdr);
1340
1341 // Calculate FCS if the global attribute ChecksumEnabled is set.
1343 {
1344 macTrailer.EnableFcs(true);
1345 macTrailer.SetFcs(commandPacket);
1346 }
1347
1348 commandPacket->AddTrailer(macTrailer);
1349
1350 // Set the Command packet to be transmitted
1352 txQElement->txQPkt = commandPacket;
1353 EnqueueTxQElement(txQElement);
1354 CheckQueue();
1355}
1356
1357void
1359{
1360 LrWpanMacHeader receivedMacHdr;
1361 rxDataReqPkt->RemoveHeader(receivedMacHdr);
1362 CommandPayloadHeader receivedMacPayload;
1363 rxDataReqPkt->RemoveHeader(receivedMacPayload);
1364
1366
1368 bool elementFound;
1369 elementFound = DequeueInd(receivedMacHdr.GetExtSrcAddr(), indTxQElement);
1370 if (elementFound)
1371 {
1373 txQElement->txQPkt = indTxQElement->txQPkt;
1374 m_txQueue.emplace_back(txQElement);
1375 }
1376 else
1377 {
1378 NS_LOG_DEBUG("Requested element not found in pending list");
1379 }
1380}
1381
1382void
1384{
1385 // Association response command was not received, return to default values.
1386 m_macPanId = 0xffff;
1388 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
1389
1391 {
1392 MlmeAssociateConfirmParams confirmParams;
1393 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
1394 confirmParams.m_status = MacStatus::NO_DATA;
1395 m_mlmeAssociateConfirmCallback(confirmParams);
1396 }
1397}
1398
1399void
1401{
1402 NS_LOG_FUNCTION(this);
1403 // The primitive is no longer pending (Channel & Page have been set)
1405
1406 if (m_startParams.m_coorRealgn) // Coordinator Realignment
1407 {
1408 // TODO: Send realignment request command frame in CSMA/CA
1409 NS_LOG_ERROR(this << " Coordinator realignment request not supported");
1410 return;
1411 }
1412 else
1413 {
1415 {
1416 m_panCoor = true;
1417 }
1418
1419 m_coor = true;
1421
1422 NS_ASSERT(m_startParams.m_PanId != 0xffff);
1423
1425 if (m_macBeaconOrder == 15)
1426 {
1427 // Non-beacon enabled PAN
1428 // Cancel any ongoing events and CSMA-CA process
1430 m_fnlCapSlot = 15;
1431 m_beaconInterval = 0;
1432
1433 m_csmaCa->Cancel();
1442
1443 m_csmaCa->SetUnSlottedCsmaCa();
1444
1446 {
1447 MlmeStartConfirmParams confirmParams;
1448 confirmParams.m_status = MacStatus::SUCCESS;
1449 m_mlmeStartConfirmCallback(confirmParams);
1450 }
1451
1452 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
1453 }
1454 else
1455 {
1457 m_csmaCa->SetBatteryLifeExtension(m_startParams.m_battLifeExt);
1458
1459 m_csmaCa->SetSlottedCsmaCa();
1460
1461 // TODO: Calculate the real Final CAP slot (requires GTS implementation)
1462 // FinalCapSlot = Superframe duration slots - CFP slots.
1463 // In the current implementation the value of the final cap slot is equal to
1464 // the total number of possible slots in the superframe (15).
1465 m_fnlCapSlot = 15;
1466
1469 m_superframeDuration = (static_cast<uint32_t>(1 << m_macSuperframeOrder)) *
1471
1472 // TODO: change the beacon sending according to the startTime parameter (if not PAN
1473 // coordinator)
1474
1476 }
1477 }
1478}
1479
1480void
1482{
1483 NS_LOG_FUNCTION(this);
1484
1486
1487 bool channelFound = false;
1488
1489 for (int i = m_channelScanIndex; i <= 26; i++)
1490 {
1492 {
1493 channelFound = true;
1494 break;
1495 }
1497 }
1498
1499 if (channelFound)
1500 {
1501 // Switch to the next channel in the list and restart scan
1503 pibAttr->phyCurrentChannel = m_channelScanIndex;
1504 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
1505 }
1506 else
1507 {
1508 // All channels in the list scan completed.
1509 // Return variables to the values before the scan and return the status to the next layer.
1511 m_macPanIdScan = 0;
1512
1513 // TODO: restart beacon transmissions that were active before the beginning of the scan
1514 // (i.e when a coordinator perform a scan and it was already transmitting beacons)
1515 MlmeScanConfirmParams confirmParams;
1516 confirmParams.m_chPage = m_scanParams.m_chPage;
1517 confirmParams.m_scanType = m_scanParams.m_scanType;
1518 confirmParams.m_energyDetList = {};
1519 confirmParams.m_unscannedCh = m_unscannedChannels;
1520 confirmParams.m_resultListSize = m_panDescriptorList.size();
1521
1522 // See IEEE 802.15.4-2011, Table 31 (panDescriptorList value on macAutoRequest)
1523 // and Section 6.2.10.2
1524 switch (confirmParams.m_scanType)
1525 {
1526 case MLMESCAN_PASSIVE:
1527 if (m_macAutoRequest)
1528 {
1529 confirmParams.m_panDescList = m_panDescriptorList;
1530 }
1531 confirmParams.m_status = MacStatus::SUCCESS;
1532 break;
1533 case MLMESCAN_ACTIVE:
1534 if (m_panDescriptorList.empty())
1535 {
1536 confirmParams.m_status = MacStatus::NO_BEACON;
1537 }
1538 else
1539 {
1540 if (m_macAutoRequest)
1541 {
1542 confirmParams.m_panDescList = m_panDescriptorList;
1543 }
1544 confirmParams.m_status = MacStatus::SUCCESS;
1545 }
1546 break;
1547 case MLMESCAN_ORPHAN:
1548 confirmParams.m_panDescList = {};
1549 confirmParams.m_status = MacStatus::NO_BEACON;
1550 confirmParams.m_resultListSize = 0;
1551 // The device lost track of the coordinator and was unable
1552 // to locate it, disassociate from the network.
1553 m_macPanId = 0xffff;
1554 m_shortAddress = Mac16Address("FF:FF");
1556 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
1557 break;
1558 default:
1559 NS_LOG_ERROR(this << " Invalid scan type");
1560 }
1561
1564 m_scanParams = {};
1565
1567 {
1568 m_mlmeScanConfirmCallback(confirmParams);
1569 }
1570 }
1571}
1572
1573void
1575{
1576 NS_LOG_FUNCTION(this);
1577 // Add the results of channel energy scan to the detectList
1579 m_maxEnergyLevel = 0;
1580
1582
1583 bool channelFound = false;
1584 for (int i = m_channelScanIndex; i <= 26; i++)
1585 {
1587 {
1588 channelFound = true;
1589 break;
1590 }
1592 }
1593
1594 if (channelFound)
1595 {
1596 // switch to the next channel in the list and restart scan
1598 pibAttr->phyCurrentChannel = m_channelScanIndex;
1599 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
1600 }
1601 else
1602 {
1603 // Scan on all channels on the list completed
1604 // Return to the MAC values previous to start of the first scan.
1606 m_macPanIdScan = 0;
1607
1608 // TODO: restart beacon transmissions that were active before the beginning of the scan
1609 // (i.e when a coordinator perform a scan and it was already transmitting beacons)
1610
1611 // All channels scanned, report success
1612 MlmeScanConfirmParams confirmParams;
1613 confirmParams.m_status = MacStatus::SUCCESS;
1614 confirmParams.m_chPage = m_phy->GetCurrentPage();
1615 confirmParams.m_scanType = m_scanParams.m_scanType;
1616 confirmParams.m_energyDetList = m_energyDetectList;
1617 confirmParams.m_resultListSize = m_energyDetectList.size();
1618
1621 m_scanParams = {};
1622
1624 {
1625 m_mlmeScanConfirmCallback(confirmParams);
1626 }
1627 }
1628}
1629
1630void
1632{
1633 uint32_t activeSlot;
1634 uint64_t capDuration;
1635 Time endCapTime;
1636 uint64_t symbolRate;
1637
1638 symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1639
1640 if (superframeType == OUTGOING)
1641 {
1643 activeSlot = m_superframeDuration / 16;
1644 capDuration = activeSlot * (m_fnlCapSlot + 1);
1645 endCapTime = Seconds((double)capDuration / symbolRate);
1646 // Obtain the end of the CAP by adjust the time it took to send the beacon
1647 endCapTime -= (Simulator::Now() - m_macBeaconTxTime);
1648
1649 NS_LOG_DEBUG("Outgoing superframe CAP duration " << (endCapTime.GetSeconds() * symbolRate)
1650 << " symbols (" << endCapTime.As(Time::S)
1651 << ")");
1652 NS_LOG_DEBUG("Active Slots duration " << activeSlot << " symbols");
1653
1654 m_capEvent =
1656 }
1657 else
1658 {
1660 activeSlot = m_incomingSuperframeDuration / 16;
1661 capDuration = activeSlot * (m_incomingFnlCapSlot + 1);
1662 endCapTime = Seconds((double)capDuration / symbolRate);
1663 // Obtain the end of the CAP by adjust the time it took to receive the beacon
1664 endCapTime -= (Simulator::Now() - m_macBeaconRxTime);
1665
1666 NS_LOG_DEBUG("Incoming superframe CAP duration " << (endCapTime.GetSeconds() * symbolRate)
1667 << " symbols (" << endCapTime.As(Time::S)
1668 << ")");
1669 NS_LOG_DEBUG("Active Slots duration " << activeSlot << " symbols");
1670
1671 m_capEvent =
1673 }
1674
1675 CheckQueue();
1676}
1677
1678void
1680{
1681 uint32_t activeSlot;
1682 uint64_t cfpDuration;
1683 Time endCfpTime;
1684 uint64_t symbolRate;
1685
1686 symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1687
1688 if (superframeType == INCOMING)
1689 {
1690 activeSlot = m_incomingSuperframeDuration / 16;
1691 cfpDuration = activeSlot * (15 - m_incomingFnlCapSlot);
1692 endCfpTime = Seconds((double)cfpDuration / symbolRate);
1693 if (cfpDuration > 0)
1694 {
1696 }
1697
1698 NS_LOG_DEBUG("Incoming superframe CFP duration " << cfpDuration << " symbols ("
1699 << endCfpTime.As(Time::S) << ")");
1700
1703 this,
1705 }
1706 else
1707 {
1708 activeSlot = m_superframeDuration / 16;
1709 cfpDuration = activeSlot * (15 - m_fnlCapSlot);
1710 endCfpTime = Seconds((double)cfpDuration / symbolRate);
1711
1712 if (cfpDuration > 0)
1713 {
1715 }
1716
1717 NS_LOG_DEBUG("Outgoing superframe CFP duration " << cfpDuration << " symbols ("
1718 << endCfpTime.As(Time::S) << ")");
1719
1720 m_cfpEvent = Simulator::Schedule(endCfpTime,
1722 this,
1724 }
1725 // TODO: Start transmit or receive GTS here.
1726}
1727
1728void
1730{
1731 uint64_t inactiveDuration;
1732 Time endInactiveTime;
1733 uint64_t symbolRate;
1734
1735 symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1736
1737 if (superframeType == INCOMING)
1738 {
1740 endInactiveTime = Seconds((double)inactiveDuration / symbolRate);
1741
1742 if (inactiveDuration > 0)
1743 {
1745 }
1746
1747 NS_LOG_DEBUG("Incoming superframe Inactive Portion duration "
1748 << inactiveDuration << " symbols (" << endInactiveTime.As(Time::S) << ")");
1749 m_beaconEvent = Simulator::Schedule(endInactiveTime, &LrWpanMac::AwaitBeacon, this);
1750 }
1751 else
1752 {
1753 inactiveDuration = m_beaconInterval - m_superframeDuration;
1754 endInactiveTime = Seconds((double)inactiveDuration / symbolRate);
1755
1756 if (inactiveDuration > 0)
1757 {
1759 }
1760
1761 NS_LOG_DEBUG("Outgoing superframe Inactive Portion duration "
1762 << inactiveDuration << " symbols (" << endInactiveTime.As(Time::S) << ")");
1764 }
1765}
1766
1767void
1769{
1771
1772 // TODO: If the device waits more than the expected time to receive the beacon (wait = 46
1773 // symbols for default beacon size)
1774 // it should continue with the start of the incoming CAP even if it did not receive the
1775 // beacon. At the moment, the start of the incoming CAP is only triggered if the beacon is
1776 // received. See MLME-SyncLoss for details.
1777}
1778
1779void
1781{
1782 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1783
1785 {
1786 MlmeSyncLossIndicationParams syncLossParams;
1787 // syncLossParams.m_logCh =
1788 syncLossParams.m_lossReason = MacStatus::BEACON_LOSS;
1789 syncLossParams.m_panId = m_macPanId;
1790 m_mlmeSyncLossIndicationCallback(syncLossParams);
1791
1792 m_beaconTrackingOn = false;
1793 m_numLostBeacons = 0;
1794 }
1795 else
1796 {
1798
1799 // Search for one more beacon
1800 uint64_t searchSymbols;
1801 Time searchBeaconTime;
1802 searchSymbols =
1804 searchBeaconTime = Seconds((double)searchSymbols / symbolRate);
1806 Simulator::Schedule(searchBeaconTime, &LrWpanMac::BeaconSearchTimeout, this);
1807 }
1808}
1809
1810void
1812{
1813 NS_LOG_FUNCTION(this << lqi << p);
1814 // The received beacon size in symbols
1815 // Beacon = Sync Header (SHR)[5 bytes] +
1816 // PHY header (PHR) [1 byte] +
1817 // PSDU (MAC header + beacon payload) [default 17 bytes]
1818 m_rxBeaconSymbols = m_phy->GetPhySHRDuration() + 1 * m_phy->GetPhySymbolsPerOctet() +
1819 (p->GetSize() * m_phy->GetPhySymbolsPerOctet());
1820
1821 // The start of Rx beacon time and start of the Incoming superframe Active Period
1822 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
1823 m_macBeaconRxTime = Simulator::Now() - Seconds(double(m_rxBeaconSymbols) / symbolRate);
1824
1825 NS_LOG_DEBUG("Beacon Received; forwarding up (m_macBeaconRxTime: "
1826 << m_macBeaconRxTime.As(Time::S) << ")");
1827
1828 // Strip the MAC header, the trailer and the Beacon Payload
1829 LrWpanMacTrailer receivedMacTrailer;
1830 p->RemoveTrailer(receivedMacTrailer);
1831
1832 LrWpanMacHeader receivedMacHdr;
1833 p->RemoveHeader(receivedMacHdr);
1834
1835 BeaconPayloadHeader receivedMacPayload;
1836 p->RemoveHeader(receivedMacPayload);
1837
1838 // Fill the PAN descriptor
1839 PanDescriptor panDescriptor;
1840
1841 if (receivedMacHdr.GetSrcAddrMode() == SHORT_ADDR)
1842 {
1843 panDescriptor.m_coorAddrMode = SHORT_ADDR;
1844 panDescriptor.m_coorShortAddr = receivedMacHdr.GetShortSrcAddr();
1845 }
1846 else
1847 {
1848 panDescriptor.m_coorAddrMode = EXT_ADDR;
1849 panDescriptor.m_coorExtAddr = receivedMacHdr.GetExtSrcAddr();
1850 }
1851
1852 panDescriptor.m_coorPanId = receivedMacHdr.GetSrcPanId();
1853 panDescriptor.m_gtsPermit = receivedMacPayload.GetGtsFields().GetGtsPermit();
1854 panDescriptor.m_linkQuality = lqi;
1855 panDescriptor.m_logChPage = m_phy->GetCurrentPage();
1856 panDescriptor.m_logCh = m_phy->GetCurrentChannelNum();
1857 panDescriptor.m_superframeSpec = receivedMacPayload.GetSuperframeSpecField();
1858 panDescriptor.m_timeStamp = m_macBeaconRxTime;
1859
1860 // Process beacon when device belongs to a PAN (associated device)
1861 if (!m_scanEvent.IsPending() && m_macPanId == receivedMacHdr.GetDstPanId())
1862 {
1863 // We need to make sure to cancel any possible ongoing unslotted CSMA/CA
1864 // operations when receiving a beacon (e.g. Those taking place at the
1865 // beginning of an Association).
1866 m_csmaCa->Cancel();
1867
1868 SuperframeField incomingSuperframe(receivedMacPayload.GetSuperframeSpecField());
1869
1870 m_incomingBeaconOrder = incomingSuperframe.GetBeaconOrder();
1871 m_incomingSuperframeOrder = incomingSuperframe.GetFrameOrder();
1872 m_incomingFnlCapSlot = incomingSuperframe.GetFinalCapSlot();
1873
1874 if (m_incomingBeaconOrder < 15)
1875 {
1876 // Start Beacon-enabled mode
1877 m_csmaCa->SetSlottedCsmaCa();
1881 (static_cast<uint32_t>(1 << m_incomingSuperframeOrder));
1882
1883 if (incomingSuperframe.IsBattLifeExt())
1884 {
1885 m_csmaCa->SetBatteryLifeExtension(true);
1886 }
1887 else
1888 {
1889 m_csmaCa->SetBatteryLifeExtension(false);
1890 }
1891
1892 // TODO: get Incoming frame GTS Fields here
1893
1894 // Begin CAP on the current device using info from
1895 // the Incoming superframe
1896 NS_LOG_DEBUG("Incoming superframe Active Portion "
1897 << "(Beacon + CAP + CFP): " << m_incomingSuperframeDuration << " symbols");
1898
1901 }
1902 else
1903 {
1904 // Start non-beacon enabled mode
1905 m_csmaCa->SetUnSlottedCsmaCa();
1906 }
1907
1909 }
1910 else if (!m_scanEvent.IsPending() && m_macPanId == 0xFFFF)
1911 {
1912 NS_LOG_DEBUG(this << " Device not associated, cannot process beacon");
1913 }
1914
1915 if (m_macAutoRequest)
1916 {
1917 if (p->GetSize() > 0)
1918 {
1920 {
1921 // The beacon contains payload, send the beacon notification.
1923 beaconParams.m_bsn = receivedMacHdr.GetSeqNum();
1924 beaconParams.m_panDescriptor = panDescriptor;
1925 beaconParams.m_sduLength = p->GetSize();
1926 beaconParams.m_sdu = p;
1928 }
1929 }
1930
1931 if (m_scanEvent.IsPending())
1932 {
1933 // Channel scanning is taking place, save only unique PAN descriptors
1934 bool descriptorExists = false;
1935
1936 for (const auto& descriptor : m_panDescriptorList)
1937 {
1938 if (descriptor.m_coorAddrMode == SHORT_ADDR)
1939 {
1940 // Found a coordinator in PAN descriptor list with the same
1941 // registered short address
1942 descriptorExists =
1943 (descriptor.m_coorShortAddr == panDescriptor.m_coorShortAddr &&
1944 descriptor.m_coorPanId == panDescriptor.m_coorPanId);
1945 }
1946 else
1947 {
1948 // Found a coordinator in PAN descriptor list with the same
1949 // registered extended address
1950 descriptorExists = (descriptor.m_coorExtAddr == panDescriptor.m_coorExtAddr &&
1951 descriptor.m_coorPanId == panDescriptor.m_coorPanId);
1952 }
1953
1954 if (descriptorExists)
1955 {
1956 break;
1957 }
1958 }
1959
1960 if (!descriptorExists)
1961 {
1962 m_panDescriptorList.emplace_back(panDescriptor);
1963 }
1964 return;
1965 }
1966 else if (m_trackingEvent.IsPending())
1967 {
1968 // check if MLME-SYNC.request was previously issued and running
1969 // Sync. is necessary to handle pending messages (indirect
1970 // transmissions)
1972 m_numLostBeacons = 0;
1973
1975 {
1976 // if tracking option is on keep tracking the next beacon
1977 uint64_t searchSymbols;
1978 Time searchBeaconTime;
1979
1980 searchSymbols = (static_cast<uint64_t>(1 << m_incomingBeaconOrder)) +
1982 searchBeaconTime = Seconds(static_cast<double>(searchSymbols / symbolRate));
1984 Simulator::Schedule(searchBeaconTime, &LrWpanMac::BeaconSearchTimeout, this);
1985 }
1986
1987 PendingAddrFields pndAddrFields;
1988 pndAddrFields = receivedMacPayload.GetPndAddrFields();
1989
1990 // TODO: Ignore pending data, and do not send data command request if
1991 // the address is in the GTS list.
1992 // If the address is not in the GTS list, then check if the
1993 // address is in the short address pending list or in the extended
1994 // address pending list and send a data command request.
1995 }
1996 }
1997 else
1998 {
1999 // m_macAutoRequest is FALSE
2000 // Data command request are not send, only the beacon notification.
2001 // see IEEE 802.15.4-2011 Section 6.2.4.1
2003 {
2005 beaconParams.m_bsn = receivedMacHdr.GetSeqNum();
2006 beaconParams.m_panDescriptor = panDescriptor;
2007 beaconParams.m_sduLength = p->GetSize();
2008 beaconParams.m_sdu = p;
2010 }
2011 }
2012}
2013
2014void
2016{
2017 NS_LOG_FUNCTION(this << lqi << p);
2018
2019 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
2020
2021 LrWpanMacTrailer receivedMacTrailer;
2022 p->RemoveTrailer(receivedMacTrailer);
2023
2024 LrWpanMacHeader receivedMacHdr;
2025 p->RemoveHeader(receivedMacHdr);
2026
2027 CommandPayloadHeader receivedMacPayload;
2028 p->RemoveHeader(receivedMacPayload);
2029
2030 switch (receivedMacPayload.GetCommandFrameType())
2031 {
2033 if (m_csmaCa->IsUnSlottedCsmaCa() && m_coor)
2034 {
2035 // Jitter = Between 0 and 2 aUnitBackoffPeriods
2036 // (0, 320us or 640us in 2.4Ghz O-QPSK)
2037 // While this jitter is not described by the standard,
2038 // it reduces the probability of collisions in beacons
2039 // transmitted as a result of a beacon request
2040 Time jitter =
2041 Seconds(static_cast<double>(m_uniformVar->GetInteger(0, 3) * aUnitBackoffPeriod) /
2042 symbolRate);
2043
2045 }
2046 else
2047 {
2048 NS_LOG_DEBUG("Beacon Request command received in beacon mode: Ignore");
2049 }
2050 break;
2052 if (m_coor)
2053 {
2055 {
2056 MlmeOrphanIndicationParams orphanParams;
2057 orphanParams.m_orphanAddr = receivedMacHdr.GetExtSrcAddr();
2058 m_mlmeOrphanIndicationCallback(orphanParams);
2059 }
2060 }
2061 break;
2064 {
2065 // Coordinator located, no need to keep scanning other channels
2067
2068 m_macPanIdScan = 0;
2071
2072 // Update the device information with the received information
2073 // from the Coordinator Realigment command.
2074 m_macPanId = receivedMacPayload.GetPanId();
2075 m_shortAddress = receivedMacPayload.GetShortAddr();
2076 m_macCoordExtendedAddress = receivedMacHdr.GetExtSrcAddr();
2077 m_macCoordShortAddress = receivedMacPayload.GetCoordShortAddr();
2078
2080 {
2081 MlmeScanConfirmParams confirmParams;
2082 confirmParams.m_scanType = m_scanParams.m_scanType;
2083 confirmParams.m_chPage = m_scanParams.m_chPage;
2084 confirmParams.m_status = MacStatus::SUCCESS;
2085 m_mlmeScanConfirmCallback(confirmParams);
2086 }
2087 m_scanParams = {};
2088 }
2089 // TODO: handle Coordinator realignment when not
2090 // used during an orphan scan.
2091 break;
2092 default:
2093 break;
2094 }
2095}
2096
2097void
2099{
2100 NS_LOG_FUNCTION(this << lqi << p);
2101
2102 LrWpanMacTrailer receivedMacTrailer;
2103 p->RemoveTrailer(receivedMacTrailer);
2104
2105 LrWpanMacHeader receivedMacHdr;
2106 p->RemoveHeader(receivedMacHdr);
2107
2108 NS_LOG_DEBUG("Data Packet is for me; forwarding up");
2109
2111 {
2113 params.m_dsn = receivedMacHdr.GetSeqNum();
2114 params.m_mpduLinkQuality = lqi;
2115 params.m_srcPanId = receivedMacHdr.GetSrcPanId();
2116 params.m_srcAddrMode = receivedMacHdr.GetSrcAddrMode();
2117
2118 switch (params.m_srcAddrMode)
2119 {
2120 case SHORT_ADDR:
2121 params.m_srcAddr = receivedMacHdr.GetShortSrcAddr();
2122 break;
2123 case EXT_ADDR:
2124 params.m_srcExtAddr = receivedMacHdr.GetExtSrcAddr();
2125 break;
2126 default:
2127 break;
2128 }
2129
2130 params.m_dstPanId = receivedMacHdr.GetDstPanId();
2131 params.m_dstAddrMode = receivedMacHdr.GetDstAddrMode();
2132
2133 switch (params.m_dstAddrMode)
2134 {
2135 case SHORT_ADDR:
2136 params.m_dstAddr = receivedMacHdr.GetShortDstAddr();
2137 break;
2138 case EXT_ADDR:
2139 params.m_dstExtAddr = receivedMacHdr.GetExtDstAddr();
2140 break;
2141 default:
2142 break;
2143 }
2144
2146 }
2147}
2148
2149void
2151{
2152 LrWpanMacHeader peekedMacHdr;
2153 p->PeekHeader(peekedMacHdr);
2154
2155 if (peekedMacHdr.GetSrcAddrMode() == SHORT_ADDR && peekedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2156 {
2157 NS_LOG_DEBUG("Packet from [" << peekedMacHdr.GetShortSrcAddr() << "] to ["
2158 << peekedMacHdr.GetShortDstAddr() << "]");
2159 }
2160 else if (peekedMacHdr.GetSrcAddrMode() == EXT_ADDR && peekedMacHdr.GetDstAddrMode() == EXT_ADDR)
2161 {
2162 NS_LOG_DEBUG("Packet from [" << peekedMacHdr.GetExtSrcAddr() << "] to ["
2163 << peekedMacHdr.GetExtDstAddr() << "]");
2164 }
2165 else if (peekedMacHdr.GetSrcAddrMode() == SHORT_ADDR &&
2166 peekedMacHdr.GetDstAddrMode() == EXT_ADDR)
2167 {
2168 NS_LOG_DEBUG("Packet from [" << peekedMacHdr.GetShortSrcAddr() << "] to ["
2169 << peekedMacHdr.GetExtDstAddr() << "]");
2170 }
2171 else if (peekedMacHdr.GetSrcAddrMode() == EXT_ADDR &&
2172 peekedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2173 {
2174 NS_LOG_DEBUG("Packet from [" << peekedMacHdr.GetExtSrcAddr() << "] to ["
2175 << peekedMacHdr.GetShortDstAddr() << "]");
2176 }
2177}
2178
2179void
2181{
2182 NS_LOG_FUNCTION(this);
2183 // Pull a packet from the queue and start sending if we are not already sending.
2184 if (m_macState == MAC_IDLE && !m_txQueue.empty() && !m_setMacState.IsPending())
2185 {
2186 if (m_csmaCa->IsUnSlottedCsmaCa() || (m_outSuperframeStatus == CAP && m_coor) ||
2188 {
2189 // check MAC is not in a IFS
2190 if (!m_ifsEvent.IsPending())
2191 {
2192 Ptr<TxQueueElement> txQElement = m_txQueue.front();
2193 m_txPkt = txQElement->txQPkt;
2194
2197 }
2198 }
2199 }
2200}
2201
2202uint16_t
2204{
2205 SuperframeField sfrmSpec;
2206
2209 sfrmSpec.SetFinalCapSlot(m_fnlCapSlot);
2210
2211 if (m_csmaCa->GetBatteryLifeExtension())
2212 {
2213 sfrmSpec.SetBattLifeExt(true);
2214 }
2215
2216 if (m_panCoor)
2217 {
2218 sfrmSpec.SetPanCoor(true);
2219 }
2220
2221 // used to associate devices via Beacons
2223 {
2224 sfrmSpec.SetAssocPermit(true);
2225 }
2226
2227 return sfrmSpec.GetSuperframe();
2228}
2229
2232{
2233 GtsFields gtsFields;
2234
2235 // TODO: Logic to populate the GTS Fields from local information here
2236
2237 return gtsFields;
2238}
2239
2242{
2243 PendingAddrFields pndAddrFields;
2244
2245 // TODO: Logic to populate the Pending Address Fields from local information here
2246 return pndAddrFields;
2247}
2248
2249void
2251{
2252 m_csmaCa = csmaCa;
2253}
2254
2255void
2257{
2258 m_phy = phy;
2259}
2260
2263{
2264 return m_phy;
2265}
2266
2267void
2269{
2271 NS_LOG_FUNCTION(this << psduLength << p << (uint16_t)lqi);
2272
2273 bool acceptFrame;
2274
2275 Ptr<Packet> originalPkt = p->Copy(); // because we will strip headers
2276 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false); // symbols per second
2277
2278 m_promiscSnifferTrace(originalPkt);
2279 m_macPromiscRxTrace(originalPkt);
2280 // XXX no rejection tracing (to macRxDropTrace) being performed below
2281
2282 LrWpanMacTrailer receivedMacTrailer;
2283 p->RemoveTrailer(receivedMacTrailer);
2284
2286 {
2287 receivedMacTrailer.EnableFcs(true);
2288 }
2289
2290 LrWpanMacHeader receivedMacHdr;
2291 p->RemoveHeader(receivedMacHdr);
2292
2293 // From section 7.5.6.2 Reception and rejection, IEEE 802.15.4-2006
2294 // - Level 1 filtering: Test FCS field and reject if frame fails.
2295 // - Level 2 filtering: If promiscuous mode pass frame to higher layer
2296 // otherwise perform Level 3 filtering.
2297 // - Level 3 filtering: Accept frame if Frame type and version is not reserved, and if
2298 // there is a dstPanId then dstPanId=m_macPanId or broadcastPanId, and if there is a
2299 // shortDstAddr then shortDstAddr=shortMacAddr or broadcastAddr, and if beacon frame then
2300 // srcPanId = m_macPanId if only srcAddr field in Data or Command frame,accept frame if
2301 // srcPanId=m_macPanId.
2302
2303 // Level 1 filtering
2304 if (!receivedMacTrailer.CheckFcs(p))
2305 {
2306 m_macRxDropTrace(originalPkt);
2307 return;
2308 }
2309
2310 // Level 2 filtering
2312 {
2313 PrintPacket(originalPkt);
2314 ReceiveInPromiscuousMode(lqi, originalPkt);
2315 return;
2316 }
2317
2318 // Level 3 frame filtering
2319 acceptFrame = (receivedMacHdr.GetType() != LrWpanMacHeader::LRWPAN_MAC_RESERVED);
2320
2321 if (acceptFrame)
2322 {
2323 acceptFrame = (receivedMacHdr.GetFrameVer() <= 1);
2324 }
2325
2326 if (acceptFrame && (receivedMacHdr.GetDstAddrMode() > 1))
2327 {
2328 // Accept frame if one of the following is true:
2329
2330 // 1) Have the same macPanId
2331 // 2) Is Message to all PANs
2332 // 3) Is a beacon or command frame and the macPanId is not present (bootstrap)
2333 acceptFrame = ((receivedMacHdr.GetDstPanId() == m_macPanId ||
2334 receivedMacHdr.GetDstPanId() == 0xffff) ||
2335 (m_macPanId == 0xffff && receivedMacHdr.IsBeacon())) ||
2336 (m_macPanId == 0xffff && receivedMacHdr.IsCommand());
2337 }
2338
2339 if (acceptFrame && (receivedMacHdr.GetDstAddrMode() == SHORT_ADDR))
2340 {
2341 if (receivedMacHdr.GetShortDstAddr() == m_shortAddress)
2342 {
2343 // unicast, for me
2344 acceptFrame = true;
2345 }
2346 else if (receivedMacHdr.GetShortDstAddr().IsBroadcast() ||
2347 receivedMacHdr.GetShortDstAddr().IsMulticast())
2348 {
2349 // Broadcast or multicast.
2350 // Discard broadcast/multicast with the ACK bit set.
2351 acceptFrame = !receivedMacHdr.IsAckReq();
2352 }
2353 else
2354 {
2355 acceptFrame = false;
2356 }
2357 }
2358
2359 if (acceptFrame && (receivedMacHdr.GetDstAddrMode() == EXT_ADDR))
2360 {
2361 acceptFrame = (receivedMacHdr.GetExtDstAddr() == m_macExtendedAddress);
2362 }
2363
2364 if (acceptFrame && m_scanEvent.IsPending())
2365 {
2366 if (!receivedMacHdr.IsBeacon())
2367 {
2368 acceptFrame = false;
2369 }
2370 }
2371 else if (acceptFrame && m_scanOrphanEvent.IsPending())
2372 {
2373 if (!receivedMacHdr.IsCommand())
2374 {
2375 acceptFrame = false;
2376 }
2377 }
2378 else if (m_scanEnergyEvent.IsPending())
2379 {
2380 // Reject any frames if energy scan is running
2381 acceptFrame = false;
2382 }
2383
2384 // Check device is panCoor with association permit when receiving Association Request
2385 // Commands.
2386 if (acceptFrame && (receivedMacHdr.IsCommand() && receivedMacHdr.IsAckReq()))
2387 {
2388 CommandPayloadHeader receivedMacPayload;
2389 p->PeekHeader(receivedMacPayload);
2390
2391 if (receivedMacPayload.GetCommandFrameType() == CommandPayloadHeader::ASSOCIATION_REQ &&
2393 {
2394 acceptFrame = false;
2395 }
2396
2397 // Although ACKs do not use CSMA to to be transmitted, we need to make sure
2398 // that the transmitted ACK will not collide with the transmission of a beacon
2399 // when beacon-enabled mode is running in the coordinator.
2400 if (acceptFrame && (m_csmaCa->IsSlottedCsmaCa() && m_capEvent.IsPending()))
2401 {
2402 Time timeLeftInCap = Simulator::GetDelayLeft(m_capEvent);
2403 uint64_t ackSymbols = lrwpan::aTurnaroundTime + m_phy->GetPhySHRDuration() +
2404 ceil(6 * m_phy->GetPhySymbolsPerOctet());
2405 Time ackTime = Seconds((double)ackSymbols / symbolRate);
2406
2407 if (ackTime >= timeLeftInCap)
2408 {
2409 NS_LOG_DEBUG("Command frame received but not enough time to transmit ACK "
2410 "before the end of CAP ");
2411 acceptFrame = false;
2412 }
2413 }
2414 }
2415
2416 if (!acceptFrame)
2417 {
2418 m_macRxDropTrace(originalPkt);
2419 return;
2420 }
2421
2422 m_macRxTrace(originalPkt);
2423 // \todo: What should we do if we receive a frame while waiting for an ACK?
2424 // Especially if this frame has the ACK request bit set, should we reply with
2425 // an ACK, possibly missing the pending ACK?
2426
2427 // If the received frame is a frame with the ACK request bit set, we immediately
2428 // send back an ACK. If we are currently waiting for a pending ACK, we assume the
2429 // ACK was lost and trigger a retransmission after sending the ACK.
2430 if ((receivedMacHdr.IsData() || receivedMacHdr.IsCommand()) && receivedMacHdr.IsAckReq() &&
2431 !(receivedMacHdr.GetDstAddrMode() == SHORT_ADDR &&
2432 (receivedMacHdr.GetShortDstAddr().IsBroadcast() ||
2433 receivedMacHdr.GetShortDstAddr().IsMulticast())))
2434 {
2435 // If this is a data or mac command frame, which is not a broadcast or
2436 // multicast, with ack req set, generate and send an ack frame. If there is a
2437 // CSMA medium access in progress we cancel the medium access for sending the
2438 // ACK frame. A new transmission attempt will be started after the ACK was send.
2440 {
2443 }
2444 else if (m_macState == MAC_CSMA)
2445 {
2446 // \todo: If we receive a packet while doing CSMA/CA, should we drop the
2447 // packet because of channel busy,
2448 // or should we restart CSMA/CA for the packet after sending the ACK?
2449 // Currently we simply restart CSMA/CA after sending the ACK.
2450 NS_LOG_DEBUG("Received a packet with ACK required while in CSMA. Cancel "
2451 "current CSMA-CA");
2452 m_csmaCa->Cancel();
2453 }
2454 // Cancel any pending MAC state change, ACKs have higher priority.
2457
2458 // save received packet and LQI to process the appropriate indication/response
2459 // after sending ACK (PD-DATA.confirm)
2460 m_rxPkt = originalPkt->Copy();
2461 m_lastRxFrameLqi = lqi;
2462
2463 // LOG Commands with ACK required.
2464 CommandPayloadHeader receivedMacPayload;
2465 p->PeekHeader(receivedMacPayload);
2466 switch (receivedMacPayload.GetCommandFrameType())
2467 {
2469 NS_LOG_DEBUG("Data Request Command Received; processing ACK");
2470 break;
2472 NS_LOG_DEBUG("Association Request Command Received; processing ACK");
2473 break;
2476 {
2477 m_assocResCmdWaitTimeout.Cancel(); // cancel event to a lost assoc resp cmd.
2478 NS_LOG_DEBUG("Association Response Command Received; processing ACK");
2479 }
2480 else
2481 {
2482 // Association response command was received before (or never received)
2483 // a Data request command ACK. This is an extreme case and it is
2484 // essentially caused by saturation in the network.
2485 // We turn a flag ON to not react once
2486 // we finally receive the Data request command ACK. This behavior is not
2487 // standard, but necessary to address this flaw in design of the
2488 // original association process.
2489 m_ignoreDataCmdAck = true;
2490 NS_LOG_DEBUG("Assoc. Resp Cmd received before Data Req. Cmd. in "
2491 "Association request");
2492 }
2493 break;
2494 }
2495 default:
2496 break;
2497 }
2498
2500 Simulator::ScheduleNow(&LrWpanMac::SendAck, this, receivedMacHdr.GetSeqNum());
2501 }
2502
2503 PrintPacket(originalPkt);
2504
2505 if (receivedMacHdr.IsBeacon())
2506 {
2507 ReceiveBeacon(lqi, originalPkt);
2508 }
2509 else if (receivedMacHdr.IsCommand())
2510 {
2511 ReceiveCommand(lqi, originalPkt);
2512 }
2513 else if (receivedMacHdr.IsData())
2514 {
2515 ReceiveData(lqi, originalPkt);
2516 }
2517 else if (receivedMacHdr.IsAcknowledgment() && m_txPkt && m_macState == MAC_ACK_PENDING)
2518 {
2519 LrWpanMacHeader peekedMacHdr;
2520 m_txPkt->PeekHeader(peekedMacHdr);
2521 // If it is an ACK with the expected sequence number, finish the transmission
2522 if (receivedMacHdr.GetSeqNum() == peekedMacHdr.GetSeqNum())
2523 {
2526
2527 // TODO: check if the IFS is the correct size after ACK.
2528 Time ifsWaitTime = Seconds((double)GetIfsSize() / symbolRate);
2529
2530 // We received an ACK to a command
2531 if (peekedMacHdr.IsCommand())
2532 {
2533 // check the original sent command frame which belongs to this received
2534 // ACK
2535 Ptr<Packet> pkt = m_txPkt->Copy();
2536 LrWpanMacHeader macHdr;
2537 CommandPayloadHeader cmdPayload;
2538 pkt->RemoveHeader(macHdr);
2539 pkt->RemoveHeader(cmdPayload);
2540
2541 switch (cmdPayload.GetCommandFrameType())
2542 {
2544 double symbolRate = m_phy->GetDataOrSymbolRate(false);
2545 Time waitTime =
2546 Seconds(static_cast<double>(m_macResponseWaitTime) / symbolRate);
2547 if (!m_beaconTrackingOn)
2548 {
2551 }
2552 else
2553 {
2554 // TODO: The data must be extracted by the coordinator within
2555 // macResponseWaitTime on timeout, MLME-ASSOCIATE.confirm is set
2556 // with status NO_DATA, and this should trigger the cancellation
2557 // of the beacon tracking (MLME-SYNC.request trackBeacon
2558 // =FALSE)
2559 }
2560 break;
2561 }
2562
2564 // MLME-comm-status.Indication generated as a result of an
2565 // association response command, therefore src and dst address use
2566 // extended mode (see 5.3.2.1)
2568 {
2569 MlmeCommStatusIndicationParams commStatusParams;
2570 commStatusParams.m_panId = m_macPanId;
2571 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2572 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
2573 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2574 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
2575 commStatusParams.m_status = MacStatus::SUCCESS;
2576 m_mlmeCommStatusIndicationCallback(commStatusParams);
2577 }
2578 // Remove element from Pending Transaction List
2580 break;
2581 }
2582
2584 if (!m_ignoreDataCmdAck)
2585 {
2586 // Schedule an event in case the Association Response Command
2587 // never reached this device during an association process.
2588 double symbolRate = m_phy->GetDataOrSymbolRate(false);
2589 Time waitTime =
2590 Seconds(static_cast<double>(m_assocRespCmdWaitTime) / symbolRate);
2593 }
2594
2596 {
2597 MlmePollConfirmParams pollConfirmParams;
2598 pollConfirmParams.m_status = MacStatus::SUCCESS;
2599 m_mlmePollConfirmCallback(pollConfirmParams);
2600 }
2601 break;
2602 }
2603
2605 // ACK of coordinator realigment commands is not specified in the
2606 // standard, in here, we assume they are required as in other
2607 // commands.
2609 {
2610 MlmeCommStatusIndicationParams commStatusParams;
2611 commStatusParams.m_panId = m_macPanId;
2612 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2613 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
2614 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2615 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
2616 commStatusParams.m_status = MacStatus::SUCCESS;
2617 m_mlmeCommStatusIndicationCallback(commStatusParams);
2618 }
2619 }
2620
2621 default: {
2622 // TODO: add response to other request commands (e.g. Orphan)
2623 break;
2624 }
2625 }
2626 }
2627 else
2628 {
2630 {
2631 Ptr<TxQueueElement> txQElement = m_txQueue.front();
2632 McpsDataConfirmParams confirmParams;
2633 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
2634 confirmParams.m_status = MacStatus::SUCCESS;
2635 m_mcpsDataConfirmCallback(confirmParams);
2636 }
2637 }
2638
2639 // Ack was successfully received, wait for the Interframe Space (IFS) and
2640 // then proceed
2644 m_ifsEvent =
2645 Simulator::Schedule(ifsWaitTime, &LrWpanMac::IfsWaitTimeout, this, ifsWaitTime);
2646 }
2647 else
2648 {
2649 // If it is an ACK with an unexpected sequence number, mark the current
2650 // transmission as failed and start a retransmit. (cf 7.5.6.4.3)
2652 if (!PrepareRetransmission())
2653 {
2657 }
2658 else
2659 {
2663 }
2664 }
2665 }
2666}
2667
2668void
2670{
2671 NS_LOG_FUNCTION(this << static_cast<uint32_t>(seqno));
2672
2674
2675 // Generate a corresponding ACK Frame.
2677 LrWpanMacTrailer macTrailer;
2678 Ptr<Packet> ackPacket = Create<Packet>(0);
2679 ackPacket->AddHeader(macHdr);
2680 // Calculate FCS if the global attribute ChecksumEnabled is set.
2682 {
2683 macTrailer.EnableFcs(true);
2684 macTrailer.SetFcs(ackPacket);
2685 }
2686 ackPacket->AddTrailer(macTrailer);
2687
2688 // Enqueue the ACK packet for further processing
2689 // when the transmitter is activated.
2690 m_txPkt = ackPacket;
2691
2692 // Switch transceiver to TX mode. Proceed sending the Ack on confirm.
2694 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
2695}
2696
2697void
2699{
2700 if (m_txQueue.size() < m_maxTxQueueSize)
2701 {
2702 m_txQueue.emplace_back(txQElement);
2703 m_macTxEnqueueTrace(txQElement->txQPkt);
2704 }
2705 else
2706 {
2708 {
2709 McpsDataConfirmParams confirmParams;
2710 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
2712 m_mcpsDataConfirmCallback(confirmParams);
2713 }
2714 NS_LOG_DEBUG("TX Queue with size " << m_txQueue.size() << " is full, dropping packet");
2715 m_macTxDropTrace(txQElement->txQPkt);
2716 }
2717}
2718
2719void
2721{
2722 Ptr<TxQueueElement> txQElement = m_txQueue.front();
2723 Ptr<const Packet> p = txQElement->txQPkt;
2724 m_numCsmacaRetry += m_csmaCa->GetNB() + 1;
2725
2726 Ptr<Packet> pkt = p->Copy();
2727 LrWpanMacHeader hdr;
2728 pkt->RemoveHeader(hdr);
2729 if (!hdr.GetShortDstAddr().IsBroadcast() && !hdr.GetShortDstAddr().IsMulticast())
2730 {
2732 }
2733
2734 txQElement->txQPkt = nullptr;
2735 txQElement = nullptr;
2736 m_txQueue.pop_front();
2737 m_txPkt = nullptr;
2738 m_retransmission = 0;
2739 m_numCsmacaRetry = 0;
2741}
2742
2743void
2745{
2746 NS_LOG_FUNCTION(this);
2747
2748 // TODO: If we are a PAN coordinator and this was an indirect transmission,
2749 // we will not initiate a retransmission. Instead we wait for the data
2750 // being extracted after a new data request command.
2751 if (!PrepareRetransmission())
2752 {
2754 }
2755 else
2756 {
2758 }
2759}
2760
2761void
2763{
2764 auto symbolRate = (uint64_t)m_phy->GetDataOrSymbolRate(false);
2765 Time lifsTime = Seconds((double)m_macLIFSPeriod / symbolRate);
2766 Time sifsTime = Seconds((double)m_macSIFSPeriod / symbolRate);
2767
2768 if (ifsTime == lifsTime)
2769 {
2770 NS_LOG_DEBUG("LIFS of " << m_macLIFSPeriod << " symbols (" << ifsTime.As(Time::S)
2771 << ") completed ");
2772 }
2773 else if (ifsTime == sifsTime)
2774 {
2775 NS_LOG_DEBUG("SIFS of " << m_macSIFSPeriod << " symbols (" << ifsTime.As(Time::S)
2776 << ") completed ");
2777 }
2778 else
2779 {
2780 NS_LOG_DEBUG("Unknown IFS size (" << ifsTime.As(Time::S) << ") completed ");
2781 }
2782
2783 m_macIfsEndTrace(ifsTime);
2784 CheckQueue();
2785}
2786
2787bool
2789{
2790 NS_LOG_FUNCTION(this);
2791
2792 // Max retransmissions reached without receiving ACK,
2793 // send the proper indication/confirmation
2794 // according to the frame type and call drop trace.
2796 {
2797 LrWpanMacHeader peekedMacHdr;
2798 m_txPkt->PeekHeader(peekedMacHdr);
2799
2800 if (peekedMacHdr.IsCommand())
2801 {
2803
2804 Ptr<Packet> pkt = m_txPkt->Copy();
2805 LrWpanMacHeader macHdr;
2806 CommandPayloadHeader cmdPayload;
2807 pkt->RemoveHeader(macHdr);
2808 pkt->RemoveHeader(cmdPayload);
2809
2810 switch (cmdPayload.GetCommandFrameType())
2811 {
2813 m_macPanId = 0xffff;
2815 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
2818 m_csmaCa->SetUnSlottedCsmaCa();
2821
2823 {
2824 MlmeAssociateConfirmParams confirmParams;
2825 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
2826 confirmParams.m_status = MacStatus::NO_ACK;
2827 m_mlmeAssociateConfirmCallback(confirmParams);
2828 }
2829 break;
2830 }
2832 // IEEE 802.15.4-2006 (Section 7.1.3.3.3 and 7.1.8.2.3)
2834 {
2835 MlmeCommStatusIndicationParams commStatusParams;
2836 commStatusParams.m_panId = m_macPanId;
2837 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2838 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
2839 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2840 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
2841 commStatusParams.m_status = MacStatus::NO_ACK;
2842 m_mlmeCommStatusIndicationCallback(commStatusParams);
2843 }
2845 break;
2846 }
2848 // IEEE 802.15.4-2006 (Section 7.1.16.1.3)
2849 m_macPanId = 0xffff;
2851 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
2854 m_csmaCa->SetUnSlottedCsmaCa();
2857
2859 {
2860 MlmePollConfirmParams pollConfirmParams;
2861 pollConfirmParams.m_status = MacStatus::NO_ACK;
2862 m_mlmePollConfirmCallback(pollConfirmParams);
2863 }
2864 break;
2865 }
2866 default: {
2867 // TODO: Specify other indications according to other commands
2868 break;
2869 }
2870 }
2871 }
2872 else
2873 {
2874 // Maximum number of retransmissions has been reached.
2875 // remove the copy of the DATA packet that was just sent
2876 Ptr<TxQueueElement> txQElement = m_txQueue.front();
2877 m_macTxDropTrace(txQElement->txQPkt);
2879 {
2880 McpsDataConfirmParams confirmParams;
2881 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
2882 confirmParams.m_status = MacStatus::NO_ACK;
2883 m_mcpsDataConfirmCallback(confirmParams);
2884 }
2885 }
2886
2888 return false;
2889 }
2890 else
2891 {
2893 m_numCsmacaRetry += m_csmaCa->GetNB() + 1;
2894 // Start next CCA process for this packet.
2895 return true;
2896 }
2897}
2898
2899void
2901{
2903 LrWpanMacHeader peekedMacHdr;
2904 p->PeekHeader(peekedMacHdr);
2905
2906 PurgeInd();
2907
2908 NS_ASSERT(peekedMacHdr.GetDstAddrMode() == SHORT_ADDR ||
2909 peekedMacHdr.GetDstAddrMode() == EXT_ADDR);
2910
2911 if (peekedMacHdr.GetDstAddrMode() == SHORT_ADDR)
2912 {
2913 indTxQElement->dstShortAddress = peekedMacHdr.GetShortDstAddr();
2914 }
2915 else
2916 {
2917 indTxQElement->dstExtAddress = peekedMacHdr.GetExtDstAddr();
2918 }
2919
2920 indTxQElement->seqNum = peekedMacHdr.GetSeqNum();
2921
2922 // See IEEE 802.15.4-2006, Table 86
2923 uint32_t unit = 0; // The persistence time in symbols
2924 if (m_macBeaconOrder == 15)
2925 {
2926 // Non-beacon enabled mode
2928 }
2929 else
2930 {
2931 // Beacon-enabled mode
2932 unit = ((static_cast<uint32_t>(1) << m_macBeaconOrder) * lrwpan::aBaseSuperframeDuration) *
2934 }
2935
2936 if (m_indTxQueue.size() < m_maxIndTxQueueSize)
2937 {
2938 double symbolRate = m_phy->GetDataOrSymbolRate(false);
2939 Time expireTime = Seconds(unit / symbolRate);
2940 expireTime += Simulator::Now();
2941 indTxQElement->expireTime = expireTime;
2942 indTxQElement->txQPkt = p;
2943 m_indTxQueue.emplace_back(indTxQElement);
2945 }
2946 else
2947 {
2949 {
2950 LrWpanMacHeader peekedMacHdr;
2951 indTxQElement->txQPkt->PeekHeader(peekedMacHdr);
2952 MlmeCommStatusIndicationParams commStatusParams;
2953 commStatusParams.m_panId = m_macPanId;
2954 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
2955 commStatusParams.m_srcExtAddr = peekedMacHdr.GetExtSrcAddr();
2956 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
2957 commStatusParams.m_dstExtAddr = peekedMacHdr.GetExtDstAddr();
2958 commStatusParams.m_status = MacStatus::TRANSACTION_OVERFLOW;
2959 m_mlmeCommStatusIndicationCallback(commStatusParams);
2960 }
2962 }
2963}
2964
2965bool
2967{
2968 PurgeInd();
2969
2970 for (auto iter = m_indTxQueue.begin(); iter != m_indTxQueue.end(); iter++)
2971 {
2972 if ((*iter)->dstExtAddress == dst)
2973 {
2974 *entry = **iter;
2975 m_macIndTxDequeueTrace((*iter)->txQPkt->Copy());
2976 m_indTxQueue.erase(iter);
2977 return true;
2978 }
2979 }
2980 return false;
2981}
2982
2983void
2985{
2986 for (uint32_t i = 0; i < m_indTxQueue.size();)
2987 {
2988 if (Simulator::Now() > m_indTxQueue[i]->expireTime)
2989 {
2990 // Transaction expired, remove and send proper confirmation/indication to a higher layer
2991 LrWpanMacHeader peekedMacHdr;
2992 m_indTxQueue[i]->txQPkt->PeekHeader(peekedMacHdr);
2993
2994 if (peekedMacHdr.IsCommand())
2995 {
2996 // IEEE 802.15.4-2006 (Section 7.1.3.3.3)
2998 {
2999 MlmeCommStatusIndicationParams commStatusParams;
3000 commStatusParams.m_panId = m_macPanId;
3001 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
3002 commStatusParams.m_srcExtAddr = peekedMacHdr.GetExtSrcAddr();
3003 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
3004 commStatusParams.m_dstExtAddr = peekedMacHdr.GetExtDstAddr();
3005 commStatusParams.m_status = MacStatus::TRANSACTION_EXPIRED;
3006 m_mlmeCommStatusIndicationCallback(commStatusParams);
3007 }
3008 }
3009 else if (peekedMacHdr.IsData())
3010 {
3011 // IEEE 802.15.4-2006 (Section 7.1.1.1.3)
3013 {
3014 McpsDataConfirmParams confParams;
3016 m_mcpsDataConfirmCallback(confParams);
3017 }
3018 }
3019 m_macIndTxDropTrace(m_indTxQueue[i]->txQPkt->Copy());
3020 m_indTxQueue.erase(m_indTxQueue.begin() + i);
3021 }
3022 else
3023 {
3024 i++;
3025 }
3026 }
3027}
3028
3029void
3030LrWpanMac::PrintPendingTxQueue(std::ostream& os) const
3031{
3032 LrWpanMacHeader peekedMacHdr;
3033
3034 os << "Pending Transaction List [" << GetShortAddress() << " | " << GetExtendedAddress()
3035 << "] | CurrentTime: " << Simulator::Now().As(Time::S) << "\n"
3036 << " Destination |"
3037 << " Sequence Number |"
3038 << " Frame type |"
3039 << " Expire time\n";
3040
3041 for (auto transaction : m_indTxQueue)
3042 {
3043 transaction->txQPkt->PeekHeader(peekedMacHdr);
3044 os << transaction->dstExtAddress << " "
3045 << static_cast<uint32_t>(transaction->seqNum) << " ";
3046
3047 if (peekedMacHdr.IsCommand())
3048 {
3049 os << " Command Frame ";
3050 }
3051 else if (peekedMacHdr.IsData())
3052 {
3053 os << " Data Frame ";
3054 }
3055 else
3056 {
3057 os << " Unknown Frame ";
3058 }
3059
3060 os << transaction->expireTime.As(Time::S) << "\n";
3061 }
3062}
3063
3064void
3065LrWpanMac::PrintTxQueue(std::ostream& os) const
3066{
3067 LrWpanMacHeader peekedMacHdr;
3068
3069 os << "\nTx Queue [" << GetShortAddress() << " | " << GetExtendedAddress()
3070 << "] | CurrentTime: " << Simulator::Now().As(Time::S) << "\n"
3071 << " Destination |"
3072 << " Sequence Number |"
3073 << " Dst PAN id |"
3074 << " Frame type |\n";
3075
3076 for (auto transaction : m_txQueue)
3077 {
3078 transaction->txQPkt->PeekHeader(peekedMacHdr);
3079
3080 os << "[" << peekedMacHdr.GetShortDstAddr() << "]"
3081 << ", [" << peekedMacHdr.GetExtDstAddr() << "] "
3082 << static_cast<uint32_t>(peekedMacHdr.GetSeqNum()) << " "
3083 << peekedMacHdr.GetDstPanId() << " ";
3084
3085 if (peekedMacHdr.IsCommand())
3086 {
3087 os << " Command Frame ";
3088 }
3089 else if (peekedMacHdr.IsData())
3090 {
3091 os << " Data Frame ";
3092 }
3093 else
3094 {
3095 os << " Unknown Frame ";
3096 }
3097
3098 os << "\n";
3099 }
3100 os << "\n";
3101}
3102
3103int64_t
3105{
3106 NS_LOG_FUNCTION(this);
3107 m_uniformVar->SetStream(stream);
3108 m_csmaCa->AssignStreams(stream + 1);
3109 return 2;
3110}
3111
3112void
3114{
3115 LrWpanMacHeader peekedMacHdr;
3116 p->PeekHeader(peekedMacHdr);
3117
3118 for (auto it = m_indTxQueue.begin(); it != m_indTxQueue.end(); it++)
3119 {
3120 if (peekedMacHdr.GetDstAddrMode() == EXT_ADDR)
3121 {
3122 if (((*it)->dstExtAddress == peekedMacHdr.GetExtDstAddr()) &&
3123 ((*it)->seqNum == peekedMacHdr.GetSeqNum()))
3124 {
3126 m_indTxQueue.erase(it);
3127 break;
3128 }
3129 }
3130 else if (peekedMacHdr.GetDstAddrMode() == SHORT_ADDR)
3131 {
3132 if (((*it)->dstShortAddress == peekedMacHdr.GetShortDstAddr()) &&
3133 ((*it)->seqNum == peekedMacHdr.GetSeqNum()))
3134 {
3136 m_indTxQueue.erase(it);
3137 break;
3138 }
3139 }
3140 }
3141
3142 p = nullptr;
3143}
3144
3145void
3147{
3149 NS_LOG_FUNCTION(this << status << m_txQueue.size());
3150
3151 LrWpanMacHeader macHdr;
3152 Time ifsWaitTime;
3153 double symbolRate;
3154
3155 symbolRate = m_phy->GetDataOrSymbolRate(false); // symbols per second
3156
3157 m_txPkt->PeekHeader(macHdr);
3158
3159 if (status == IEEE_802_15_4_PHY_SUCCESS)
3160 {
3161 if (!macHdr.IsAcknowledgment())
3162 {
3163 if (macHdr.IsBeacon())
3164 {
3165 // Start CAP only if we are in beacon mode (i.e. if slotted csma-ca is running)
3166 if (m_csmaCa->IsSlottedCsmaCa())
3167 {
3168 // The Tx Beacon in symbols
3169 // Beacon = 5 bytes Sync Header (SHR) + 1 byte PHY header (PHR) + PSDU (default
3170 // 17 bytes)
3171 uint64_t beaconSymbols = m_phy->GetPhySHRDuration() +
3172 1 * m_phy->GetPhySymbolsPerOctet() +
3173 (m_txPkt->GetSize() * m_phy->GetPhySymbolsPerOctet());
3174
3175 // The beacon Tx time and start of the Outgoing superframe Active Period
3177 Simulator::Now() - Seconds(static_cast<double>(beaconSymbols) / symbolRate);
3178
3180 this,
3182 NS_LOG_DEBUG("Beacon Sent (m_macBeaconTxTime: " << m_macBeaconTxTime.As(Time::S)
3183 << ")");
3184
3186 {
3187 MlmeStartConfirmParams mlmeConfirmParams;
3188 mlmeConfirmParams.m_status = MacStatus::SUCCESS;
3189 m_mlmeStartConfirmCallback(mlmeConfirmParams);
3190 }
3191 }
3192
3193 ifsWaitTime = Seconds(static_cast<double>(GetIfsSize()) / symbolRate);
3194
3195 if (m_csmaCa->IsSlottedCsmaCa())
3196 {
3197 // The beacon was sent immediately in beacon-enabled mode
3198 m_txPkt = nullptr;
3199 }
3200 else
3201 {
3202 // The beacon was sent using CSMA/CA as a result of a beacon request
3203 // therefore, remove it from TX Queue
3205 }
3206 }
3207 else if (macHdr.IsAckReq()) // We have sent a regular data packet, check if we have to
3208 // wait for an ACK.
3209 {
3210 // we sent a regular data frame or command frame (e.g. AssocReq command) that
3211 // require ACK wait for the ack or the next retransmission timeout start
3212 // retransmission timer
3213 Time waitTime = Seconds(static_cast<double>(GetMacAckWaitDuration()) / symbolRate);
3219 return;
3220 }
3221 else if (macHdr.IsCommand())
3222 {
3223 // We handle commands that do not require ACK
3224 // (e.g. Coordinator realigment command in an orphan response)
3225 // Other command with ACK required are handle by the previous if statement.
3226
3227 // Check the transmitted packet command type and issue the appropriate indication.
3228 Ptr<Packet> txOriginalPkt = m_txPkt->Copy();
3229 LrWpanMacHeader txMacHdr;
3230 txOriginalPkt->RemoveHeader(txMacHdr);
3231 CommandPayloadHeader txMacPayload;
3232 txOriginalPkt->RemoveHeader(txMacPayload);
3233
3235 {
3237 {
3238 MlmeCommStatusIndicationParams commStatusParams;
3239 commStatusParams.m_panId = m_macPanId;
3240
3241 commStatusParams.m_srcAddrMode = macHdr.GetSrcAddrMode();
3242 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
3243 commStatusParams.m_srcShortAddr = macHdr.GetShortSrcAddr();
3244
3245 commStatusParams.m_dstAddrMode = macHdr.GetDstAddrMode();
3246 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
3247 commStatusParams.m_dstShortAddr = macHdr.GetShortDstAddr();
3248
3249 commStatusParams.m_status = MacStatus::SUCCESS;
3250 m_mlmeCommStatusIndicationCallback(commStatusParams);
3251 }
3252 }
3253
3254 ifsWaitTime = Seconds(static_cast<double>(GetIfsSize()) / symbolRate);
3256 }
3257 else
3258 {
3260 // remove the copy of the packet that was just sent
3262 {
3263 McpsDataConfirmParams confirmParams;
3264 NS_ASSERT_MSG(!m_txQueue.empty(), "TxQsize = 0");
3265 Ptr<TxQueueElement> txQElement = m_txQueue.front();
3266 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
3267 confirmParams.m_status = MacStatus::SUCCESS;
3268 m_mcpsDataConfirmCallback(confirmParams);
3269 }
3270 ifsWaitTime = Seconds(static_cast<double>(GetIfsSize()) / symbolRate);
3272 }
3273 }
3274 else
3275 {
3276 // The packet sent was a successful ACK
3277
3278 // Check the received frame before the transmission of the ACK,
3279 // and send the appropriate Indication or Confirmation
3280 Ptr<Packet> recvOriginalPkt = m_rxPkt->Copy();
3281 LrWpanMacHeader receivedMacHdr;
3282 recvOriginalPkt->RemoveHeader(receivedMacHdr);
3283
3284 if (receivedMacHdr.IsCommand())
3285 {
3286 CommandPayloadHeader receivedMacPayload;
3287 recvOriginalPkt->RemoveHeader(receivedMacPayload);
3288
3289 if (receivedMacPayload.GetCommandFrameType() ==
3291 {
3293 {
3294 // NOTE: The LQI parameter is not part of the standard but found
3295 // in some implementations as is required for higher layers (See Zboss
3296 // implementation).
3297 MlmeAssociateIndicationParams associateParams;
3298 associateParams.capabilityInfo = receivedMacPayload.GetCapabilityField();
3299 associateParams.m_extDevAddr = receivedMacHdr.GetExtSrcAddr();
3300 associateParams.lqi = m_lastRxFrameLqi;
3301 m_mlmeAssociateIndicationCallback(associateParams);
3302 }
3303
3304 // Clear the packet buffer for the packet request received.
3305 m_rxPkt = nullptr;
3306 }
3307 else if (receivedMacPayload.GetCommandFrameType() ==
3309 {
3310 MlmeAssociateConfirmParams confirmParams;
3311
3312 switch (static_cast<MacStatus>(receivedMacPayload.GetAssociationStatus()))
3313 {
3314 case MacStatus::SUCCESS:
3315 // The assigned short address by the coordinator
3316 SetShortAddress(receivedMacPayload.GetShortAddr());
3317 m_macPanId = receivedMacHdr.GetSrcPanId();
3318
3319 confirmParams.m_status = MacStatus::SUCCESS;
3320 confirmParams.m_assocShortAddr = GetShortAddress();
3321 break;
3323 confirmParams.m_status = MacStatus::FULL_CAPACITY;
3324 m_macPanId = 0xffff;
3326 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3329 m_csmaCa->SetUnSlottedCsmaCa();
3332 break;
3334 default:
3335 confirmParams.m_status = MacStatus::ACCESS_DENIED;
3336 m_macPanId = 0xffff;
3338 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3341 m_csmaCa->SetUnSlottedCsmaCa();
3344 break;
3345 }
3346
3348 {
3349 m_mlmeAssociateConfirmCallback(confirmParams);
3350 }
3351 }
3352 else if (receivedMacPayload.GetCommandFrameType() == CommandPayloadHeader::DATA_REQ)
3353 {
3354 // We enqueue the the Assoc Response command frame in the Tx queue
3355 // and the packet is transmitted as soon as the PHY is free and the IFS have
3356 // taken place.
3358 }
3359 }
3360
3361 // Clear the packet buffer for the ACK packet sent.
3362 m_txPkt = nullptr;
3363 }
3364 }
3365 else if (status == IEEE_802_15_4_PHY_UNSPECIFIED)
3366 {
3367 if (!macHdr.IsAcknowledgment())
3368 {
3369 NS_ASSERT_MSG(!m_txQueue.empty(), "TxQsize = 0");
3370 Ptr<TxQueueElement> txQElement = m_txQueue.front();
3371 m_macTxDropTrace(txQElement->txQPkt);
3373 {
3374 McpsDataConfirmParams confirmParams;
3375 confirmParams.m_msduHandle = txQElement->txQMsduHandle;
3376 confirmParams.m_status = MacStatus::FRAME_TOO_LONG;
3377 m_mcpsDataConfirmCallback(confirmParams);
3378 }
3380 }
3381 else
3382 {
3383 NS_LOG_ERROR("Unable to send ACK");
3384 }
3385 }
3386 else
3387 {
3388 // Something went really wrong. The PHY is not in the correct state for
3389 // data transmission.
3390 NS_FATAL_ERROR("Transmission attempt failed with PHY status " << status);
3391 }
3392
3393 if (!ifsWaitTime.IsZero())
3394 {
3395 m_ifsEvent =
3396 Simulator::Schedule(ifsWaitTime, &LrWpanMac::IfsWaitTimeout, this, ifsWaitTime);
3397 }
3398
3401}
3402
3403void
3405{
3406 NS_LOG_FUNCTION(this << status);
3407 // Direct this call through the csmaCa object
3408 m_csmaCa->PlmeCcaConfirm(status);
3409}
3410
3411void
3412LrWpanMac::PlmeEdConfirm(PhyEnumeration status, uint8_t energyLevel)
3413{
3414 NS_LOG_FUNCTION(this << status << energyLevel);
3415
3416 if (energyLevel > m_maxEnergyLevel)
3417 {
3418 m_maxEnergyLevel = energyLevel;
3419 }
3420
3422 Seconds(8.0 / m_phy->GetDataOrSymbolRate(false)))
3423 {
3424 m_phy->PlmeEdRequest();
3425 }
3426}
3427
3428void
3431 Ptr<PhyPibAttributes> attribute)
3432{
3433 NS_LOG_FUNCTION(this << status << id << attribute);
3434}
3435
3436void
3438{
3439 NS_LOG_FUNCTION(this << status);
3440
3441 if (m_macState == MAC_SENDING &&
3442 (status == IEEE_802_15_4_PHY_TX_ON || status == IEEE_802_15_4_PHY_SUCCESS))
3443 {
3445
3446 // Start sending if we are in state SENDING and the PHY transmitter was enabled.
3450 m_phy->PdDataRequest(m_txPkt->GetSize(), m_txPkt);
3451 }
3452 else if (m_macState == MAC_CSMA &&
3453 (status == IEEE_802_15_4_PHY_RX_ON || status == IEEE_802_15_4_PHY_SUCCESS))
3454 {
3455 // Start the CSMA algorithm as soon as the receiver is enabled.
3456 m_csmaCa->Start();
3457 }
3458 else if (m_macState == MAC_IDLE)
3459 {
3461 status == IEEE_802_15_4_PHY_TRX_OFF);
3462
3464 {
3465 // Kick start Energy Detection Scan
3466 m_phy->PlmeEdRequest();
3467 }
3468 else if (status == IEEE_802_15_4_PHY_RX_ON || status == IEEE_802_15_4_PHY_SUCCESS)
3469 {
3470 // Check if there is not messages to transmit when going idle
3471 CheckQueue();
3472 }
3473 }
3474 else if (m_macState == MAC_ACK_PENDING)
3475 {
3477 }
3478 else
3479 {
3480 // TODO: What to do when we receive an error?
3481 // If we want to transmit a packet, but switching the transceiver on results
3482 // in an error, we have to recover somehow (and start sending again).
3483 NS_FATAL_ERROR("Error changing transceiver state");
3484 }
3485}
3486
3487void
3489{
3490 NS_LOG_FUNCTION(this << status << id);
3492 {
3494 {
3495 // get the first channel to scan from scan channel list
3496 bool channelFound = false;
3497 for (int i = m_channelScanIndex; i <= 26; i++)
3498 {
3500 {
3501 channelFound = true;
3502 break;
3503 }
3505 }
3506
3507 if (channelFound)
3508 {
3510 pibAttr->phyCurrentChannel = m_channelScanIndex;
3511 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel,
3512 pibAttr);
3513 }
3514 }
3515 else
3516 {
3518 {
3519 MlmeScanConfirmParams confirmParams;
3520 confirmParams.m_scanType = m_scanParams.m_scanType;
3521 confirmParams.m_chPage = m_scanParams.m_chPage;
3522 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3523 m_mlmeScanConfirmCallback(confirmParams);
3524 }
3525 NS_LOG_ERROR(this << "Channel Scan: Invalid channel page");
3526 }
3527 }
3529 {
3531 {
3532 auto symbolRate = static_cast<uint64_t>(m_phy->GetDataOrSymbolRate(false));
3533 Time nextScanTime;
3534
3536 {
3537 nextScanTime = Seconds(static_cast<double>(m_macResponseWaitTime) / symbolRate);
3538 }
3539 else
3540 {
3541 uint64_t scanDurationSym =
3543
3544 nextScanTime = Seconds(static_cast<double>(scanDurationSym) / symbolRate);
3545 }
3546
3547 switch (m_scanParams.m_scanType)
3548 {
3549 case MLMESCAN_ED:
3550 m_maxEnergyLevel = 0;
3553 // set phy to RX_ON and kick start the first PLME-ED.request
3554 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3555 break;
3556 case MLMESCAN_ACTIVE:
3559 break;
3560 case MLMESCAN_PASSIVE:
3562 // turn back the phy to RX_ON after setting Page/channel
3563 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3564 break;
3565 case MLMESCAN_ORPHAN:
3567 Simulator::Schedule(nextScanTime, &LrWpanMac::EndChannelScan, this);
3569 break;
3570
3571 default:
3572 MlmeScanConfirmParams confirmParams;
3573 confirmParams.m_scanType = m_scanParams.m_scanType;
3574 confirmParams.m_chPage = m_scanParams.m_chPage;
3575 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3577 {
3578 m_mlmeScanConfirmCallback(confirmParams);
3579 }
3580 NS_LOG_ERROR("Scan Type currently not supported");
3581 return;
3582 }
3583 }
3584 else
3585 {
3587 {
3588 MlmeScanConfirmParams confirmParams;
3589 confirmParams.m_scanType = m_scanParams.m_scanType;
3590 confirmParams.m_chPage = m_scanParams.m_chPage;
3591 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3592 m_mlmeScanConfirmCallback(confirmParams);
3593 }
3594 NS_LOG_ERROR("Channel " << m_channelScanIndex
3595 << " could not be set in the current page");
3596 }
3597 }
3599 {
3601 {
3603 pibAttr->phyCurrentChannel = m_startParams.m_logCh;
3604 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
3605 }
3606 else
3607 {
3609 {
3610 MlmeStartConfirmParams confirmParams;
3611 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3612 m_mlmeStartConfirmCallback(confirmParams);
3613 }
3614 NS_LOG_ERROR("Invalid page parameter in MLME-start");
3615 }
3616 }
3619 {
3621 {
3623 }
3624 else
3625 {
3627 {
3628 MlmeStartConfirmParams confirmParams;
3629 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3630 m_mlmeStartConfirmCallback(confirmParams);
3631 }
3632 NS_LOG_ERROR("Invalid channel parameter in MLME-start");
3633 }
3634 }
3636 {
3638 {
3640 pibAttr->phyCurrentChannel = m_associateParams.m_chNum;
3641 m_phy->PlmeSetAttributeRequest(PhyPibAttributeIdentifier::phyCurrentChannel, pibAttr);
3642 }
3643 else
3644 {
3645 m_macPanId = 0xffff;
3647 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3650 m_csmaCa->SetUnSlottedCsmaCa();
3653
3655 {
3656 MlmeAssociateConfirmParams confirmParams;
3657 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
3658 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3659 m_mlmeAssociateConfirmCallback(confirmParams);
3660 }
3661 NS_LOG_ERROR("Invalid page parameter in MLME-associate");
3662 }
3663 }
3666 {
3668 {
3670 }
3671 else
3672 {
3673 m_macPanId = 0xffff;
3675 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3678 m_csmaCa->SetUnSlottedCsmaCa();
3681
3683 {
3684 MlmeAssociateConfirmParams confirmParams;
3685 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
3686 confirmParams.m_status = MacStatus::INVALID_PARAMETER;
3687 m_mlmeAssociateConfirmCallback(confirmParams);
3688 }
3689 NS_LOG_ERROR("Invalid channel parameter in MLME-associate");
3690 }
3691 }
3692 else
3693 {
3695 {
3696 MlmeSetConfirmParams confirmParams;
3698 {
3699 confirmParams.m_status = MacStatus::SUCCESS;
3700 }
3701 else
3702 {
3704 }
3705
3707 {
3709 }
3711 {
3713 }
3714
3715 m_mlmeSetConfirmCallback(confirmParams);
3716 }
3717 }
3718}
3719
3720void
3722{
3723 NS_LOG_FUNCTION(this << "mac state = " << macState);
3724
3725 if (macState == MAC_IDLE)
3726 {
3729 {
3730 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3731 }
3732 else
3733 {
3734 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TRX_OFF);
3735 }
3736 }
3737 else if (macState == MAC_ACK_PENDING)
3738 {
3740 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3741 }
3742 else if (macState == MAC_CSMA)
3743 {
3746 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3747 }
3748 else if (m_macState == MAC_CSMA && macState == CHANNEL_IDLE)
3749 {
3750 // Channel is idle, set transmitter to TX_ON
3752 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
3753 }
3754 else if (m_macState == MAC_CSMA && macState == CHANNEL_ACCESS_FAILURE)
3755 {
3757
3758 // Cannot find a clear channel, drop the current packet
3759 // and send the proper confirm/indication according to the packet type
3760 NS_LOG_DEBUG(this << " cannot find clear channel");
3761
3763
3764 Ptr<Packet> pkt = m_txPkt->Copy();
3765 LrWpanMacHeader macHdr;
3766 pkt->RemoveHeader(macHdr);
3767
3768 if (macHdr.IsCommand())
3769 {
3770 CommandPayloadHeader cmdPayload;
3771 pkt->RemoveHeader(cmdPayload);
3772
3773 switch (cmdPayload.GetCommandFrameType())
3774 {
3776 m_macPanId = 0xffff;
3778 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3781 m_csmaCa->SetUnSlottedCsmaCa();
3784
3786 {
3787 MlmeAssociateConfirmParams confirmParams;
3788 confirmParams.m_assocShortAddr = Mac16Address("FF:FF");
3790 m_mlmeAssociateConfirmCallback(confirmParams);
3791 }
3792 break;
3793 }
3796 {
3797 MlmeCommStatusIndicationParams commStatusParams;
3798 commStatusParams.m_panId = m_macPanId;
3799 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
3800 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
3801 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
3802 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
3804 m_mlmeCommStatusIndicationCallback(commStatusParams);
3805 }
3807 break;
3808 }
3810 m_macPanId = 0xffff;
3812 m_macCoordExtendedAddress = Mac64Address("ff:ff:ff:ff:ff:ff:ff:ed");
3815 m_csmaCa->SetUnSlottedCsmaCa();
3818
3820 {
3821 MlmePollConfirmParams pollConfirmParams;
3822 pollConfirmParams.m_status = MacStatus::CHANNEL_ACCESS_FAILURE;
3823 m_mlmePollConfirmCallback(pollConfirmParams);
3824 }
3825 break;
3826 }
3829 {
3830 MlmeCommStatusIndicationParams commStatusParams;
3831 commStatusParams.m_panId = m_macPanId;
3832 commStatusParams.m_srcAddrMode = LrWpanMacHeader::EXTADDR;
3833 commStatusParams.m_srcExtAddr = macHdr.GetExtSrcAddr();
3834 commStatusParams.m_dstAddrMode = LrWpanMacHeader::EXTADDR;
3835 commStatusParams.m_dstExtAddr = macHdr.GetExtDstAddr();
3837 m_mlmeCommStatusIndicationCallback(commStatusParams);
3838 }
3839 break;
3840 }
3843 {
3844 m_unscannedChannels.emplace_back(m_phy->GetCurrentChannelNum());
3845 }
3846 // TODO: Handle orphan notification command during a
3847 // channel access failure when not is not scanning.
3848 break;
3849 }
3851 if (m_scanEvent.IsPending())
3852 {
3853 m_unscannedChannels.emplace_back(m_phy->GetCurrentChannelNum());
3854 }
3855 // TODO: Handle beacon request command during a
3856 // channel access failure when not scanning.
3857 break;
3858 }
3859 default: {
3860 // TODO: Other commands(e.g. Disassociation notification, etc)
3861 break;
3862 }
3863 }
3865 }
3866 else if (macHdr.IsData())
3867 {
3869 {
3870 McpsDataConfirmParams confirmParams;
3871 confirmParams.m_msduHandle = m_txQueue.front()->txQMsduHandle;
3873 m_mcpsDataConfirmCallback(confirmParams);
3874 }
3875 // remove the copy of the packet that was just sent
3877 }
3878 else
3879 {
3880 // TODO:: specify behavior for other packets
3881 m_txPkt = nullptr;
3882 m_retransmission = 0;
3883 m_numCsmacaRetry = 0;
3884 }
3885
3888 {
3889 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
3890 }
3891 else
3892 {
3893 m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TRX_OFF);
3894 }
3895 }
3896 else if (m_macState == MAC_CSMA && macState == MAC_CSMA_DEFERRED)
3897 {
3899 m_txPkt = nullptr;
3900 // The MAC is running on beacon mode and the current packet could not be sent in the
3901 // current CAP. The packet will be send on the next CAP after receiving the beacon.
3902 // The PHY state does not change from its current form. The PHY change (RX_ON) will be
3903 // triggered by the scheduled beacon event.
3904
3905 NS_LOG_DEBUG("****** PACKET DEFERRED to the next superframe *****");
3906 }
3907}
3908
3909void
3914
3915void
3920
3921uint16_t
3923{
3924 return m_macPanId;
3925}
3926
3932
3938
3939void
3940LrWpanMac::SetPanId(uint16_t panId)
3941{
3942 m_macPanId = panId;
3943}
3944
3945void
3947{
3948 NS_LOG_LOGIC(this << " change lrwpan mac state from " << m_macState << " to " << newState);
3949 m_macStateLogger(m_macState, newState);
3950 m_macState = newState;
3951}
3952
3953uint64_t
3955{
3956 return lrwpan::aUnitBackoffPeriod + lrwpan::aTurnaroundTime + m_phy->GetPhySHRDuration() +
3957 ceil(6 * m_phy->GetPhySymbolsPerOctet());
3958}
3959
3960uint8_t
3965
3966void
3968{
3969 NS_LOG_DEBUG("Transmit Queue Size: " << m_txQueue.size());
3970}
3971
3972void
3974{
3975 m_macMaxFrameRetries = retries;
3976}
3977
3978bool
3980{
3982 LrWpanMacHeader macHdr;
3983 m_txPkt->PeekHeader(macHdr);
3984
3985 if (m_coor)
3986 {
3987 // The device is its coordinator and the packet is not to itself
3988 return false;
3989 }
3990 else if (m_macCoordShortAddress == macHdr.GetShortDstAddr() ||
3992 {
3993 return true;
3994 }
3995 else
3996 {
3997 NS_LOG_DEBUG("ERROR: Packet not for the coordinator!");
3998 return false;
3999 }
4000}
4001
4004{
4006
4008 {
4009 return m_macSIFSPeriod;
4010 }
4011 else
4012 {
4013 return m_macLIFSPeriod;
4014 }
4015}
4016
4017void
4022
4023void
4028
4029uint64_t
4031{
4033 // Sync Header (SHR) + 8 bits PHY header (PHR) + PSDU
4034 return (m_phy->GetPhySHRDuration() + 1 * m_phy->GetPhySymbolsPerOctet() +
4035 (m_txPkt->GetSize() * m_phy->GetPhySymbolsPerOctet()));
4036}
4037
4038bool
4040{
4042 LrWpanMacHeader macHdr;
4043 m_txPkt->PeekHeader(macHdr);
4044
4045 return macHdr.IsAckReq();
4046}
4047
4048} // namespace lrwpan
4049} // 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.
void PrintPacket(Ptr< Packet > p)
Display the MAC header contents of a successfully received packet when logs are active.
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.
void ReceiveData(uint8_t lqi, Ptr< Packet > p)
Used to process the reception of data.
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.
void ReceiveInPromiscuousMode(uint8_t lqi, Ptr< Packet > p)
Process a frame when promiscuous mode is active.
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).
void ReceiveCommand(uint8_t lqi, Ptr< Packet > p)
Used to process the reception of a command packet.
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:439
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