A Discrete-Event Network Simulator
API
uan-mac-aloha.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #include "uan-mac-aloha.h"
22 #include "uan-tx-mode.h"
23 #include "ns3/log.h"
24 #include "uan-phy.h"
25 #include "uan-header-common.h"
26 
27 #include <iostream>
28 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE ("UanMacAloha");
33 
34 NS_OBJECT_ENSURE_REGISTERED (UanMacAloha);
35 
37  : UanMac (),
38  m_cleared (false)
39 {
40 }
41 
43 {
44 }
45 
46 void
48 {
49  if (m_cleared)
50  {
51  return;
52  }
53  m_cleared = true;
54  if (m_phy)
55  {
56  m_phy->Clear ();
57  m_phy = 0;
58  }
59 }
60 
61 void
63 {
64  Clear ();
66 }
67 
68 TypeId
70 {
71  static TypeId tid = TypeId ("ns3::UanMacAloha")
72  .SetParent<UanMac> ()
73  .SetGroupName ("Uan")
74  .AddConstructor<UanMacAloha> ()
75  ;
76  return tid;
77 }
78 
79 bool
80 UanMacAloha::Enqueue (Ptr<Packet> packet, uint16_t protocolNumber, const Address &dest)
81 {
82  NS_LOG_DEBUG ("" << Simulator::Now ().GetSeconds () << " MAC " << Mac8Address::ConvertFrom (GetAddress ()) << " Queueing packet for " << Mac8Address::ConvertFrom (dest));
83 
84  if (!m_phy->IsStateTx ())
85  {
88 
89  UanHeaderCommon header;
90  header.SetSrc (src);
91  header.SetDest (udest);
92  header.SetType (0);
93  header.SetProtocolNumber (protocolNumber);
94 
95  packet->AddHeader (header);
96  m_phy->SendPacket (packet, GetTxModeIndex ());
97  return true;
98  }
99  else
100  return false;
101 }
102 
103 void
105 {
106  m_forUpCb = cb;
107 }
108 void
110 {
111  m_phy = phy;
112  m_phy->SetReceiveOkCallback (MakeCallback (&UanMacAloha::RxPacketGood, this));
113  m_phy->SetReceiveErrorCallback (MakeCallback (&UanMacAloha::RxPacketError, this));
114 
115 }
116 void
118 {
119  NS_UNUSED (sinr);
120  UanHeaderCommon header;
121  pkt->RemoveHeader (header);
122  NS_LOG_DEBUG ("Receiving packet from " << header.GetSrc () << " For " << header.GetDest ());
123 
124  if (header.GetDest () == GetAddress () || header.GetDest () == Mac8Address::GetBroadcast ())
125  {
126  m_forUpCb (pkt, header.GetProtocolNumber (), header.GetSrc ());
127  }
128 
129 }
130 
131 void
133 {
134  NS_LOG_DEBUG ("" << Simulator::Now () << " MAC " << Mac8Address::ConvertFrom (GetAddress ()) << " Received packet in error with sinr " << sinr);
135 }
136 
137 int64_t
139 {
140  NS_LOG_FUNCTION (this << stream);
141  return 0;
142 }
143 
144 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
Ptr< UanPhy > m_phy
PHY layer attached to this MAC.
Definition: uan-mac-aloha.h:68
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Callback template class.
Definition: callback.h:1176
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
void SetSrc(Mac8Address src)
Set the source address.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_UNUSED(x)
Mark a local variable as unused.
Definition: unused.h:36
static Mac8Address GetBroadcast(void)
Get the broadcast address (255).
Definition: mac8-address.cc:87
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
a polymophic address class
Definition: address.h:90
phy
Definition: third.py:86
virtual bool Enqueue(Ptr< Packet > pkt, uint16_t protocolNumber, const Address &dest)
Enqueue packet to be transmitted.
Virtual base class for all UAN MAC protocols.
Definition: uan-mac.h:49
void SetProtocolNumber(uint16_t protocolNumber)
Set the packet type.
Mac8Address GetDest(void) const
Get the destination address.
virtual Address GetAddress(void)
Get the MAC Address.
Definition: uan-mac.cc:55
void RxPacketGood(Ptr< Packet > pkt, double sinr, UanTxMode txMode)
Receive packet from lower layer (passed to PHY as callback).
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:41
A class used for addressing MAC8 MAC&#39;s.
Definition: mac8-address.h:42
Callback< void, Ptr< Packet >, uint16_t, const Mac8Address &> m_forUpCb
Forwarding up callback.
Definition: uan-mac-aloha.h:70
static TypeId GetTypeId(void)
Register this type.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void RxPacketError(Ptr< Packet > pkt, double sinr)
Packet received at lower layer in error.
virtual ~UanMacAloha()
Dummy destructor, see DoDispose.
Mac8Address GetSrc(void) const
Get the source address.
ALOHA MAC Protocol, the simplest MAC protocol for wireless networks.
Definition: uan-mac-aloha.h:43
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Common packet header fields.
virtual void Clear(void)
Clears all pointer references.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
bool m_cleared
Flag when we&#39;ve been cleared.
Definition: uan-mac-aloha.h:72
virtual void AttachPhy(Ptr< UanPhy > phy)
Attach PHY layer to this MAC.
void SetDest(Mac8Address dest)
Set the destination address.
UanMacAloha()
Default constructor.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
uint16_t GetProtocolNumber(void) const
Get the packet type value.
void SetType(uint8_t type)
Set the header type.
uint32_t GetTxModeIndex()
Definition: uan-mac.cc:49
virtual void DoDispose()
Destructor implementation.
virtual void SetForwardUpCb(Callback< void, Ptr< Packet >, uint16_t, const Mac8Address &> cb)
Set the callback to forward packets up to higher layers.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:54
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256