DESERT 3.5.1
Loading...
Searching...
No Matches
uwmulti-stack-controller-phy-master.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2014 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
40#include <mphy_pktheader.h>
41
45static class UwMultiStackControllerPhyMasterClass : public TclClass
46{
47 public:
51 UwMultiStackControllerPhyMasterClass() : TclClass("Module/UW/MULTI_STACK_CONTROLLER_PHY_MASTER") {}
52
57 TclObject* create(int, const char*const*)
58 {
60 }
62
64:
66 signaling_active_(0),
67 last_layer_used_(0),
68 power_statistics_(0),
69 power_stat_node_(MAC_BROADCAST),
70 alpha_(0.5),
71 signaling_period_(10),
72 signaling_sent_(0),
73 signaling_timer_(this)
74{
75 bind("alpha_", &alpha_);
76 bind("signaling_active_", &signaling_active_);
77 bind("signaling_period_", &signaling_period_);
78}
79
80int UwMultiStackControllerPhyMaster::command(int argc, const char*const* argv)
81{
82 Tcl& tcl = Tcl::instance();
83 if (argc == 2)
84 {
85 if(strcasecmp(argv[1], "signalingON") == 0)
86 {
88 return TCL_OK;
89 }
90 else if(strcasecmp(argv[1], "signalingOFF") == 0)
91 {
93 return TCL_OK;
94 }
95 else if(strcasecmp(argv[1], "getSignalsSent") == 0)
96 {
97 tcl.resultf("%d", signaling_sent_);
98 return TCL_OK;
99 }
100 }
101 else if (argc == 3)
102 {
103 if(strcasecmp(argv[1], "setAlpha") == 0)
104 {
105 alpha_ = atof(argv[2]);
106 return TCL_OK;
107 }
108 else if(strcasecmp(argv[1], "setManualLowerlId") == 0)
109 {
110 lower_id_active_ = atoi(argv[2]);
112 return TCL_OK;
113 }
114 }
115 return UwMultiStackControllerPhy::command(argc, argv);
116} /* UwMultiStackControllerPhyMaster::command */
117
118//signaling TIMER
120 assert(module->signaling_active_);
121 if (module->debug_)
122 {
123 std::cout << NOW << " expired! " << std::endl;
124 }
125 ((UwMultiStackControllerPhyMaster *)module)->resetCheckAndSignal();
126}
127
129{
130 hdr_cmn *ch = HDR_CMN(p);
131 if (ch->direction() == hdr_cmn::UP)
132 updateMasterStatistics(p,idSrc);
133 // Filippo: signaling con risposta
134 if (ch->ptype() == PT_MULTI_ST_SIGNALING)
135 Packet::free(p);
136 else
138}
139
150
163
165{
166 assert(signaling_active_);
167 if (debug_)
168 {
169 std::cout << NOW << " UwMultiStackControllerPhyMaster::resetCheckAndSignal " << std::endl;
170 }
173 power_stat_node_ = MAC_BROADCAST;
174}
175
177{
178 int mac_addr = -1;
179 ClMsgPhy2MacAddr msg;
180 sendSyncClMsg(&msg);
181 mac_addr = msg.getAddr();
182
183 int id_short_range = getShorterRangeLayer(last_layer_used_);
184 int id_long_range = getLongerRangeLayer(last_layer_used_);
185 double upper_threshold; //meaningful value only if (upper_threshold_valid)
186 double lower_threshold; //meaningful value only if (lower_threshold_valid)
187 bool upper_threshold_valid = getThreshold(last_layer_used_,id_short_range,upper_threshold);
188 bool lower_threshold_valid = getThreshold(last_layer_used_,id_long_range,lower_threshold);
189
190 if (upper_threshold_valid && (power_statistics_ > upper_threshold)) {
191 lower_id_active_ = id_short_range;
192 }
193 if (lower_threshold_valid && (power_statistics_ < lower_threshold)) {
194 lower_id_active_ = id_long_range;
195 }
196
197 if (debug_)
198 {
199 std::cout << NOW << " ControllerPhyMaster("<< mac_addr
200 <<")::checkBestLayer(), power_statistics_= " << power_statistics_
201 << " best layer id = " << lower_id_active_ << " upper_threshold = " << upper_threshold
202 << " lower_threshold = " << lower_threshold << std::endl;
203 }
204 if (signaling_active_) {
205 signaling_timer_.force_cancel();
207 }
208 return lower_id_active_;
209}
210
212{
213 assert(signaling_active_);
214 //Retreive my mac to set macSA
215 int my_mac_addr = -1;
216 ClMsgPhy2MacAddr msg;
217 sendSyncClMsg(&msg);
218 my_mac_addr = msg.getAddr();
219
220 Packet *p = Packet::alloc();
221 hdr_cmn* ch = hdr_cmn::access(p);
222 ch->ptype() = PT_MULTI_ST_SIGNALING;
223 ch->size() = signaling_pktSize_;
224 hdr_mac* mach = HDR_MAC(p);
225 //mach->macDA() = MAC_BROADCAST;
226 mach->macDA() = power_stat_node_;
227 mach->macSA() = my_mac_addr;
228 if (debug_)
229 {
230 std::cout << NOW << " ControllerPhyMaster::signalsBestPhy()" << std::endl;
231 }
233 sendDown(lower_id_active_, p, min_delay_);
234
235}
236
238 //It blocks the clmsg caused by our signaling
239 if (m->type() == CLMSG_PHY2MAC_ENDTX)
240 {
241 hdr_cmn* ch = hdr_cmn::access( static_cast<ClMsgPhy2MacEndTx *>(m)->pkt);
242 if (ch->ptype() == PT_MULTI_ST_SIGNALING)
243 return 0;
244 }
245 else if (m->type() == CLMSG_PHY2MAC_STARTRX)
246 {
247 hdr_cmn* ch = hdr_cmn::access( static_cast<ClMsgPhy2MacStartRx *>(m)->pkt);
248 if (ch->ptype() == PT_MULTI_ST_SIGNALING)
249 return 0;
250 }
252}
253
255{
256 int mac_addr = -1;
257 ClMsgPhy2MacAddr msg;
258 sendSyncClMsg(&msg);
259 mac_addr = msg.getAddr();
260
261 hdr_mac* mach = HDR_MAC(p);
262 hdr_MPhy* ph = HDR_MPHY(p);
263 double power_statistics_to_print = power_statistics_;
264 /*if (mach->macDA() == mac_addr && idSrc == last_layer_used_)*/
265 if (mach->macDA() == mac_addr && idSrc == lower_id_active_)
266 {
267 if (power_stat_node_ == mach->macSA())
269 : ph->Pr;
270 else if (ph->Pr > power_statistics_){
271 power_statistics_ = ph->Pr;
272 power_stat_node_ = mach->macSA();
273 }
274 power_statistics_to_print = power_statistics_;
275
276 if (debug_)
277 {
278 std::cout << NOW << " ControllerPhyMaster("<< mac_addr <<
279 ")::updateMasterStatistics(Packet *p, int idSrc), Pr = " << ph->Pr << " idSrc = " << idSrc
280 << " lower_id_active_ = " << lower_id_active_ << " power_statistics_ = " << power_statistics_to_print << std::endl;
281 }
282
285 }
286 else if (debug_)
287 std::cout << NOW << " ControllerPhyMaster("<< mac_addr <<
288 ")::updateMasterStatistics(Packet *p, int idSrc), Pr = " << ph->Pr << " idSrc = " << idSrc
289 << " lower_id_active_ = " << lower_id_active_ << " power_statistics_ = " << power_statistics_to_print << std::endl;
290}
291
292
Class that represents the binding with the tcl configuration script.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
Class used to represents the UwMultiStackControllerPhyMaster layer of a node.
int getLongerRangeLayer(int layer_id)
Return the previous layer in order which can achieve longer range with lower bitrete.
int getShorterRangeLayer(int layer_id)
Return the next layer in order which can achieve shorter range with higer bitrete.
virtual void resetCheckAndSignal()
If signaling is active and signaling timer expires, check the average power thresholds at each packet...
double power_statistics_
ID of the last PHY layer used.
virtual int checkBestLayer()
Check the average power thresholds.
virtual int getBestLayer(Packet *p)
Return the best layer to forward the packet when the system works in AUTOMATIC_MODE.
UwMultiStackSignalingTimer signaling_timer_
Number of signaling packets sent.
double alpha_
Address of the node which the master is collecting the average power.
int power_stat_node_
Average received power from the closest node according to the IIR filter.
virtual void checkAndSignal()
If signaling is active, check the average power thresholds at each packet received.
int last_layer_used_
If true master is in signaling mode, otherwise not.
virtual void signalsBestPhy()
Signals the best phy.
UwMultiStackControllerPhyMaster()
Constructor of UwMultiPhy class.
virtual int command(int, const char *const *)
TCL command interpreter.
int signaling_sent_
Period to check the best layer if no interactions occurs.
virtual void updateMasterStatistics(Packet *p, int idSrc)
It implements the slave choice rule to choose the lower layer when the system works in AUTOMATIC_MODE...
int recvSyncClMsg(ClMessage *m)
Cross-Layer messages synchronous interpreter.
virtual void recv(Packet *p, int idSrc)
It manages each packet reception, either from the upper and the lower layer.
virtual void recv(Packet *p, int idSrc)
It manages each packet reception, either from the upper and the lower layer.
virtual int recvSyncClMsg(ClMessage *m)
Cross-Layer messages synchronous interpreter.
virtual int command(int, const char *const *)
TCL command interpreter.
int debug_
Flag to activate debug verbosity.
@ UW_AUTOMATIC_SWITCH
State to switch-mode automatically.
int lower_id_active_
<Current switch mode (either AUTOMATIC or MANUAL).
virtual bool getThreshold(int i, int j, double &thres_ij)
get the threshold value for the transition from layer i to layer j, checking first whether the layers...
UwMultiStackControllerPhyMasterClass class_uwmulti_stack_controller_phy_master
Definition of UwMultiStackControllerPhyMaster class. This controller can switch from TWO layers thank...