DESERT 3.5.1
Loading...
Searching...
No Matches
uw-ofdm-aloha.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#include "uw-ofdm-aloha.h"
40#include <mac.h>
41#include <cmath>
42#include <climits>
43#include <iomanip>
44#include <rng.h>
45#include "uwphy-clmsg.h"
46
47enum
48{
49 NOT_SET = -1,
51};
52
56static class UWOFDMALOHAModuleClass : public TclClass
57{
58public:
63 : TclClass("Module/UW/OFDM_ALOHA")
64 {
65 }
66
67 TclObject *
68 create(int, const char *const *)
69 {
70 return (new UWOFDMAloha());
71 }
73
75{
77 if (module->curr_state == UWOFDMALOHA_STATE_WAIT_ACK)
78 {
79
80 if (module->uwofdmaloha_debug)
81 cout << NOW << " UWOFDMAloha (" << module->addr
82 << ") timer expire() current state = "
83 << module->status_info[module->curr_state]
84 << "; ACK not received, next state = "
85 << module->status_info[UWOFDMALOHA_STATE_BACKOFF] << endl;
86
87 module->refreshReason(UWOFDMALOHA_REASON_ACK_TIMEOUT);
88 module->stateBackoff();
89 }
90 else
91 {
92 if (module->uwofdmaloha_debug)
93 cout << NOW << " UWOFDMAloha (" << module->addr
94 << ")::AckTimer::expired() " << endl;
95 }
96}
97
98void UWOFDMAloha::BackOffTimer::expire(Event *e)
99{
100 timer_status = UWOFDMALOHA_EXPIRED;
101 if (module->curr_state == UWOFDMALOHA_STATE_BACKOFF)
102 {
103
104 if (module->uwofdmaloha_debug)
105 cout << NOW << " UWOFDMAloha (" << module->addr
106 << ") timer expire() current state = "
107 << module->status_info[module->curr_state]
108 << "; backoff expired, next state = "
109 << module->status_info[UWOFDMALOHA_STATE_IDLE] << endl;
110
112 module->exitBackoff();
113 module->stateIdle();
114 }
115 else
116 {
117 if (module->uwofdmaloha_debug)
118 cout << NOW << " UWOFDMAloha (" << module->addr
119 << ")::BackOffTimer::expired() " << endl;
120 }
121}
122
123const double UWOFDMAloha::prop_speed = 1500.0;
124bool UWOFDMAloha::initialized = false;
125
126map<UWOFDMAloha::UWOFDMALOHA_STATUS, string> UWOFDMAloha::status_info;
127map<UWOFDMAloha::UWOFDMALOHA_REASON_STATUS, string> UWOFDMAloha::reason_info;
128map<UWOFDMAloha::UWOFDMALOHA_PKT_TYPE, string> UWOFDMAloha::pkt_type_info;
129
131 : ack_timer(this),
132 backoff_timer(this),
133 txsn(1),
135 curr_data_pkt(0),
137 curr_tx_rounds(0),
138 print_transitions(false),
139 has_buffer_queue(true),
145 start_tx_time(0),
146 recv_data_id(-1),
147 srtt(0),
148 sumrtt(0),
149 sumrtt2(0),
150 rttsamples(0),
151 n_receptions(0),
152 disturbanceNode(false)
153{
154 mac2phy_delay_ = 1e-19;
155
156 bind("HDR_size_", (int *)&HDR_size);
157 bind("ACK_size_", (int *)&ACK_size);
158 bind("max_tx_tries_", (int *)&max_tx_tries);
159 bind("wait_constant_", (double *)&wait_constant);
160 bind("uwofdmaloha_debug_", (int *)&uwofdmaloha_debug); // debug mode
161 bind("max_payload_", (int *)&max_payload);
162 bind("ACK_timeout_", (double *)&ACK_timeout);
163 bind("alpha_", (double *)&alpha_);
164 bind("buffer_pkts_", (int *)&buffer_pkts);
165 bind("backoff_tuner_", (double *)&backoff_tuner);
166 bind("max_backoff_counter_", (int *)&max_backoff_counter);
167 bind("MAC_addr_", (int *)&addr);
168
169 if (max_tx_tries <= 0)
170 max_tx_tries = INT_MAX;
171 if (buffer_pkts > 0)
172 has_buffer_queue = true;
173}
174
178
179// // TCL command interpreter
180
181int UWOFDMAloha::command(int argc, const char *const *argv)
182{
183 Tcl &tcl = Tcl::instance();
184 if (argc == 2)
185 {
186 if (strcasecmp(argv[1], "setAckMode") == 0)
187 {
189 return TCL_OK;
190 }
191 else if (strcasecmp(argv[1], "setNoAckMode") == 0)
192 {
194 return TCL_OK;
195 }
196 else if (strcasecmp(argv[1], "setDisturbanceNode") == 0)
197 {
198 disturbanceNode = true;
199 return TCL_OK;
200 }
201 else if (strcasecmp(argv[1], "initialize") == 0)
202 {
203 if (initialized == false)
204 initInfo();
206 fout.open("/tmp/OFDMALOHAstateTransitions.txt", ios_base::app);
207 return TCL_OK;
208 }
209 else if (strcasecmp(argv[1], "printTransitions") == 0)
210 {
211 print_transitions = true;
212 return TCL_OK;
213 } // stats functions
214 else if (strcasecmp(argv[1], "getQueueSize") == 0)
215 {
216 tcl.resultf("%d", mapPacket.size());
217 return TCL_OK;
218 }
219 else if (strcasecmp(argv[1], "getUpLayersDataRx") == 0)
220 {
221 tcl.resultf("%d", getUpLayersDataPktsRx());
222 return TCL_OK;
223 }
224 else if (strcasecmp(argv[1], "getAckPktsTx") == 0)
225 {
226 tcl.resultf("%d", getAckPktsTx());
227 return TCL_OK;
228 }
229 }
230 else if (argc == 3)
231 {
232 if (strcasecmp(argv[1], "setMacAddr") == 0)
233 {
234 addr = atoi(argv[2]);
235 if (debug_)
236 cout << "OFDM Aloha MAC address of current node is " << addr << endl;
237 return TCL_OK;
238 }
239 if (strcasecmp(argv[1], "addInvalidCarriers") == 0)
240 {
241 addInvalidCarriers(atoi(argv[2]));
242 if (debug_)
243 cout << "Carrier " << atoi(argv[2]) << " not usable " << endl;
244 return TCL_OK;
245 }
246 }
247 else if (argc == 6)
248 {
249 if (strcasecmp(argv[1], "init_macofdm_node") == 0)
250 {
251 init_macofdm_node(atoi(argv[2]), atof(argv[3]), atoi(argv[4]), argv[5]);
252 return TCL_OK;
253 }
254 }
255 return MMac::command(argc, argv);
256}
257
259{
260
261 initialized = true;
262
263 if ((print_transitions) && (system(NULL)))
264 {
265 system("rm -f /tmp/OFDMALOHAstateTransitions.txt");
266 system("touch /tmp/OFDMALOHAstateTransitions.txt");
267 }
268
269 status_info[UWOFDMALOHA_STATE_IDLE] = "Idle state";
270 status_info[UWOFDMALOHA_STATE_TX_DATA] = "Transmit DATA state";
271 status_info[UWOFDMALOHA_STATE_TX_ACK] = "Transmit ACK state";
272 status_info[UWOFDMALOHA_STATE_WAIT_ACK] = "Wait for ACK state";
273 status_info[UWOFDMALOHA_STATE_DATA_RX] = "DATA received state";
274 status_info[UWOFDMALOHA_STATE_ACK_RX] = "ACK received state";
275 status_info[UWOFDMALOHA_STATE_RX_IDLE] = "Start rx Idle state";
276 status_info[UWOFDMALOHA_STATE_RX_WAIT_ACK] = "Start rx Wait ACK state";
277 status_info[UWOFDMALOHA_STATE_CHK_ACK_TIMEOUT] = "Check Wait ACK timeout state";
278 status_info[UWOFDMALOHA_STATE_WRONG_PKT_RX] = "Wrong Pkt Rx state";
279 status_info[UWOFDMALOHA_STATE_BACKOFF] = "Backoff state";
280 status_info[UWOFDMALOHA_STATE_RX_BACKOFF] = "Start rx Backoff state";
282 "Check Backoff timeout state";
284 status_info[UWOFDMALOHA_STATE_MULTIPLE_RX_IN_PROGRESS] = "Receiving Multiple Data";
285
286 reason_info[UWOFDMALOHA_REASON_DATA_PENDING] = "DATA pending from upper layers";
287 reason_info[UWOFDMALOHA_REASON_DATA_RX] = "DATA received";
288 reason_info[UWOFDMALOHA_REASON_DATA_TX] = "DATA transmitted";
289 reason_info[UWOFDMALOHA_REASON_ACK_TX] = "ACK tranmsitted";
290 reason_info[UWOFDMALOHA_REASON_ACK_RX] = "ACK received";
292 reason_info[UWOFDMALOHA_REASON_DATA_EMPTY] = "DATA queue empty";
294 "DATA dropped due to max tx rounds";
296 reason_info[UWOFDMALOHA_REASON_PKT_NOT_FOR_ME] = "Received an erroneous pkt";
297 reason_info[UWOFDMALOHA_REASON_WAIT_ACK_PENDING] = "Wait for ACK timer pending";
298 reason_info[UWOFDMALOHA_REASON_PKT_ERROR] = "Erroneous pkt";
300 reason_info[UWOFDMALOHA_REASON_BACKOFF_PENDING] = "Backoff timer pending";
301
304 pkt_type_info[UWOFDMALOHA_DATAMAX_PKT] = "MAX payload DATA pkt";
305}
306
307// Initialize subCarriers parameters inside a node, default all carriers are used
308void UWOFDMAloha::init_macofdm_node(int subCarNum, double subCarSize, int ctrl_subCar, string modulation)
309{
310 mac_ncarriers = subCarNum;
311 ctrl_car = ctrl_subCar;
312 mac_carrierSize = subCarSize;
313
314 for (int i = 0; i < mac_ncarriers; i++)
315 {
316 mac_carVec.push_back(1);
317 mac_carMod.push_back("BPSK");
318 }
319 for (int i = 0; i < nouse_carriers.size(); i++)
320 {
322 }
323 return;
324}
325
326void UWOFDMAloha::updateRTT(double curr_rtt)
327{
328 srtt = alpha_ * srtt + (1 - alpha_) * curr_rtt;
329 sumrtt += curr_rtt;
330 sumrtt2 += curr_rtt * curr_rtt;
331 rttsamples++;
333}
334
336{
337 updateRTT(rtt);
338
340 cout << NOW << " UWOFDMAloha (" << addr
341 << ")::updateAckTimeout() curr ACK_timeout = " << ACK_timeout
342 << endl;
343}
344
349
350double
352{
353 incrTotalBackoffTimes();
354 double random = RNG::defaultrng()->uniform_double();
355
360
361 double backoff_duration =
362 backoff_tuner * random * 2.0 * ACK_timeout * pow(2.0, counter);
363
364 backoffSumDuration(backoff_duration);
365
367 {
368 cout << NOW << " UWOFDMAloha (" << addr
369 << ")::getBackoffTime() backoff time = " << backoff_duration
370 << " s" << endl;
371 }
372 return backoff_duration;
373}
374
375double
377{
378 double duration;
379 Packet *temp_data_pkt;
380 map<pktSeqNum, Packet *>::iterator it_p;
381
382 if (type == UWOFDMALOHA_DATA_PKT)
383 {
384 if (!mapPacket.empty())
385 {
386 it_p = mapPacket.begin();
387 temp_data_pkt = ((*it_p).second)->copy();
388 // temp_data_pkt = (Q.front())->copy();
389 hdr_cmn *ch = HDR_CMN(temp_data_pkt);
390 ch->size() = HDR_size + ch->size();
391 }
392 else
393 {
394 temp_data_pkt = Packet::alloc();
395 hdr_cmn *ch = HDR_CMN(temp_data_pkt);
396 ch->size() = HDR_size + max_payload;
397 }
398 }
399 else if (type == UWOFDMALOHA_ACK_PKT)
400 {
401 temp_data_pkt = Packet::alloc();
402 hdr_cmn *ch = HDR_CMN(temp_data_pkt);
403 ch->size() = ACK_size;
404 }
405 duration = Mac2PhyTxDuration(temp_data_pkt);
406 Packet::free(temp_data_pkt);
407 return (duration);
408}
409
411{
413 std::cout << NOW << " UWOFDMAloha (" << addr
414 << ")::recvFromUpperLayers()";
415 if (((has_buffer_queue == true) && (mapPacket.size() < buffer_pkts)) ||
416 (has_buffer_queue == false))
417 {
419 // Q.push(p);
420 putPktInQueue(p);
422 waitStartTime();
424 cout << NOW << " UWOFDMAloha (" << addr
425 << ")::recvFromUpperLayers() current status "
426 << status_info[curr_state] << " prev state " << status_info[prev_state]
427 << " prev prev state " << status_info[prev_prev_state] << endl;
429 cout << NOW << " UWOFDMAloha (" << addr
430 << ")::recvFromUpperLayers() recv queue size: " << pkt_rcv_queue.size() << endl;
431
433 {
434
436 stateTxData();
437 }
438 }
439 else
440 {
441 incrDiscardedPktsTx();
443 }
444}
445
446void UWOFDMAloha::initPkt(Packet *p, UWOFDMALOHA_PKT_TYPE type, int dest_addr)
447{
448 hdr_cmn *ch = hdr_cmn::access(p);
449 hdr_mac *mach = HDR_MAC(p);
450 hdr_MPhy *ph = HDR_MPHY(p);
451 hdr_OFDM *ofdmph = HDR_OFDM(p);
452 // use MF_BEACON for disturb packets
453
455 ofdmph->nativeOFDM = true;
456 ofdmph->carrierNum = mac_ncarriers;
457
459 {
460 std::cout << NOW << " UWOFDMAloha (" << addr
461 << ")::initPkt(), pkt destination " << dest_addr << std::endl;
462 }
463
464 for (std::size_t i = 0; i < mac_carVec.size(); ++i)
465 {
466 ofdmph->carriers[i] = mac_carVec[i];
467 ofdmph->carMod[i] = mac_carMod[i];
468 }
471
472 int curr_size = ch->size();
473
474 switch (type)
475 {
476
478 {
479 ch->size() = curr_size + HDR_size;
480 if (disturbanceNode)
481 mach->ftype() = MF_BEACON;
482 else
483 mach->ftype() = MF_DATA;
484 }
485 break;
486
487 case (UWOFDMALOHA_ACK_PKT):
488 {
489 ch->ptype() = PT_MMAC_ACK;
490 ch->size() = ACK_size;
491 ch->uid() = recv_data_id;
492 mach->macSA() = addr;
493 mach->macDA() = dest_addr;
494 }
495 break;
496 }
498 {
499 std::cout << NOW << " UWOFDMAloha (" << addr
500 << ")::initPkt() Packet SEQ NUM " << getPktSeqNum(p) << " initialized like:" << std::endl;
501 }
502}
503
505{
506
508 cout << NOW << " UWOFDMAloha (" << addr
509 << ")::Mac2PhyStartTx() start tx packet" << endl;
510
511 hdr_OFDM *ofdmph = HDR_OFDM(p);
512
513 hdr_mac *mach = HDR_MAC(p);
514 MMac::Mac2PhyStartTx(p);
515
517 std::cout << NOW << " UWOFDMAloha (" << addr
518 << ")::Mac2PhyStartTx() Sent down packet SEQ NUM "
519 << getPktSeqNum(p) << " to " << mach->macDA() << endl;
520}
521
522void UWOFDMAloha::Phy2MacEndTx(const Packet *p)
523{
524 hdr_mac *mach = HDR_MAC(p);
525
527 cout << NOW << " UWOFDMAloha (" << addr
528 << ")::Phy2MacEndTx() end tx packet for node " << mach->macDA() << endl;
529
530 switch (curr_state)
531 {
532
534 {
537 {
538
540 cout << NOW << " UWOFDMAloha (" << addr
541 << ")::Phy2MacEndTx() DATA sent,from "
542 << status_info[curr_state] << " to "
544
545 stateWaitAck();
546 }
547 else
548 {
549
551 cout << NOW << " UWOFDMAloha (" << addr
552 << ")::Phy2MacEndTx() DATA sent, from "
553 << status_info[curr_state] << " to "
555
556 stateIdle();
557 }
558 }
559 break;
560
562 {
564
566 cout << NOW << " UWOFDMAloha (" << addr
567 << ")::Phy2MacEndTx() ack sent, from "
568 << status_info[curr_state] << " to "
570 stateIdle();
571 }
572 break;
573
574 default:
575 {
576 cout << NOW << " UWOFDMAloha (" << addr
577 << ")::Phy2MacEndTx() logical error, current state = "
578 << status_info[curr_state] << " prev state " << status_info[prev_state]
579 << " reason " << reason_info[last_reason] << endl;
580 stateIdle();
581 // exit(1);
582 }
583 break;
584 }
585}
586
587void UWOFDMAloha::Phy2MacStartRx(const Packet *p)
588{
589 hdr_mac *mach = HDR_MAC(p);
590 Packet *temp_packet = p->copy();
591 n_receptions++;
593 cout << NOW << " UWOFDMAloha (" << addr << ")::Phy2MacStartRx() rx Packet SEQ NUM "
594 << getPktSeqNum(temp_packet) << " from " << mach->macSA() << endl;
595}
596
598{
599 hdr_cmn *ch = HDR_CMN(p);
600 packet_t rx_pkt_type = ch->ptype();
601 hdr_mac *mach = HDR_MAC(p);
602 MacFrameType frame_type = mach->ftype();
603 hdr_MPhy *ph = HDR_MPHY(p);
604
605 int source_mac = mach->macSA();
606 int dest_mac = mach->macDA();
607
608 double gen_time = ph->txtime;
609 double received_time = ph->rxtime;
610 double diff_time = received_time - gen_time;
611
612 double distance = diff_time * prop_speed;
613
614 n_receptions--;
615
617 cout << NOW << " UWOFDMAloha (" << addr << ")::Phy2MacEndRx() "
619 << ", received a pkt type = " << ch->ptype()
620 << ", src addr = " << mach->macSA()
621 << " dest addr = " << mach->macDA()
622 << ", estimated distance between nodes = " << distance << " m "
623 << endl;
624
625 if (ch->error())
626 {
627
629 cout << NOW << " UWOFDMAloha (" << addr
630 << ")::Phy2MacEndRx() dropping corrupted pkt " << endl;
631 incrErrorPktsRx();
632
635 return;
636 }
637 else
638 {
639 if (dest_mac == addr || dest_mac == MAC_BROADCAST)
640 {
641 if (frame_type == MF_ACK)
642 {
644 {
646 // Packet::free(p);
648 cout << NOW << " UWOFDMAloha (" << addr
649 << ")::Phy2MacEndRx() PKT NOT FOR ME 111 (wasn't waiting ACKS)" << endl;
651 return;
652 }
654 {
655 cout << NOW << " UWOFDMAloha (" << addr
656 << ")::Phy2MacEndRx() PKT NOT FOR ME 333 (wasn't waiting ACKS)" << endl;
657
659
660 return;
661 }
662 if (getPktSeqNum(p) != waitforpktnum)
663 {
664 cout << NOW << " UWOFDMAloha (" << addr
665 << ")::Phy2MacEndRx() PKT NOT FOR ME 444 (Previous ACK)" << endl;
667 return;
668 }
670 stateRxAck(p);
671 }
672 else if (frame_type == MF_DATA && curr_state == UWOFDMALOHA_STATE_IDLE)
673 {
676 cout << NOW << " UWOFDMAloha (" << addr
677 << ")::Phy2MacEndRx() going to stateRxData " << endl;
678
679 stateRxData(p);
680 }
681 else
682 {
684
686 std::cout << NOW << " UWOFDMAloha (" << addr
687 << ")::Phy2MacEndRx() NOT IN CONDITION TO RECEIVE FOR NOW " << endl;
689 return;
690 }
691 }
692 else
693 {
695 if(frame_type != MF_RTS && frame_type != MF_CTS && frame_type != MF_ACK && frame_type != MF_DATA){
696 cerr << NOW << " UWSmartOFDM (" << addr << ")::Phy2MacEndRx Unrecognized packet " << std::endl;
698 }
699 else {
700
702 cout << NOW << " UWOFDMAloha (" << addr
703 << ")::Phy2MacEndRx() PKT NOT FOR ME 222x " << endl;
704
706 }
707 return;
708 }
709 }
710}
711
713{
714 Packet *data_pkt = curr_data_pkt->copy();
715
717 {
719 }
720 waitforpktnum = getPktSeqNum(data_pkt);
722 cout << NOW << " UWOFDMAloha (" << addr
723 << ")::txData sending SEQ NUM PACKET " << getPktSeqNum(data_pkt) << std::endl;
724
725 incrDataPktsTx();
727 Mac2PhyStartTx(data_pkt);
728}
729
730void UWOFDMAloha::txAck(int dest_addr)
731{
732 Packet *ack_pkt = Packet::alloc();
733 initPkt(ack_pkt, UWOFDMALOHA_ACK_PKT, dest_addr);
734
736 cout << NOW << " UWOFDMAloha (" << addr
737 << ")::txAck sending ACK to " << dest_addr << std::endl;
738
739 incrAckPktsTx();
740 Mac2PhyStartTx(ack_pkt);
741}
742
744{
746
748 cout << NOW << " UWOFDMAloha (" << addr << ")::stateCheckAckExpired()"
749 << endl;
750
751 map<pktSeqNum, AckTimer>::iterator it_a;
752 it_a = mapAckTimer.begin();
753
756 if (((*it_a).second).isActive())
757 {
760 }
761 else if (((*it_a).second).isExpired())
762 {
764 stateBackoff();
765 }
766 else
767 {
768 cerr << NOW << " UWOFDMAloha (" << addr
769 << ")::stateCheckAckExpired() ack_timer logical error, current "
770 "timer state = "
771 << status_info[curr_state] << endl;
772 stateIdle();
773 }
774}
775
777{
779
781 cout << NOW << " UWOFDMAloha (" << addr << ")::stateCheckBackoffExpired()"
782 << endl;
786 {
788 stateBackoff();
789 }
790 else if (backoff_timer.isExpired())
791 {
793 exitBackoff();
794 stateIdle();
795 }
796 else
797 {
798 cerr << NOW << " UWOFDMAloha (" << addr
799 << ")::stateCheckBackoffExpired() backoff_timer logical error, "
800 "current timer state = "
801 << status_info[curr_state] << endl;
802 stateIdle();
803 }
804}
805
807{
808 mapAckTimer.clear();
810
812 cout << NOW << " UWOFDMAloha (" << addr
813 << ")::stateIdle() reception queue size = " << pkt_rcv_queue.size() << endl;
814
816 cout << NOW << " UWOFDMAloha (" << addr
817 << ")::stateIdle() transmission queue size = " << mapPacket.size() << endl;
818
820
823
824 if (!mapPacket.empty() && n_receptions == 0)
825 {
826 stateTxData();
827 }
828}
829
837
839{
840 backoff_timer.force_cancel();
842
845 else
847
849 std::cout << NOW << " UWOFDMAloha (" << addr << ")::stateBackoff() " << endl;
850 if (uwofdmaloha_debug && mapPacket.size() > 0)
851 {
852
853 map<pktSeqNum, Packet *>::iterator it_p;
854 it_p = mapPacket.begin();
856 displayCarriers((*it_p).second);
857 }
860}
861
870
872{
874
876 std::cout << NOW << " UWOFDMAloha (" << addr << ")::stateTxData() " << endl;
879
880 map<pktSeqNum, Packet *>::iterator it_p;
881 it_p = mapPacket.begin();
882 curr_data_pkt = (*it_p).second;
883 int seq_num;
884 seq_num = getPktSeqNum(curr_data_pkt);
885
888 for (int i = 0; i < mac_ncarriers; i++)
889 {
890 std::cout << "carrier[" << i << "] = " << ofdmph->carMod[i] << std::endl;
891 }
892
893 map<pktSeqNum, AckTimer>::iterator it_a;
894
895 if (seq_num != last_sent_data_id)
896 {
897 putAckTimerInMap(seq_num);
898 it_a = mapAckTimer.find(seq_num);
901 ((*it_a).second).resetCounter();
902 hdr_mac *mach = HDR_MAC(curr_data_pkt);
903 start_tx_time = NOW; // we set curr RTT
904 last_sent_data_id = seq_num;
905 txData();
906 }
907 else
908 {
909
910 if (mapAckTimer.size() == 0)
911 {
912 putAckTimerInMap(seq_num);
913 it_a = mapAckTimer.find(seq_num);
914 ((*it_a).second).resetCounter();
918 {
919 hdr_mac *mach = HDR_MAC(curr_data_pkt);
920 start_tx_time = NOW; // we set curr RTT
921 last_sent_data_id = seq_num;
922 txData();
923 }
924 else
925 {
926 eraseItemFromPktQueue(seq_num);
927 incrDroppedPktsTx();
928
930
932 cout << NOW << " UWOFDMAloha (" << addr
933 << ")::statePreTxData() curr_tx_rounds "
935 << " > max_tx_tries = " << max_tx_tries << endl;
936 waitforpktnum = -1;
937 stateIdle();
938 }
939 } else {
941 }
942 }
943}
944
946{
947
948 map<pktSeqNum, AckTimer>::iterator it_a;
949 it_a = mapAckTimer.begin();
950
951 ((*it_a).second).stop();
953
955 cout << NOW << " UWOFDMAloha (" << addr << ")::stateWaitAck() " << endl;
958
959 ((*it_a).second).incrCounter();
960 ((*it_a).second).schedule(ACK_timeout + 2 * wait_constant);
961}
962
970
971void UWOFDMAloha::stateTxAck(int dest_addr)
972{
974
976 cout << NOW << " UWOFDMAloha (" << addr << ")::stateTxAck() dest addr "
977 << dest_addr << endl;
980
981 txAck(dest_addr);
982}
983
984void UWOFDMAloha::stateRxData(Packet *data_pkt)
985{
986 ack_timer.stop();
988
990
991 hdr_mac *mach = HDR_MAC(data_pkt);
992 int dst_addr = mach->macSA();
993
994 hdr_cmn *ch = hdr_cmn::access(data_pkt);
995 recv_data_id = ch->uid();
996 ch->size() = ch->size() - HDR_size;
997 incrDataPktsRx();
998 sendUp(data_pkt);
1000 cout << NOW << " UWOFDMAloha (" << addr << ")::stateRxData() SEQ NUM "
1001 << getPktSeqNum(data_pkt) << " from " << dst_addr << endl;
1003 stateTxAck(dst_addr);
1004 else
1005 stateIdle();
1006}
1007
1009{
1010
1011 map<pktSeqNum, AckTimer>::iterator it_a;
1012 it_a = mapAckTimer.begin();
1013
1014 ((*it_a).second).stop();
1017 cout << NOW << " UWOFDMAloha (" << addr << ")::stateRxAck() " << endl;
1018
1019 int seq_num;
1020 seq_num = getPktSeqNum(p);
1021
1023 cout << NOW << " UWOFDMAloha (" << addr << ")::stateRxAck() seq number " << seq_num << endl;
1024 Packet::free(p);
1025
1028 cout << NOW << " UWOFDMAloha (" << addr << ")::stateRxAck() refreshReason " << endl;
1029
1030 eraseItemFromPktQueue(seq_num);
1032 cout << NOW << " UWOFDMAloha (" << addr << ")::stateRxAck() erase from pkt queue" << endl;
1033 eraseItemFrommapAckTimer(seq_num);
1035 cout << NOW << " UWOFDMAloha (" << addr << ")::stateRxAck() erase from ACK timer" << endl;
1037 incrAckPktsRx();
1039 cout << NOW << " UWOFDMAloha (" << addr << ")::stateRxAck() Going back to Idle" << endl;
1040 stateIdle();
1041}
1042
1044{
1046 cout << NOW << " UWOFDMAloha (" << addr << ")::printStateInfo() "
1047 << "from " << status_info[prev_state] << " to "
1049 << ". Reason: " << reason_info[last_reason] << endl;
1050
1052 {
1053 fout << left << setw(10) << NOW << " UWOFDMAloha (" << addr
1054 << ")::printStateInfo() "
1055 << "from " << status_info[prev_state] << " to "
1057 << ". Reason: " << reason_info[last_reason]
1058 << ". Backoff duration = " << delay << endl;
1059 }
1060 else
1061 {
1062 fout << left << setw(10) << NOW << " UWOFDMAloha (" << addr
1063 << ")::printStateInfo() "
1064 << "from " << status_info[prev_state] << " to "
1066 << ". Reason: " << reason_info[last_reason] << endl;
1067 }
1068}
Class that describe the binding with tcl scripting language.
TclObject * create(int, const char *const *)
UWOFDMALOHAModuleClass()
Constructor of the class.
virtual void expire(Event *e)
What a node is going to do when a timer expire.
virtual void freeze()
It freezes or in another word, it stops the timer for some time.
void incrCounter()
Increment the timer counter.
bool isActive()
It tells whether the timer is active or not.
bool isExpired()
Tells whether the timer is expired or not.
bool isFrozen()
It tells whether the timer is in freeze mode or not.
virtual void schedule(double val)
Schedule the time, i.e., how long a timer is going to run.
double getDuration()
This methods provide the duration of a timer.
virtual void stop()
Stop the timer any way.
virtual void unFreeze()
It starts the timer from where it was stopped.
void resetCounter()
Reset the timer counter.
int getCounter()
It provides, how many times a timer ran.
UWOFDMAloha *UWOFDMALOHA_TIMER_STATUS timer_status
< Pointer of UWOFDMAloha module.
virtual ~UWOFDMAloha()
Destructor of UWOFDMAloha Class.
UWOFDMALOHA_REASON_STATUS last_reason
Enum variable which stores the last reason why a node changes its state.
static map< UWOFDMALOHA_PKT_TYPE, string > pkt_type_info
Container which stores all the packet type information of UWOFDMAloha.
void eraseItemFromPktQueue(int seq_num)
It erases the packet from the container.
virtual void resetCurrTxRounds()
If a node is going to transmit a new packet, it resets the tx counter.
Packet * curr_data_pkt
Pointer of the latest selected data packet.
int getPktSeqNum(Packet *p)
This method is used to get the sequence number from a packet.
@ UWOFDMALOHA_REASON_ACK_TX
@ UWOFDMALOHA_REASON_BACKOFF_TIMEOUT
@ UWOFDMALOHA_REASON_DATA_RX
@ UWOFDMALOHA_REASON_DATA_EMPTY
@ UWOFDMALOHA_REASON_ACK_TIMEOUT
@ UWOFDMALOHA_REASON_ACK_RX
@ UWOFDMALOHA_REASON_DATA_PENDING
@ UWOFDMALOHA_REASON_PKT_ERROR
@ UWOFDMALOHA_REASON_MAX_TX_TRIES
@ UWOFDMALOHA_REASON_WAIT_ACK_PENDING
@ UWOFDMALOHA_REASON_BACKOFF_PENDING
@ UWOFDMALOHA_REASON_DATA_TX
@ UWOFDMALOHA_REASON_NOT_SET
@ UWOFDMALOHA_REASON_PKT_NOT_FOR_ME
@ UWOFDMALOHA_REASON_START_RX
virtual void txAck(int dest_addr)
This methods transmits ACK packet from MAC layer to PHY layer.
UWOFDMALOHA_ACK_MODES ack_mode
Enum variable.
UWOFDMALOHA_STATUS prev_state
Enum variable.
virtual void incrCurrTxRounds()
Increments the current transmission round of a packet.
UWOFDMAloha()
Constructor of UWOFDMAloha Class.
virtual void stateRxIdle()
If a node start receiving a packet in Idle state.
void init_macofdm_node(int subCarNum, double subCarSize, int ctrl_subCar, string modulation)
Initialize subCarriers parameters inside a node, default all carriers are used.
virtual void stateRxData(Packet *p)
It process the packet which is received.
double wait_constant
This fixed time is used to componsate different time variations.
virtual void stateCheckBackoffExpired()
It checks whether the backoff timer is already expired while it was busy with other activities.
int ACK_size
Size of the ACK, if the node uses ARQ technique.
map< pktSeqNum, Packet * > mapPacket
Container where Data packets are stored.
virtual void Phy2MacEndRx(Packet *p)
PHY layer informs the MAC layer that the reception of the packet is over.
int uwofdmaloha_debug
Debuging Flag.
virtual void refreshReason(UWOFDMALOHA_REASON_STATUS reason)
To know the reason why a node is in this current state.
double max_backoff_counter
Maximum number of backoff it will consider while it increases the backoff exponentially.
int rttsamples
Number of RTT samples.
std::vector< int > nouse_carriers
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
virtual void stateTxAck(int dest_addr)
If the protocl uses ARQ technique, in that case, after receiving a Data packet the node sends an ACK ...
double srtt
Smoothed Round Trip Time, calculated as for TCP.
std::vector< Packet * > pkt_rcv_queue
virtual void exitBackoff()
It stops the backoff timer.
int last_sent_data_id
sequence number of the last sent packet
static const double prop_speed
Speed of the sound signal.
int last_data_id_rx
The sequence number of last received packet.
virtual double getBackoffTime()
This function calculates the backoff duration and return the backoff time.It employs the exponential ...
ofstream fout
An object of ofstream class.
int curr_tx_rounds
How many times a packet is transmitted.
virtual void refreshState(UWOFDMALOHA_STATUS state)
Refreshes the states of the node.
virtual void initPkt(Packet *p, UWOFDMALOHA_PKT_TYPE pkt_type, int dest_addr=0)
This method, initialize the packet.
virtual void stateBackoff()
If ACK packet is not received within the acknowledgement expire time.
void addInvalidCarriers(int c)
Adds carriers that should not be used.
virtual double computeTxTime(UWOFDMALOHA_PKT_TYPE type)
Compute the transmission time of a packet.
UWOFDMALOHA_STATUS curr_state
Enum variable.
double start_tx_time
Time when a packet start transmitting.
double alpha_
This variable is used to tune the RTT.
virtual void Phy2MacEndTx(const Packet *p)
It infroms that a packet transmission end.
virtual void recvFromUpperLayers(Packet *p)
This function receives the packet from upper layer and save it in the queue.
double ACK_timeout
ACK timeout for the initial packet.
static map< UWOFDMALOHA_REASON_STATUS, string > reason_info
Container which stores all the reason information.
virtual void Mac2PhyStartTx(Packet *p)
It informs that a packet transmission started.
virtual void stateRxAck(Packet *p)
The node comes to this state if it receives an ACK packet.
static map< UWOFDMALOHA_STATUS, string > status_info
Container which stores all the status information.
virtual void txData()
This method transmits Data packets from MAC layer to PHY layer.
int buffer_pkts
Number of packets a node can store in the container.
virtual void updateRTT(double rtt)
Update the Round Trip Time (RTT) which is necessary to compute the acknowledgement duration as well a...
map< pktSeqNum, AckTimer > mapAckTimer
Container where acknowledgement timer(s) is stored.
virtual void stateRxBackoff()
If a node start receiving a packet when it is in backoff state.
virtual void stateTxData()
If a node has packet to transmits.
std::vector< string > mac_carMod
--— OFDM PARAMS
UWOFDMALOHA_PKT_TYPE
Enumeration class of UWOFDMAloha packet type.
virtual void Phy2MacStartRx(const Packet *p)
PHY layer informs the MAC layer that it is receiving a packet.
void putPktInQueue(Packet *p)
A node receives packet(s) from upper layer and store them in the container.
int getUpLayersDataPktsRx()
bool print_transitions
Whether to print the state of the nodes.
double mac_carrierSize
AckTimer ack_timer
An object of the AckTimer class.
int recv_data_id
The sequence number of the packet which is received.
int max_tx_tries
Maximum number of retransmissions attempt.
@ UWOFDMALOHA_STATE_BACKOFF
@ UWOFDMALOHA_STATE_CHK_BACKOFF_TIMEOUT
@ UWOFDMALOHA_STATE_WRONG_PKT_RX
@ UWOFDMALOHA_STATE_WAIT_ACK
@ UWOFDMALOHA_STATE_DATA_RX
@ UWOFDMALOHA_STATE_RX_IDLE
@ UWOFDMALOHA_STATE_RX_IN_PROGRESS
@ UWOFDMALOHA_STATE_TX_DATA
@ UWOFDMALOHA_STATE_CHK_ACK_TIMEOUT
@ UWOFDMALOHA_STATE_MULTIPLE_RX_IN_PROGRESS
@ UWOFDMALOHA_STATE_RX_BACKOFF
@ UWOFDMALOHA_STATE_RX_WAIT_ACK
int txsn
Sequence number of the packet which is transmitted.
virtual void stateRxWaitAck()
If a node receives any packet while it was waiting for ACK packet, it moves to this state.
double sumrtt
Sum of RTT samples.
bool has_buffer_queue
Whether the node has buffer to store data or not.
BackOffTimer backoff_timer
An object of the backoff timer class.
virtual void printStateInfo(double delay=0)
This methods print the state information of the nodes.
double backoff_tuner
Tunes the backoff duration.
double sumrtt2
Sum of (RTT^2)
virtual void updateAckTimeout(double rtt)
Like updateRTT() function.
int max_payload
Maximum number of payload in a packet.
static bool initialized
It checks whether UWOFDMAloha protocol is initialized or not.
virtual void incrUpperDataRx()
Increment the number of Data packet receive for the upper layer.
int HDR_size
Size of the HDR if any.
virtual void stateCheckAckExpired()
It checks whether the ack timer is already expired while it was busy with other activities.
void displayCarriers(Packet *p)
Prints the carriers as a sequence of 0s and 1s.
virtual void stateWaitAck()
After transmitting a Data packet, a node waits for the ACK packet.
std::vector< int > mac_carVec
UWOFDMALOHA_STATUS prev_prev_state
Enum variable.
virtual void initInfo()
This function is used to initialize the UWOFDMAloha protocol.
virtual void stateIdle()
Node is in Idle state.
void putAckTimerInMap(int seq_num)
Put acknowledgement timer in the container.
void eraseItemFrommapAckTimer(int seq_num)
Erase an item from acknowledgement stored container.
Header of the OFDM message with fields to implement a multi carrier system.
int carriers[MAX_CARRIERS]
double carrierSize
bool nativeOFDM
string carMod[MAX_CARRIERS]
UWOFDMALOHAModuleClass class_module_uwofdmaloha
@ NOT_SET
@ SESSION_DISTANCE_NOT_SET
This is the base class of UWOFDMAloha Protocol, derived of MMac. Your can find the brief description ...
#define UWOFDMALOHA_DROP_REASON_ERROR
#define UWOFDMALOHA_DROP_REASON_WRONG_RECEIVER
#define UWOFDMALOHA_DROP_REASON_BUFFER_FULL
@ NOT_SET
Definition uwaloha.cpp:44
std::pair< int, int > counter
counter of collisions
#define HDR_OFDM(p)
alias defined to access the PROBE HEADER
Definition of ClMsgUwMmac class.