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