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
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE ("UanMacAloha");
33
34NS_OBJECT_ENSURE_REGISTERED (UanMacAloha);
35
37 : UanMac (),
38 m_cleared (false)
39{
40}
41
43{
44}
45
46void
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
61void
63{
64 Clear ();
66}
67
70{
71 static TypeId tid = TypeId ("ns3::UanMacAloha")
72 .SetParent<UanMac> ()
73 .SetGroupName ("Uan")
74 .AddConstructor<UanMacAloha> ()
75 ;
76 return tid;
77}
78
79bool
80UanMacAloha::Enqueue (Ptr<Packet> packet, uint16_t protocolNumber, const Address &dest)
81{
82 NS_LOG_DEBUG ("" << Now ().As (Time::S) << " 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
103void
105{
106 m_forUpCb = cb;
107}
108void
110{
111 m_phy = phy;
112 m_phy->SetReceiveOkCallback (MakeCallback (&UanMacAloha::RxPacketGood, this));
113 m_phy->SetReceiveErrorCallback (MakeCallback (&UanMacAloha::RxPacketError, this));
114
115}
116void
117UanMacAloha::RxPacketGood (Ptr<Packet> pkt, [[maybe_unused]] double sinr, UanTxMode txMode)
118{
119 UanHeaderCommon header;
120 pkt->RemoveHeader (header);
121 NS_LOG_DEBUG ("Receiving packet from " << header.GetSrc () << " For " << header.GetDest ());
122
123 if (header.GetDest () == GetAddress () || header.GetDest () == Mac8Address::GetBroadcast ())
124 {
125 m_forUpCb (pkt, header.GetProtocolNumber (), header.GetSrc ());
126 }
127
128}
129
130void
132{
133 NS_LOG_DEBUG ("" << Simulator::Now () << " MAC " << Mac8Address::ConvertFrom (GetAddress ()) << " Received packet in error with sinr " << sinr);
134}
135
136int64_t
138{
139 NS_LOG_FUNCTION (this << stream);
140 return 0;
141}
142
143}
a polymophic address class
Definition: address.h:91
Callback template class.
Definition: callback.h:1279
A class used for addressing MAC8 MAC's.
Definition: mac8-address.h:43
static Mac8Address GetBroadcast(void)
Get the broadcast address (255).
Definition: mac8-address.cc:87
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:54
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
@ S
second
Definition: nstime.h:114
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Common packet header fields.
void SetSrc(Mac8Address src)
Set the source address.
Mac8Address GetSrc(void) const
Get the source address.
Mac8Address GetDest(void) const
Get the destination address.
void SetProtocolNumber(uint16_t protocolNumber)
Set the packet type.
void SetDest(Mac8Address dest)
Set the destination address.
void SetType(uint8_t type)
Set the header type.
uint16_t GetProtocolNumber(void) const
Get the packet type value.
ALOHA MAC Protocol, the simplest MAC protocol for wireless networks.
Definition: uan-mac-aloha.h:44
virtual ~UanMacAloha()
Dummy destructor, see DoDispose.
Ptr< UanPhy > m_phy
PHY layer attached to this MAC.
Definition: uan-mac-aloha.h:66
virtual void SetForwardUpCb(Callback< void, Ptr< Packet >, uint16_t, const Mac8Address & > cb)
Set the callback to forward packets up to higher layers.
bool m_cleared
Flag when we've been cleared.
Definition: uan-mac-aloha.h:70
virtual void Clear(void)
Clears all pointer references.
virtual bool Enqueue(Ptr< Packet > pkt, uint16_t protocolNumber, const Address &dest)
Enqueue packet to be transmitted.
virtual void AttachPhy(Ptr< UanPhy > phy)
Attach PHY layer to this MAC.
UanMacAloha()
Default constructor.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
void RxPacketGood(Ptr< Packet > pkt, double sinr, UanTxMode txMode)
Receive packet from lower layer (passed to PHY as callback).
virtual void DoDispose()
Destructor implementation.
Callback< void, Ptr< Packet >, uint16_t, const Mac8Address & > m_forUpCb
Forwarding up callback.
Definition: uan-mac-aloha.h:68
static TypeId GetTypeId(void)
Register this type.
void RxPacketError(Ptr< Packet > pkt, double sinr)
Packet received at lower layer in error.
Virtual base class for all UAN MAC protocols.
Definition: uan-mac.h:50
virtual Address GetAddress(void)
Get the MAC Address.
Definition: uan-mac.cc:55
uint32_t GetTxModeIndex()
Get the Tx mode index (Modulation type).
Definition: uan-mac.cc:49
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:42
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
phy
Definition: third.py:93