DESERT 3.5.1
Loading...
Searching...
No Matches
uw-csma-aloha-trigger-sink.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
39
43static class UwCsmaAloha_Trigger_SINKModuleClass : public TclClass
44{
45public:
50 : TclClass("Module/UW/CSMA_ALOHA/TRIGGER/SINK")
51 {
52 }
53
58 TclObject *
59 create(int, const char *const *)
60 {
61 return (new UwCsmaAloha_Trigger_SINK());
62 }
64
65void
67{
69 if (module->curr_state != UW_CS_ALOHA_TRIG_SINK_STATE_TX_TRIGGER) {
70 module->stateDisableRx();
71 }
72}
73
78 , receive_timer(this)
81{
82 bind("debug_", (double *) &debug_);
83 bind("TRIGGER_size_", (int *) &TRIGGER_size);
84 bind("tx_timer_duration_", (double *) &tx_timer_duration);
85}
86
90
91int
92UwCsmaAloha_Trigger_SINK::command(int argc, const char *const *argv)
93{
94 Tcl &tcl = Tcl::instance();
95 if (argc == 2) {
96 if (strcasecmp(argv[1], "sinkRun") == 0) {
97 stateIdle();
98 return TCL_OK;
99 } else if (strcasecmp(argv[1], "getNTriggerSent") == 0) {
100 tcl.resultf("%d", getTriggerMsgSent());
101 return TCL_OK;
102 }
103 }
104 return MMac::command(argc, argv);
105}
106
107int
109{
110 switch (m->type()) {
111 default:
112 return Module::crLayCommand(m);
113 }
114}
115
116void
118{
119 // no receive data pkt from upper layers in SINK mode
120}
121
122void
124{
126 if (debug_)
127 cout << NOW << "UwCsmaAloha_Trigger_SINK(" << addr
128 << ") ----> Enabling Receive() timer_duration = "
129 << tx_timer_duration << endl;
132}
133
134void
136{
138 if (debug_)
139 cout << NOW << "UwCsmaAloha_Trigger_SINK(" << addr
140 << ")----> Timer fired ----> Disabling Receving Data" << endl;
142 stateIdle();
143}
144
145void
146UwCsmaAloha_Trigger_SINK::initPkt(Packet *p, int dest_addr)
147{
148 hdr_cmn *ch = hdr_cmn::access(p);
149 hdr_mac *mach = HDR_MAC(p);
150 ch->ptype() = PT_MMAC_TRIGGER;
151 ch->size() = TRIGGER_size;
152 mach->macSA() = addr;
153 mach->macDA() = dest_addr;
154}
155
156void
158{
159 MMac::Mac2PhyStartTx(p);
160}
161
162void
164{
165 if (debug_)
166 cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
167 << ")::Phy2MacEndTx() end tx TRIGGER packet" << endl;
170}
171
172void
174{
177 if (debug_)
178 cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
179 << ")::Phy2MacStartRx() rx Packet " << endl;
180 } else {
181 if (debug_)
182 cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
183 << ")::Phy2MacStartRx() Receiving disabled" << endl;
184 }
185}
186
187void
189{
191 hdr_cmn *ch = HDR_CMN(p);
192 packet_t rx_pkt_type = ch->ptype();
193 hdr_mac *mach = HDR_MAC(p);
194 hdr_MPhy *ph = HDR_MPHY(p);
195
196 int dest_mac = mach->macDA();
197
198 if (ch->error()) {
199 if (debug_)
200 cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
201 << ")::Phy2MacEndRx() dropping corrupted pkt " << endl;
202 incrErrorPktsRx();
206 } else {
207 if (dest_mac == addr || dest_mac == (int) MAC_BROADCAST) {
208 if (rx_pkt_type != PT_MMAC_TRIGGER) {
210 stateRxData(p);
211 } else {
212 if (debug_)
213 cout << " UwCsmaAloha_Trigger_SINK(" << addr
214 << "):: Received a packet of wrong type " << endl;
216 }
217 } else {
220 }
221 }
222 } else {
223 if (debug_)
224 cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
225 << "):: Phy2MacEndRx ---> Not enabled to receive data "
226 << endl;
228 }
229}
230
231void
233{
235 if (debug_)
236 cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
237 << ")::stateRxData() " << endl;
238 incrDataPktsRx();
239 sendUp(data_pkt);
240}
241
242void
244{
245 if (debug_)
246 cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
247 << ")::stateRxPacketNotForMe() pkt for another address. Dropping "
248 "pkt"
249 << endl;
250 if (p != NULL) {
252 }
253}
254
255void
257{
258 receive_timer.force_cancel();
260 if (debug_)
261 std::cout << NOW << " UwCsmaAloha_Trigger_SINK(" << addr
262 << ")::stateIdle() --> TRANSMITTING TRIGGER" << endl;
264}
265
266void
268{
269 if (debug_)
270 cout << NOW << "UwCsmaAloha_Trigger_SINK(" << addr
271 << ")--->stateTxTRIGGER()" << endl;
273 Packet *p = Packet::alloc();
274 initPkt(p, MAC_BROADCAST);
275 txTRIGGER(p);
276}
277
278void
280{
283 if (debug_)
284 cout << NOW << "UwCsmaAloha_Trigger_SINK(" << addr
285 << ")---> Transmitting TRIGGER" << endl;
288 } else {
289 if (debug_)
290 cout << NOW << "UwCsmaAloha_Trigger_SINK(" << addr
291 << ")----> Not transmitting TRIGGER, receiving active" << endl;
292 Packet::free(p);
293 }
294}
295
296void
298{
299 std::string response;
300 std::cout << "Press Enter to continue";
301 std::getline(std::cin, response);
302}
Class that represents the binding with the tcl configuration script.
UwCsmaAloha_Trigger_SINKModuleClass()
Constructor of the class.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
UwCsmaAloha_Trigger_SINK *UW_CS_ALOHA_TRIG_SINK_TIMER_STATUS timer_status
< Pointer to an object of type CsmaAloha
virtual void expire(Event *e)
Method called when the timer expire.
Class that describes a UwCsmaAloha_Trigger_SINK module.
virtual void Phy2MacEndTx(const Packet *p)
Method called when the PHY layers finishes to transmit packet.
UW_CS_ALOHA_TRIG_SINK_STATUS curr_state
Current state of the protocol.
void refreshReason(UW_CS_ALOHA_TRIG_SINK_REASON_STATUS reason)
Refresh the reason for the change of state.
virtual void recvFromUpperLayers(Packet *p)
Receives the packet from the upper layer (e.g.
int TRIGGER_size
Size of the TRIGGER packet.
virtual void Phy2MacStartRx(const Packet *p)
Method called when the PHY layer begins to receive a packet.
bool receiving_state_active
True if the sink is allowed to receive data packet.
void incrTRIGGERPacketTx()
Increment the number of TRIGGER packets transmitted.
int getTriggerMsgSent()
Return the number of TRIGGER packets sent over the simulation.
ReceiveTimer receive_timer
timer of receive state
double tx_timer_duration
Duration of the timer in which one node is allowed to transmit.
virtual void stateRxData(Packet *p)
The sink receives a DATA packet from a node.
virtual void initPkt(Packet *p, int dest_addr=0)
Init the headers of the packet in transmission.
virtual void stateRxPacketNotForMe(Packet *p)
The destination address of the packet is not equal to the sink's address.
virtual void txTRIGGER(Packet *p)
Transmit the TRIGGER packet.
virtual void waitForUser()
Used for debug purposes.
virtual void stateTxTRIGGER()
The sink transmit the TRIGGER packet to begin the neighbour discovery phase.
virtual void stateIdle()
State IDLE of the protocol.
int trigger_pkts_tx
Number of TRIGGER packet received.
UW_CS_ALOHA_TRIG_SINK_REASON_STATUS last_reason
Last reason of the state change of the protocol.
virtual ~UwCsmaAloha_Trigger_SINK()
Destructor of the UwCsmaAloha_Trigger_SINK class.
virtual int crLayCommand(ClMessage *m)
Cross-Layer messages interpreter.
void refreshState(UW_CS_ALOHA_TRIG_SINK_STATUS state)
Refresh the state of the protocol.
virtual void Mac2PhyStartTx(Packet *p)
Pass the packet to the PHY layer.
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
virtual void stateDisableRx()
The sink is not allowed to receive the packet from a certain node anymore.
virtual void Phy2MacEndRx(Packet *p)
Method called when the PHY layer finishes to receive a packet.
virtual void stateEnableRx()
State in which the sink is enabled to receive packets.
UwCsmaAloha_Trigger_SINK()
Constructor of the UwCsmaAloha_Trigger_SINK class.
UW_CS_ALOHA_TRIG_SINK_STATUS prev_state
Previous state of the protocol.
UwCsmaAloha_Trigger_SINKModuleClass class_module_uw_csma_aloha_trigger
Provides the declaration of UwCsmaAloha_Trigger_SINK class.
#define UW_CS_ALOHA_TRIG_SINK_DROP_REASON_ERROR
Packet corrupted.
#define UW_CS_ALOHA_TRIG_SINK_DROP_REASON_UNKNOWN_TYPE
The type of the packet is unknown.
#define UW_CS_ALOHA_TRIG_SINK_DROP_REASON_RECEIVING_NOT_ENABLED
The sink is not enabled to receive data.
#define UW_CS_ALOHA_TRIG_SINK_DROP_REASON_WRONG_RECEIVER
The packet is for another node.