DESERT 3.5.1
Loading...
Searching...
No Matches
uw-csma-ca.h
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//
29
39#ifndef CSMA_CA_H
40#define CSMA_CA_H
41
42#include <mmac.h>
43#include <queue>
44#include <sstream>
45#include <fstream>
46#include <unistd.h>
47#include <iostream>
48#include <time.h>
49
50#include "uw-csma-ca-hdrs.h"
51
52#define DROP_REASON_SNR "SNR"
53#define DROP_REASON_NOTFORME "NFM"
54#define DROP_REASON_NOTRIGHTSTATE "NRS"
55#define DROP_REASON_GENERICERROR "ERR"
56
63
65
67
79
81
83
85
87
89
90
91
92string log_level_string[] = {"ERROR", "WARNING", "INFO", "DEBUG"};
96class CsmaCa : public MMac
97{
98public:
99 class CsmaCaTimer : public TimerHandler
100 {
101 public:
103 : TimerHandler()
104 {
105 module = m;
106 timer_type = tt;
107 }
108
109 protected:
110 virtual void expire(Event *e);
111 CsmaCa *module;
113 };
114
115 CsmaCa();
116 virtual ~CsmaCa();
127 virtual int command(int argc, const char *const *argv);
128
129protected:
135 virtual void recvFromUpperLayers(Packet *p);
141 virtual void Mac2PhyStartTx(Packet *p);
147 virtual void Phy2MacEndTx(const Packet *p);
153 virtual void Phy2MacStartRx(const Packet *p);
159 virtual void Phy2MacEndRx(Packet *p);
166 virtual void dropPacket(Packet *p, csma_ca_pkt_type_t type, char *reason);
171 inline csma_ca_states_t
173 {
174 return state;
175 }
180 inline void
182 {
184 state = s;
185 }
192 {
193 return log_level;
194 }
199 inline int
201 {
202 return data_q.size();
203 }
208 inline time_t
210 {
211 return time(NULL);
212 }
217 inline string
219 {
220 return logfile;
221 }
225 virtual void state_Wait_CTS();
229 virtual void state_Wait_Data();
233 virtual void state_Idle();
238 virtual void state_Backoff(int tx_time);
243 virtual int stateRxData(Packet *p);
250 virtual int stateRxRTS(hdr_ca_RTS *rts, int mac_src, int mac_dst);
257 virtual int stateRxCTS(hdr_ca_CTS *cts, int mac_src, int mac_dst);
264 virtual int stateRxACK(Packet *ack);
268 virtual void stateTxCTS();
272 virtual int stateTxData();
276 virtual void state_Wait_ACK();
281 virtual void stateTxAck(int mac_dst);
287 virtual void initializeLog();
288
294private:
295/* Log Macros */
296#define LOGERR(log) printonLog(CSMA_CA_ERROR, log);
297#define LOGDBG(log) printonLog(CSMA_CA_DEBUG, log);
298#define LOGWRN(log) printonLog(CSMA_CA_WARN, log);
299#define LOGINFO(log) printonLog(CSMA_CA_INFO, log);
305 void printonLog(csma_ca_log_level_t level, string log);
306
307 /* Private Methods */
311 void extractDataPacket();
315 int txData();
320 int txRTS(int mac_dest);
325 int txCTS(int mac_dest);
329 int txAck();
333 void data_timer_fired();
337 void backoff_timer_fired();
341 void cts_timer_fired();
345 void ack_timer_fired();
350 inline int
352 {
353 return (int) ((data_size * 8) / bitrate);
354 }
355
356 /* Header build Methods */
362 void buildRTShdr(hdr_ca_RTS **rts, uint8_t tx_time);
368 void buildCTShdr(hdr_ca_CTS **cts, uint8_t tx_time);
375 Packet *buildPacket(int mac_dest, csma_ca_pkt_type_t type, uint8_t tx_time);
376
377 /* config from tcl */
387 /* status variables */
391 std::queue<Packet *> data_q;
396 /* log */
397 std::ofstream outLog;
399 string logfile;
401 /* statistics*/
408};
409
410#endif /* CSMA_CA_H */
CsmaCa *csma_ca_timers_t timer_type
Definition uw-csma-ca.h:112
virtual void expire(Event *e)
CsmaCaTimer(CsmaCa *m, csma_ca_timers_t tt)
Definition uw-csma-ca.h:102
Class that describes a CsmaAloha module.
Definition uw-csma-ca.h:97
string logfile
File name of log.
Definition uw-csma-ca.h:399
int cts_wait_val
Timer duration of CTS.
Definition uw-csma-ca.h:383
int max_queue_size
Maximum dimension of Queue.
Definition uw-csma-ca.h:378
void extractDataPacket()
Extract data packet from queue.
int cts_pkt_dropped
CTS packet dropped.
Definition uw-csma-ca.h:405
Packet * buildPacket(int mac_dest, csma_ca_pkt_type_t type, uint8_t tx_time)
Build a generic packet.
virtual void state_Wait_ACK()
Wait for an ACK after a data transmission.
time_t getEpoch()
Return the system epoch.
Definition uw-csma-ca.h:209
virtual int stateTxData()
Transmission of a DATA packet.
csma_ca_states_t state
Current state of the protocol.
Definition uw-csma-ca.h:393
int n_rts_rx
RTS received.
Definition uw-csma-ca.h:402
int n_cts_rx
CTS received.
Definition uw-csma-ca.h:403
virtual int stateRxACK(Packet *ack)
Reception of an ACK packet.
int data_pkt_dropped
DATA packet dropped.
Definition uw-csma-ca.h:404
virtual void initializeLog()
Initializes the protocol at the beginning of the simulation.
int data_wait_val
Timer duration of DATA.
Definition uw-csma-ca.h:384
void ack_timer_fired()
ACK timer is expired.
int txData()
Actually transmit a DATA packet.
void data_timer_fired()
data timer is expired
virtual void Phy2MacStartRx(const Packet *p)
Method called when the Phy Layer start to receive a Packet.
CsmaCaTimer ack_timer
ACK timer.
Definition uw-csma-ca.h:292
std::queue< Packet * > data_q
Size of DATA packet.
Definition uw-csma-ca.h:391
virtual void state_Backoff(int tx_time)
Backoff state.
string getLogFile()
Return name of the log file.
Definition uw-csma-ca.h:218
int actual_mac_data_src
Source MAC of DATA packet we are handling.
Definition uw-csma-ca.h:388
virtual int stateRxCTS(hdr_ca_CTS *cts, int mac_src, int mac_dst)
Reception of an CTS packet.
virtual void state_Idle()
Protocol in IDLE state.
void buildRTShdr(hdr_ca_RTS **rts, uint8_t tx_time)
Build an RTS header.
void backoff_timer_fired()
backoff timer is expired
void buildCTShdr(hdr_ca_CTS **cts, uint8_t tx_time)
Build an CTS header.
void printonLog(csma_ca_log_level_t level, string log)
Print a message on log file.
virtual void dropPacket(Packet *p, csma_ca_pkt_type_t type, char *reason)
Drop the packet logging the reason and incrementing the counters.
virtual void state_Wait_CTS()
Waiting for CTS packet.
virtual int stateRxData(Packet *p)
Transmit a data packet.
int getQueueSize()
Return the size of the data packet queue.
Definition uw-csma-ca.h:200
virtual void stateTxCTS()
Transmission of a CTS packet.
int data_size
Size of DATA packet.
Definition uw-csma-ca.h:379
CsmaCaTimer backoff_timer
Backoff timer.
Definition uw-csma-ca.h:289
int txRTS(int mac_dest)
Actually transmit a RTS packet.
Packet * actual_data_packet
Pointer to DATA packet we are handling.
Definition uw-csma-ca.h:390
int ack_pkt_dropped
ACK packet dropped.
Definition uw-csma-ca.h:407
int backoff_max
Maximum value in range of backoff.
Definition uw-csma-ca.h:382
virtual ~CsmaCa()
virtual int stateRxRTS(hdr_ca_RTS *rts, int mac_src, int mac_dst)
Reception of an RTS packet.
int actual_expected_tx_time
Tx time of DATA packet we are handling.
Definition uw-csma-ca.h:389
csma_ca_log_level_t getLogLevel()
Return the current log level of the protocol.
Definition uw-csma-ca.h:191
virtual void stateTxAck(int mac_dst)
Tranmission of an ACK.
int computeTxTime()
Compute transmission time of a packet using known bitrate.
Definition uw-csma-ca.h:351
int ack_wait_val
Timer duration of ACK.
Definition uw-csma-ca.h:385
CsmaCaTimer data_timer
Data timer.
Definition uw-csma-ca.h:291
virtual void Mac2PhyStartTx(Packet *p)
Pass the packet to the PHY layer.
csma_ca_log_level_t log_level
Current log level chosen for protocol.
Definition uw-csma-ca.h:398
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
virtual void state_Wait_Data()
Waiting for DATA packet.
int bitrate
Bit rate adopted.
Definition uw-csma-ca.h:380
int txCTS(int mac_dest)
Actually transmit a CTS packet.
virtual void Phy2MacEndRx(Packet *p)
Method called when the Phy Layer finish to receive a Packet.
virtual void Phy2MacEndTx(const Packet *p)
Method called when the PHY layer finish to transmit the packet.
std::ofstream outLog
Stdout stream of log.
Definition uw-csma-ca.h:397
int backoff_delta
Delta value (configurable) to be added to backoff.
Definition uw-csma-ca.h:381
void cts_timer_fired()
CTS timer is expired.
csma_ca_states_t previous_state
Previous state of the protocol.
Definition uw-csma-ca.h:394
CsmaCaTimer cts_timer
CTS timer.
Definition uw-csma-ca.h:290
virtual void recvFromUpperLayers(Packet *p)
Receives the packet from the upper layer (e.g.
void updateState(csma_ca_states_t s)
Update the state of the protocol.
Definition uw-csma-ca.h:181
int txAck()
Actually transmit an ACK.
ack_modes_t ack_mode
ACK mode (configurable.
Definition uw-csma-ca.h:392
csma_ca_states_t getState()
Get the state of the protocol.
Definition uw-csma-ca.h:172
int rts_pkt_dropped
RTS packet dropped.
Definition uw-csma-ca.h:406
Provides the headers of ctrl packets of uw-csma-ca module.
CSMA_CA_PKT_TYPE
Definition uw-csma-ca.h:80
@ CSMA_CA_RTS
Definition uw-csma-ca.h:80
@ CSMA_CA_DATA
Definition uw-csma-ca.h:80
@ CSMA_CA_CTS
Definition uw-csma-ca.h:80
@ CSMA_CA_ACK
Definition uw-csma-ca.h:80
CSMA_CA_TIMERS
Definition uw-csma-ca.h:57
@ CSMA_CA_DATA_TIMER
Definition uw-csma-ca.h:58
@ CSMA_CA_CTS_TIMER
Definition uw-csma-ca.h:60
@ CSMA_CA_ACK_TIMER
Definition uw-csma-ca.h:61
@ CSMA_CA_BACKOFF_TIMER
Definition uw-csma-ca.h:59
CSMA_CA_ACK_MODES
Definition uw-csma-ca.h:64
@ CSMA_CA_NO_ACK_MODE
Definition uw-csma-ca.h:64
@ CSMA_CA_ACK_MODE
Definition uw-csma-ca.h:64
enum CSMA_CA_PKT_TYPE csma_ca_pkt_type_t
Definition uw-csma-ca.h:82
enum log_level csma_ca_log_level_t
Definition uw-csma-ca.h:88
CSMA_CA_STATES
Definition uw-csma-ca.h:68
@ CSMA_CA_BACKOFF
Definition uw-csma-ca.h:70
@ CSMA_CA_WAIT_ACK
Definition uw-csma-ca.h:76
@ CSMA_CA_TX_RTS
Definition uw-csma-ca.h:71
@ CSMA_CA_TX_ACK
Definition uw-csma-ca.h:77
@ CSMA_CA_TX_DATA
Definition uw-csma-ca.h:73
@ CSMA_CA_IDLE
Definition uw-csma-ca.h:69
@ CSMA_CA_TX_CTS
Definition uw-csma-ca.h:72
@ CSMA_CA_WAIT_DATA
Definition uw-csma-ca.h:75
@ CSMA_CA_WAIT_CTS
Definition uw-csma-ca.h:74
enum CSMA_CA_TIMERS csma_ca_timers_t
log_level
Definition uw-csma-ca.h:86
@ CSMA_CA_ERROR
Definition uw-csma-ca.h:86
@ CSMA_CA_DEBUG
Definition uw-csma-ca.h:86
@ CSMA_CA_WARN
Definition uw-csma-ca.h:86
@ CSMA_CA_INFO
Definition uw-csma-ca.h:86
enum CSMA_CA_STATES csma_ca_states_t
Definition uw-csma-ca.h:84
enum CSMA_CA_ACK_MODES ack_modes_t
Definition uw-csma-ca.h:66
string log_level_string[]
Definition uw-csma-ca.h:92