DESERT 3.5.1
Loading...
Searching...
No Matches
uwPosBasedRt.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 \‍) 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 "uwPosBasedRt.h"
40#include <iostream>
41
42extern packet_t PT_UWPOSITIONBASEDROUTING;
43
44
45
46static class UwPosBasedRtModuleClass : public TclClass
47{
48public:
50 : TclClass("Module/UW/PosBasedRt")
51 {
52 }
53
54 TclObject *
55 create(int, const char *const *)
56 {
57 return (new UwPosBasedRt());
58 }
60
61
62
64 : ipAddr(0)
65 , timestamp(0)
66 , ROV_speed(1)
67 , maxTxRange(3000)
68 , node_pos()
69{
70 static_routing.clear();
71 bind("debug_", &debug_);
72 bind("maxTxRange_",(double *) &maxTxRange);
73 bind("ROV_speed_", (double *) &ROV_speed);
74 if (maxTxRange <= 0) {
75 std::cout << " UwPosBasedRt::maxTxRange value not valid, value set by "
76 << "default to 3000 m" << std::endl;
77 maxTxRange = 3000;
78 }
79 if (ROV_speed <= 0) {
80 std::cout << " UwPosBasedRt::ROV_speed value not valid, value set by "
81 << "defaul to 1 m/s" << std::endl;
82 ROV_speed = 1;
83 }
84}
85
89
90
91
92int UwPosBasedRt::command(int argc, const char *const *argv)
93{
94 Tcl &tcl = Tcl::instance();
95
96 if(argc == 3) {
97 if (strcasecmp(argv[1],"setMaxTxRange") == 0) {
98 if (atof(argv[2]) <= 0) {
99 std::cerr << " UwPosBasedRt(IP=" <<(int)ipAddr << ")::invalid "
100 << "value for max transmission range, use a value >= 0";
101 return TCL_ERROR;
102 }
103 setMaxTxRange(atof(argv[2]));
104 return TCL_OK;
105 }
106 if (strcasecmp(argv[1], "addr") == 0) {
107 ipAddr = static_cast<uint8_t>(atoi(argv[2]));
108 if (ipAddr == 0) {
109 std::cerr << "UwPosBasedRt::0 is not a valid IP address"
110 << std::endl;
111 return TCL_ERROR;
112 }
113 return TCL_OK;
114 }
115 if (strcasecmp(argv[1],"setNodePosition") == 0) {
116 Position *p = dynamic_cast<Position*> (tcl.lookup(argv[2]));
117 node_pos = *p;
118 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
119 << ")::node position, x: " <<node_pos.getX() << ", y: "
120 << node_pos.getY()<< ", z: " <<node_pos.getZ() << std::endl;
121 return TCL_OK;
122 }
123 } else if (argc == 5) {
124 if (strcasecmp(argv[1], "addRoute") == 0) {
125 if (strcasecmp(argv[4], "toMovingNode") == 0) {
126 addRoute(static_cast<uint8_t>(atoi(argv[2])),
127 static_cast<uint8_t>(atoi(argv[3])),0);
128 return TCL_OK;
129 } else if (strcasecmp(argv[4], "toFixedNode") == 0) {
130 addRoute(static_cast<uint8_t>(atoi(argv[2])),
131 static_cast<uint8_t>(atoi(argv[3])),1);
132 return TCL_OK;
133 } else {
134 std::cerr << "UwPosBasedRt::invalid command, set "
135 << "\"toFixedNode\" or \"toMovingNode\" "<< std::endl;
136 return TCL_ERROR;
137 }
138 }
139 }
140 return Module::command(argc, argv);
141}
142
143
144void UwPosBasedRt::recv(Packet* p)
145{
146 //packet arrives only if i'm the intended node, the next hop or the source
147 if (ipAddr == 0) {
148 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
149 << ")::node IP addres not set, drop packet" << std::endl;
150 drop(p, 1, DROP_IP_NOT_SET);
151 }
152
153 hdr_cmn *ch = HDR_CMN(p);
154 hdr_uwip *iph = HDR_UWIP(p);
155
156 if (ch->direction() == hdr_cmn::UP) {
157
158 updatePosInfo(p);
159
160 if (iph->daddr() == ipAddr) //I'm the intended node
161 {
162 if (debug_)
163 std::cout << NOW << " UwPosBasedRt(IP="
164 <<(int)ipAddr << ")::packet for me, send up" << std::endl;
165
166 sendUp(p);
167 return;
168 } else { //find next hop
169 ch->next_hop() = (nsaddr_t)findNextHop(p);
170 if (ch->next_hop() == 0) {
171 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
172 << ")::next hop not found, drop packet" << std::endl;
173 drop(p, 1, DROP_NEXT_HOP_NOT_FOUND);
174 }
175 if (debug_) {
176 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
177 << ")::next hop: " << (int)ch->next_hop() << std::endl;
178 }
179 sendDown(p);
180 return;
181 }
182
183 } else {//DOWN
184
185 initPkt(p);
186 ch->next_hop() = (nsaddr_t)findNextHop(p);
187 if (ch->next_hop() == 0) {
188 if (debug_)
189 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
190 << ")::next hop not found, drop packet" << std::endl;
191
192 drop(p, 1, DROP_NEXT_HOP_NOT_FOUND);
193 }
194 if (debug_) {
195 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
196 << ")::next hop: " << (int)ch->next_hop() << std::endl;
197 }
198 sendDown(p);
199 return;
200 }
201}
202
204{
205
207 hdr_uwip *iph = HDR_UWIP(p);
208
209 //If iph->daddr() is an address in the ROV_routing table, i.e.,
210 //destination is a vehicles, initialize packet with information about ROV
211
212 std::map<uint8_t, UwPosEstimation>::iterator itROV =
213 ROV_routing.find(iph->daddr());
214 if (itROV != ROV_routing.end()) {
215
216 Position tempLastROVpos = (itROV->second).getInitPos();
217 Position tempLastWp = (itROV->second).getDest();
218
219
220 pbrh->x_ROV() = tempLastROVpos.getX();
221 pbrh->y_ROV() = tempLastROVpos.getY();
222 pbrh->z_ROV() = tempLastROVpos.getZ();
223 pbrh->x_waypoint() = tempLastWp.getX();
224 pbrh->y_waypoint() = tempLastWp.getY();
225 pbrh->z_waypoint() = tempLastWp.getZ();
226 pbrh->timestamp() = (itROV->second).getTimestamp();
227 pbrh->ROV_speed() = (itROV->second).getSpeed();
228 pbrh->IP_ROV() = iph->daddr();
229
230 if (debug_)
231 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
232 << ")::information inserted"
233 << ", x waypoint: " << pbrh->x_waypoint()
234 << " ,y waypoint: " << pbrh->y_waypoint()
235 << " ,z waypoint: " << pbrh->z_waypoint()
236 << " ,x ROV: " << pbrh->x_ROV()
237 << " ,y ROV: " << pbrh->y_ROV()
238 << " ,z ROV: " << pbrh->z_ROV()
239 << " ,timestamp: " << pbrh->timestamp()
240 << " ,ROV speed: " << pbrh->ROV_speed()
241 << " ,ROV IP: " << (int)pbrh->IP_ROV() << std::endl;
242 } else {
243 if (debug_)
244 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
245 << ")::initPacket, dest addr not found "
246 << " in ROV routing table" << std::endl;
247 pbrh->IP_ROV() = 0;
248 }
249
250}
251
253{
254
255
257
258 std::map<uint8_t, UwPosEstimation>::iterator itROV =
259 ROV_routing.find(pbrh->IP_ROV());
260 if (itROV != ROV_routing.end()) { //there is an entry with this IP ROV
261
262 if ((itROV->second).getTimestamp() < pbrh->timestamp()) {
263 //info in the packet is more recent
264 Position tempLastROVpos;
265 tempLastROVpos.setX(pbrh->x_ROV());
266 tempLastROVpos.setY(pbrh->y_ROV());
267 tempLastROVpos.setZ(pbrh->z_ROV());
268 Position tempLastWp;
269 tempLastWp.setX(pbrh->x_waypoint());
270 tempLastWp.setY(pbrh->y_waypoint());
271 tempLastWp.setZ(pbrh->z_waypoint());
272 (itROV->second).update(tempLastROVpos,tempLastWp,
273 pbrh->timestamp(),pbrh->ROV_speed());
274
275 Position provvInitPos = (itROV->second).getInitPos();
276 Position provvDest = (itROV->second).getDest();
277 if (debug_) {
278 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
279 << ")::update node info" << std::endl;
280 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
281 << ")::information setted in the node"
282 << ", x ROV: " << provvInitPos.getX()
283 << " ,y ROV: " << provvInitPos.getY()
284 << " ,z ROV: " << provvInitPos.getZ()
285 << " ,x wp: " << provvDest.getX()
286 << " ,y wp: " << provvDest.getY()
287 << " ,z wp: " << provvDest.getZ()
288 << " ,timestamp: " << (itROV->second).getTimestamp()
289 << " ,ROV speed: " << (itROV->second).getSpeed()
290 << std::endl;
291 }
292 } else { //info in the node is more recent
293
294 Position tempLastROVpos = (itROV->second).getInitPos();
295 Position tempLastWp = (itROV->second).getDest();
296 pbrh->x_ROV() = tempLastROVpos.getX();
297 pbrh->y_ROV() = tempLastROVpos.getY();
298 pbrh->z_ROV() = tempLastROVpos.getZ();
299 pbrh->x_waypoint() = tempLastWp.getX();
300 pbrh->y_waypoint() = tempLastWp.getY();
301 pbrh->z_waypoint() = tempLastWp.getZ();
302 pbrh->timestamp() = (itROV->second).getTimestamp();
303 pbrh->ROV_speed() = (itROV->second).getSpeed();
304
305 if (debug_) {
306 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
307 << ")::update packet info" << std::endl;
308 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
309 << ")::information setted in the packet"
310 << ", x waypoint: " << pbrh->x_waypoint()
311 << " ,y waypoint: " << pbrh->y_waypoint()
312 << " ,z waypoint: " << pbrh->z_waypoint()
313 << " ,x ROV: " << pbrh->x_ROV()
314 << " ,y ROV: " << pbrh->y_ROV()
315 << " ,z ROV: " << pbrh->z_ROV()
316 << " ,timestamp: " << pbrh->timestamp()
317 << " ,ROV speed: " << pbrh->ROV_speed() << std::endl;
318 }
319 }
320 }
321
322
323}
324
325uint8_t UwPosBasedRt::findNextHop(const Packet* p)
326{
327 hdr_uwip *iph = HDR_UWIP(p);
328
329 std::map<uint8_t, UwPosEstimation>::iterator itROV =
330 ROV_routing.find(iph->daddr());
331 if (itROV != ROV_routing.end()) {
332 Position tempEstim = (itROV->second).getEstimatePos(NOW);
333
334 if (debug_)
335 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
336 << ")::estimated ROV position, x ROV: " << tempEstim.getX()
337 << " ,y ROV: " << tempEstim.getY()
338 << " ,z ROV: " << tempEstim.getZ() << std::endl;
339
340 double dist = nodesDistance(tempEstim,node_pos);
341 if (debug_)
342 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr << ")::" <<
343 "estimated distance between node and ROV " << dist << std::endl;
344
345 if (dist < maxTxRange) {
346 if (debug_)
347 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
348 << ")::forward directly to ROV" <<std::endl;
349 return iph->daddr();
350 } else {
351 if (debug_)
352 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
353 << ")::ROV is not in the tx range" <<std::endl;
354 }
355 }
356
357 //ROV is not in the tx range or packet is intendet for a static node
358 std::map<uint8_t, uint8_t>::const_iterator it =
359 static_routing.find(iph->daddr());
360 if (it != static_routing.end()) {
361 if (debug_)
362 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
363 << ")::routing table, entry found (dest,next hop)=("
364 << (int)it->first<<"," << (int)it->second<<")" << std::endl;
365 return it->second;
366 } else {
367 return 0;
368 }
369
370
371}
372
373
374double UwPosBasedRt::nodesDistance(Position& p1, Position& p2)
375{
376 double x1 = p1.getX();
377 double y1 = p1.getY();
378 double z1 = p1.getZ();
379 double x2 = p2.getX();
380 double y2 = p2.getY();
381 double z2 = p2.getZ();
382
383 return sqrt(pow(x2-x1,2) + pow(y2-y1,2) + pow(z2-z1,2));
384}
385
387 const uint8_t &dst, const uint8_t &next, const int toFixedNode)
388{
389 if (dst == 0) {
390 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
391 << ")::invalid IP address equal to 0" << std::endl;
392 return;
393 }
394 if (toFixedNode == 0) {
395
396 std::map<uint8_t, UwPosEstimation>::iterator itROV =
397 ROV_routing.find(dst);
398 if (itROV != ROV_routing.end()) {
399 if (debug_)
400 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
401 << ")::entry for ROV routing yet present" << std::endl;
402
403 } else {
404 if (debug_)
405 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
406 << ")::inserted entry for ROV routing" << std::endl;
407 UwPosEstimation tempEstimateROVPos;
408 ROV_routing.insert(std::pair<uint8_t,
409 UwPosEstimation>(dst,tempEstimateROVPos));
410 }
411
412 }
413 std::map<uint8_t, uint8_t>::iterator it = static_routing.find(dst);
414 if (it != static_routing.end()) { //entry alredy exist
415 it->second = next;
416 return;
417 } else {
418 static_routing.insert(std::pair<uint8_t,uint8_t>(dst,next));
419 }
420 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
421 << ")::addRoute, inserted entry (dest=" << (int)dst
422 << ",next="<< (int)next <<")" <<std::endl;
423
424
425}
426
427
428void UwPosBasedRt::setMaxTxRange(double newRange)
429{
430 if (newRange == 0) {
431 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
432 << ")::value for max transmission range not valid" << std::endl;
433 return;
434 }
435 maxTxRange = newRange;
436 if (debug_)
437 std::cout << NOW << " UwPosBasedRt(IP=" <<(int)ipAddr
438 << ")::max transmission range set to " << maxTxRange << std::endl;
439}
TclObject * create(int, const char *const *)
virtual int command(int, const char *const *)
TCL command interpreter.
double ROV_speed
Last known ROV speed.
virtual ~UwPosBasedRt()
Destructor of UwPosBasedRt class.
virtual void recv(Packet *p)
Performs the reception of packets from upper and lower layers.
virtual void setMaxTxRange(double newRange)
Set maximum transmission range.
virtual void addRoute(const uint8_t &dst, const uint8_t &next, const int toFixedNode)
Add new entry to the routing table.
double maxTxRange
Maximum transmission range, in meters, for this node.
std::map< uint8_t, uint8_t > static_routing
Routing table: destination - next hop.
virtual uint8_t findNextHop(const Packet *p)
Find next hop of a packet passed as input.
virtual void updatePosInfo(Packet *p)
update informations regardin position, if needed
Position node_pos
Position of this node.
UwPosBasedRt()
Constructor of UwPosBasedRt class.
int debug_
Flag to enable or disable dirrefent levels of debug.
std::map< uint8_t, UwPosEstimation > ROV_routing
Rouitng table for ROV.
virtual void initPkt(Packet *p)
Initialize field of hdr_uwpos_based_rt
virtual double nodesDistance(Position &p1, Position &p2)
Compute absoulute distance between 2 nodes.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
hdr_uwpos_based_rt describes packets used by UWPOSBASEDRT.
#define HDR_UWPOS_BASED_RT(p)
UwPosBasedRtModuleClass class_module_uwposbasedrt
packet_t PT_UWPOSITIONBASEDROUTING
Routing protocol for static node based on ROV position.
#define DROP_NEXT_HOP_NOT_FOUND
#define DROP_IP_NOT_SET
#define HDR_UWIP(P)
Definition uwip-module.h:58