|
|
|
|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2011 The Boeing Company |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 2 as |
| 7 |
* published by the Free Software Foundation; |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
* |
| 18 |
* Authors: |
| 19 |
* Gary Pei <guangyu.pei@boeing.com> |
| 20 |
* kwong yin <kwong-sang.yin@boeing.com> |
| 21 |
* Tom Henderson <thomas.r.henderson@boeing.com> |
| 22 |
* Sascha Alexander Jopen <jopen@cs.uni-bonn.de> |
| 23 |
* Erwan Livolant <erwan.livolant@inria.fr> |
| 24 |
*/ |
| 25 |
#include "lr-wpan-nullmac.h" |
| 26 |
#include "lr-wpan-csmaca.h" |
| 27 |
#include "lr-wpan-mac-header.h" |
| 28 |
#include "lr-wpan-mac-trailer.h" |
| 29 |
#include <ns3/simulator.h> |
| 30 |
#include <ns3/log.h> |
| 31 |
#include <ns3/uinteger.h> |
| 32 |
#include <ns3/node.h> |
| 33 |
#include <ns3/packet.h> |
| 34 |
#include <ns3/random-variable-stream.h> |
| 35 |
#include <ns3/double.h> |
| 36 |
|
| 37 |
#undef NS_LOG_APPEND_CONTEXT |
| 38 |
#define NS_LOG_APPEND_CONTEXT \ |
| 39 |
std::clog << "[address " << m_shortAddress << "] "; |
| 40 |
|
| 41 |
namespace ns3 { |
| 42 |
|
| 43 |
NS_LOG_COMPONENT_DEFINE ("LrWpanNullMac"); |
| 44 |
|
| 45 |
NS_OBJECT_ENSURE_REGISTERED (LrWpanNullMac); |
| 46 |
|
| 47 |
TypeId |
| 48 |
LrWpanNullMac::GetTypeId (void) |
| 49 |
{ |
| 50 |
static TypeId tid = TypeId ("ns3::LrWpanNullMac") |
| 51 |
.SetParent<LrWpanMac> () |
| 52 |
.SetGroupName ("LrWpan") |
| 53 |
.AddConstructor<LrWpanNullMac> () |
| 54 |
; |
| 55 |
return tid; |
| 56 |
} |
| 57 |
|
| 58 |
LrWpanNullMac::LrWpanNullMac () |
| 59 |
{ |
| 60 |
// First set the state to a known value, call ChangeMacState to fire trace source. |
| 61 |
m_lrWpanMacState = MAC_IDLE; |
| 62 |
ChangeMacState (MAC_IDLE); |
| 63 |
|
| 64 |
m_macRxOnWhenIdle = true; |
| 65 |
m_macPanId = 0; |
| 66 |
m_associationStatus = ASSOCIATED; |
| 67 |
m_selfExt = Mac64Address::Allocate (); |
| 68 |
m_macPromiscuousMode = false; |
| 69 |
m_macMaxFrameRetries = 3; |
| 70 |
m_retransmission = 0; |
| 71 |
m_numCsmacaRetry = 0; |
| 72 |
m_txPkt = 0; |
| 73 |
|
| 74 |
Ptr<UniformRandomVariable> uniformVar = CreateObject<UniformRandomVariable> (); |
| 75 |
uniformVar->SetAttribute ("Min", DoubleValue (0.0)); |
| 76 |
uniformVar->SetAttribute ("Max", DoubleValue (255.0)); |
| 77 |
m_macDsn = SequenceNumber8 (uniformVar->GetValue ()); |
| 78 |
m_shortAddress = Mac16Address ("00:00"); |
| 79 |
} |
| 80 |
|
| 81 |
LrWpanNullMac::~LrWpanNullMac () |
| 82 |
{ |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
LrWpanNullMac::DoInitialize () |
| 87 |
{ |
| 88 |
if (m_macRxOnWhenIdle) |
| 89 |
{ |
| 90 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON); |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF); |
| 95 |
} |
| 96 |
|
| 97 |
LrWpanMac::DoInitialize (); |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
LrWpanNullMac::DoDispose () |
| 102 |
{ |
| 103 |
LrWpanMac::DoDispose (); |
| 104 |
} |
| 105 |
|
| 106 |
bool |
| 107 |
LrWpanNullMac::GetRxOnWhenIdle () |
| 108 |
{ |
| 109 |
return m_macRxOnWhenIdle; |
| 110 |
} |
| 111 |
|
| 112 |
void |
| 113 |
LrWpanNullMac::SetRxOnWhenIdle (bool rxOnWhenIdle) |
| 114 |
{ |
| 115 |
NS_LOG_FUNCTION (this << rxOnWhenIdle); |
| 116 |
m_macRxOnWhenIdle = rxOnWhenIdle; |
| 117 |
|
| 118 |
if (m_lrWpanMacState == MAC_IDLE) |
| 119 |
{ |
| 120 |
if (m_macRxOnWhenIdle) |
| 121 |
{ |
| 122 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON); |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void |
| 132 |
LrWpanNullMac::SetLrWpanMacState (LrWpanMacState macState) |
| 133 |
{ |
| 134 |
NS_LOG_FUNCTION (this << "mac state = " << macState); |
| 135 |
|
| 136 |
McpsDataConfirmParams confirmParams; |
| 137 |
|
| 138 |
if (macState == MAC_IDLE) |
| 139 |
{ |
| 140 |
ChangeMacState (MAC_IDLE); |
| 141 |
|
| 142 |
if (m_macRxOnWhenIdle) |
| 143 |
{ |
| 144 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON); |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF); |
| 149 |
} |
| 150 |
|
| 151 |
CheckQueue (); |
| 152 |
} |
| 153 |
else if (macState == MAC_ACK_PENDING) |
| 154 |
{ |
| 155 |
ChangeMacState (MAC_ACK_PENDING); |
| 156 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON); |
| 157 |
} |
| 158 |
else if (macState == MAC_CSMA) |
| 159 |
{ |
| 160 |
NS_ASSERT (m_lrWpanMacState == MAC_IDLE || m_lrWpanMacState == MAC_ACK_PENDING); |
| 161 |
|
| 162 |
ChangeMacState (MAC_CSMA); |
| 163 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON); |
| 164 |
} |
| 165 |
else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_IDLE) |
| 166 |
{ |
| 167 |
// Channel is idle, set transmitter to TX_ON |
| 168 |
ChangeMacState (MAC_SENDING); |
| 169 |
m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON); |
| 170 |
} |
| 171 |
else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_ACCESS_FAILURE) |
| 172 |
{ |
| 173 |
NS_ASSERT (m_txPkt); |
| 174 |
|
| 175 |
// cannot find a clear channel, drop the current packet. |
| 176 |
NS_LOG_DEBUG ( this << " cannot find clear channel"); |
| 177 |
confirmParams.m_msduHandle = m_txQueue.front ()->txQMsduHandle; |
| 178 |
confirmParams.m_status = IEEE_802_15_4_CHANNEL_ACCESS_FAILURE; |
| 179 |
m_macTxDropTrace (m_txPkt); |
| 180 |
if (!m_mcpsDataConfirmCallback.IsNull ()) |
| 181 |
{ |
| 182 |
m_mcpsDataConfirmCallback (confirmParams); |
| 183 |
} |
| 184 |
// remove the copy of the packet that was just sent |
| 185 |
RemoveFirstTxQElement (); |
| 186 |
|
| 187 |
ChangeMacState (MAC_IDLE); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
} // namespace ns3 |