DESERT 3.5.1
Loading...
Searching...
No Matches
uwjammer.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2024 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#include "uwjammer.h"
40#include "mac.h"
41#include "uwjammer_cmn_hdr.h"
42#include <iostream>
43
44const std::map<Uwjammer::JammerStatus, std::string> Uwjammer::status_info{
45 {JammerStatus::IDLE, "Idle State"},
46 {JammerStatus::BUSY, "PHY is transmitting"}};
47
51static class UwjammerModule_Class : public TclClass
52{
53public:
58 : TclClass("Module/UW/JAMMER")
59 {
60 }
61
66 TclObject *
67 create(int, const char *const *)
68 {
69 return (new Uwjammer());
70 }
72
74 : buffer_data_pkts(0)
75 , n_jam_sent(0)
76 , n_jam_discarded(0)
77 , n_data_discarded(0)
78 , curr_data_pkt(0)
79 , Q_data()
80{
81 bind("buffer_data_pkts_", (int *) &buffer_data_pkts);
82
85 else if (buffer_data_pkts <= 0)
87
89}
90
91int
92Uwjammer::command(int argc, const char *const *argv)
93{
94 Tcl &tcl = Tcl::instance();
95 if (argc == 2) {
96 if (strcasecmp(argv[1], "getDataQueueSize") == 0) {
97 tcl.resultf("%d", Q_data.size());
98
99 return TCL_OK;
100 } else if (strcasecmp(argv[1], "getJamSent") == 0) {
101 tcl.resultf("%d", getJamSent());
102
103 return TCL_OK;
104 } else if (strcasecmp(argv[1], "getJamDiscarded") == 0) {
105 tcl.resultf("%d", getJamDiscarded());
106
107 return TCL_OK;
108 } else if (strcasecmp(argv[1], "getDataDiscarded") == 0) {
109 tcl.resultf("%d", getDataDiscarded());
110
111 return TCL_OK;
112 }
113
114 } else if (argc == 3) {
115 if (strcasecmp(argv[1], "setMacAddr") == 0) {
116 const char *str = argv[2];
117 char *end_ptr{};
118 int mac_addr = std::strtol(str, &end_ptr, 10);
119
120 if (str != end_ptr) {
121 addr = mac_addr;
122
123 return TCL_OK;
124 }
125
126 return TCL_ERROR;
127 }
128 }
129
130 return MMac::command(argc, argv);
131}
132
133int
135{
136 switch (m->type()) {
137 default:
138 return MMac::crLayCommand(m);
139 }
140}
141
142void
144{
145 if ((int) Q_data.size() < buffer_data_pkts) {
146 if (debug_)
147 std::cout << NOW << "Uwjammer(" << addr << ")::RECV_FROM_U_LAYERS_"
148 << std::endl;
149
150 Q_data.push(p);
151
153 txJam();
154
155 } else {
156 if (debug_)
157 std::cout << NOW << "Uwjammer(" << addr << ")::DROP_FULL_QUEUE"
158 << std::endl;
159
161
163 }
164}
165
166void
168{
169 curr_data_pkt = (Q_data.front())->copy();
170 Q_data.pop();
171
172 hdr_cmn *ch = HDR_CMN(curr_data_pkt);
173 hdr_mac *mach = HDR_MAC(curr_data_pkt);
174 hdr_JAMMER *jammerhdr = HDR_JAMMER(curr_data_pkt);
175
176 if (Q_data.size() > 0) {
177 ch->ptype() = PT_JAMMER;
178 ch->size() += sizeof(hdr_JAMMER);
179 }
180
181 jammerhdr->id_node() = node_id;
182 mach->macSA() = addr;
183
184 n_jam_sent++;
185
187}
188
189void
191{
192 if (debug_)
193 std::cout << NOW << "Uwjammer(" << addr << ")::BUSY_STATE" << std::endl;
194
196
197 MMac::Mac2PhyStartTx(p);
198}
199
200void
202{
203 MMac::Phy2MacEndTx(p);
204
205 if (debug_)
206 std::cout << NOW << "Uwjammer(" << addr
207 << ")::JAM_TRANSMITTED_AND_IDLE_STATE" << std::endl;
208
210 stateIdle();
211}
212
213void
215{
216 if ((int) Q_data.size() > 0)
217 txJam();
218}
219
220void
222{
223 if (debug_)
224 std::cout << NOW << "Uwjammer(" << addr
225 << ")::DROP_DATA_PACKET_RECEIVED" << std::endl;
226
228
230}
Class that represents the binding with the tcl configuration script.
Definition uwjammer.cpp:52
UwjammerModule_Class()
Constructor of the class.
Definition uwjammer.cpp:57
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
Definition uwjammer.cpp:67
Class that describes a Uwjammer module.
Definition uwjammer.h:55
int getJamSent() const
Returns the number of packets sent during the simulation.
Definition uwjammer.h:140
int getJamDiscarded() const
Returns the number of packets discarded during the simulation because the buffer is full.
Definition uwjammer.h:147
size_t n_data_discarded
Number of packets received and discarded.
Definition uwjammer.h:168
JammerStatus curr_state
Current state of the protocol.
Definition uwjammer.h:173
virtual void txJam()
Transmits the jam packet (calling Mac2PhyStartTx) and increment the counter of transmitted jam packet...
Definition uwjammer.cpp:167
size_t n_jam_sent
Number of packets sent.
Definition uwjammer.h:166
int buffer_data_pkts
Size of the buffer in number of packets.
Definition uwjammer.h:163
std::queue< Packet * > Q_data
Queue of DATA in number of packets.
Definition uwjammer.h:171
int getDataDiscarded() const
Returns the number of packets received and discarded during the simulation.
Definition uwjammer.h:154
virtual void Phy2MacEndRx(Packet *p) override
Method called when the Phy Layer finish to receive a Packet.
Definition uwjammer.cpp:221
virtual int crLayCommand(ClMessage *m) override
Cross-Layer messages interpreter.
Definition uwjammer.cpp:134
uint node_id
Unique Node ID.
Definition uwjammer.h:164
virtual void stateIdle()
IDLE state, check if there is at least one more packet to transmit.
Definition uwjammer.cpp:214
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
Definition uwjammer.cpp:92
static constexpr const int MAX_BUFFER_SIZE
Maximum size of the queue in number of packets.
Definition uwjammer.h:176
size_t n_jam_discarded
Number of packets discarded because the buffer is full.
Definition uwjammer.h:167
virtual void recvFromUpperLayers(Packet *p) override
Receives the packet from the upper layer (e.g.
Definition uwjammer.cpp:143
Packet * curr_data_pkt
Pointer to the current DATA packet.
Definition uwjammer.h:170
static const std::map< JammerStatus, std::string > status_info
Textual info of the state.
Definition uwjammer.h:44
virtual void Mac2PhyStartTx(Packet *p)
Pass the packet to the PHY layer.
Definition uwjammer.cpp:190
Uwjammer()
Constructor of the class.
Definition uwjammer.cpp:73
virtual void Phy2MacEndTx(const Packet *p) override
Method called when the PHY layer finish to transmit the packet.
Definition uwjammer.cpp:201
virtual void refreshState(JammerStatus state)
Refresh the state of the protocol.
Definition uwjammer.h:161
Header of the JAMMER message.
uint & id_node()
Reference to id_node variable.
UwjammerModule_Class class_module_uwjammer
Provides the description of Uwjammer Class.
#define UWJAMMER_DROP_REASON_JAMMER_PROTOCOL
Protocol rules.
Definition uwjammer.h:48
#define UWJAMMER_DROP_REASON_BUFFER_FULL
Buffer is full.
Definition uwjammer.h:46
Header of a jammer packet.
#define HDR_JAMMER(p)
alias defined to access the JAMMER HEADER