-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCtpRoutingEngineP.nc
839 lines (731 loc) · 30.6 KB
/
CtpRoutingEngineP.nc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#include <Timer.h>
#include <TreeRouting.h>
#include <CollectionDebugMsg.h>
/* $Id: CtpRoutingEngineP.nc,v 1.15 2008/06/04 04:30:41 regehr Exp $ */
/*
* "Copyright (c) 2005 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/**
* The TreeRoutingEngine is responsible for computing the routes for
* collection. It builds a set of trees rooted at specific nodes (roots) and
* maintains these trees using information provided by the link estimator on
* the quality of one hop links.
*
* <p>Each node is part of only one tree at any given time, but there is no
* difference from the node's point of view of which tree it is part. In other
* words, a message is sent towards <i>a</i> root, but which one is not
* specified. It is assumed that the roots will work together to have all data
* aggregated later if need be. The tree routing engine's responsibility is
* for each node to find the path with the least number of transmissions to
* any one root.
*
* <p>The tree is proactively maintained by periodic beacons sent by each
* node. These beacons are jittered in time to prevent synchronizations in the
* network. All nodes maintain the same <i>average</i> beacon sending rate
* (defined by BEACON_INTERVAL +- 50%). The beacon contains the node's parent,
* the current hopcount, and the cumulative path quality metric. The metric is
* defined as the parent's metric plus the bidirectional quality of the link
* between the current node and its parent. The metric represents the
* expected number of transmissions along the path to the root, and is 0 by
* definition at the root.
*
* <p>Every time a node receives an update from a neighbor it records the
* information if the node is part of the neighbor table. The neighbor table
* keeps the best candidates for being parents i.e., the nodes with the best
* path metric. The neighbor table does not store the full path metric,
* though. It stores the parent's path metric, and the link quality to the
* parent is only added when the information is needed: (i) when choosing a
* parent and (ii) when choosing a route. The nodes in the neighbor table are
* a subset of the nodes in the link estimator table, as a node is not
* admitted in the neighbor table with an estimate of infinity.
*
* <p>There are two uses for the neighbor table, as mentioned above. The first
* one is to select a parent. The parent is just the neighbor with the best
* path metric. It serves to define the node's own path metric and hopcount,
* and the set of child-parent links is what defines the tree. In a sense the
* tree is defined to form a coherent propagation substrate for the path
* metrics. The parent is (re)-selected periodically, immediately before a
* node sends its own beacon, in the updateRouteTask.
*
* <p>The second use is to actually choose a next hop towards any root at
* message forwarding time. This need not be the current parent, even though
* it is currently implemented as such.
*
* <p>The operation of the routing engine has two main tasks and one main
* event: updateRouteTask is called periodically and chooses a new parent;
* sendBeaconTask broadcasts the current route information to the neighbors.
* The main event is the receiving of a neighbor's beacon, which updates the
* neighbor table.
*
* <p> The interface with the ForwardingEngine occurs through the nextHop()
* call.
*
* <p> Any node can become a root, and routed messages from a subset of the
* network will be routed towards it. The RootControl interface allows
* setting, unsetting, and querying the root state of a node. By convention,
* when a node is root its hopcount and metric are 0, and the parent is
* itself. A root always has a valid route, to itself.
*
* @author Rodrigo Fonseca
* @author Philip Levis (added trickle-like updates)
* Acknowledgment: based on MintRoute, MultiHopLQI, BVR tree construction, Berkeley's MTree
*
* @date $Date: 2008/06/04 04:30:41 $
* @see Net2-WG
*/
/*just test*/
generic module CtpRoutingEngineP(uint8_t routingTableSize, uint32_t minInterval, uint32_t maxInterval) {
provides {
interface UnicastNameFreeRouting as Routing;
interface RootControl;
interface CtpInfo;
interface StdControl;
interface CtpRoutingPacket;
interface Init;
}
uses {
interface AMSend as BeaconSend;
interface Receive as BeaconReceive;
interface LinkEstimator;
interface AMPacket;
interface SplitControl as RadioControl;
interface Timer<TMilli> as BeaconTimer;
interface Timer<TMilli> as RouteTimer;
interface Random;
interface CollectionDebug;
interface CtpCongestion;
interface CompareBit;
}
}
implementation {
bool ECNOff = TRUE;
/* Keeps track of whether the radio is on. No sense updating or sending
* beacons if radio is off */
bool radioOn = FALSE;
/* Controls whether the node's periodic timer will fire. The node will not
* send any beacon, and will not update the route. Start and stop control this. */
bool running = FALSE;
/* Guards the beacon buffer: only one beacon being sent at a time */
bool sending = FALSE;
/* Tells updateNeighbor that the parent was just evicted.*/
bool justEvicted = FALSE;
route_info_t routeInfo;
bool state_is_root;
am_addr_t my_ll_addr;
message_t beaconMsgBuffer;
ctp_routing_header_t* beaconMsg;
/* routing table -- routing info about neighbors */
routing_table_entry routingTable[routingTableSize];
uint8_t routingTableActive;
/* statistics */
uint32_t parentChanges;
/* end statistics */
uint32_t routeUpdateTimerCount;
// Maximimum it takes to hear four beacons
enum {
DEATH_TEST_INTERVAL = (maxInterval * 4) / (BEACON_INTERVAL / 1024),
};
// forward declarations
void routingTableInit();
uint8_t routingTableFind(am_addr_t);
error_t routingTableUpdateEntry(am_addr_t, am_addr_t , uint16_t);
error_t routingTableEvict(am_addr_t neighbor);
uint32_t currentInterval = minInterval;
uint32_t t;
bool tHasPassed;
void chooseAdvertiseTime() {
t = currentInterval;
t /= 2;
t += call Random.rand32() % t;
tHasPassed = FALSE;
call BeaconTimer.stop();
call BeaconTimer.startOneShot(t);
}
void resetInterval() {
currentInterval = minInterval;
chooseAdvertiseTime();
}
void decayInterval() {
currentInterval *= 2;
if (currentInterval > maxInterval) {
currentInterval = maxInterval;
}
chooseAdvertiseTime();
}
void remainingInterval() {
uint32_t remaining = currentInterval;
remaining -= t;
tHasPassed = TRUE;
call BeaconTimer.startOneShot(remaining);
}
command error_t Init.init() {
uint8_t maxLength;
routeUpdateTimerCount = 0;
radioOn = FALSE;
running = FALSE;
parentChanges = 0;
state_is_root = 0;
routeInfoInit(&routeInfo);
routingTableInit();
my_ll_addr = call AMPacket.address();
beaconMsg = call BeaconSend.getPayload(&beaconMsgBuffer, call BeaconSend.maxPayloadLength());
maxLength = call BeaconSend.maxPayloadLength();
dbg("TreeRoutingCtl","TreeRouting initialized. (used payload:%d max payload:%d!\n",
sizeof(beaconMsg), maxLength);
return SUCCESS;
}
command error_t StdControl.start() {
//start will (re)start the sending of messages
if (!running) {
running = TRUE;
resetInterval();
call RouteTimer.startPeriodic(BEACON_INTERVAL);
dbg("TreeRoutingCtl","%s running: %d radioOn: %d\n", __FUNCTION__, running, radioOn);
}
return SUCCESS;
}
command error_t StdControl.stop() {
running = FALSE;
dbg("TreeRoutingCtl","%s running: %d radioOn: %d\n", __FUNCTION__, running, radioOn);
return SUCCESS;
}
event void RadioControl.startDone(error_t error) {
radioOn = TRUE;
dbg("TreeRoutingCtl","%s running: %d radioOn: %d\n", __FUNCTION__, running, radioOn);
if (running) {
uint16_t nextInt;
nextInt = call Random.rand16() % BEACON_INTERVAL;
nextInt += BEACON_INTERVAL >> 1;
call BeaconTimer.startOneShot(nextInt);
}
}
event void RadioControl.stopDone(error_t error) {
radioOn = FALSE;
dbg("TreeRoutingCtl","%s running: %d radioOn: %d\n", __FUNCTION__, running, radioOn);
}
/* Is this quality measure better than the minimum threshold? */
// Implemented assuming quality is EETX
bool passLinkEtxThreshold(uint16_t etx) {
return TRUE;
return (etx < ETX_THRESHOLD);
}
/* Converts the output of the link estimator to path metric
* units, that can be *added* to form path metric measures */
uint16_t evaluateEtx(uint16_t quality) {
//dbg("TreeRouting","%s %d -> %d\n",__FUNCTION__,quality, quality+10);
return (quality + 10);
}
/* updates the routing information, using the info that has been received
* from neighbor beacons. Two things can cause this info to change:
* neighbor beacons, changes in link estimates, including neighbor eviction */
task void updateRouteTask() {
uint8_t i;
routing_table_entry* entry;
routing_table_entry* best;
uint16_t minEtx;
uint16_t currentEtx;
uint16_t linkEtx, pathEtx;
if (state_is_root)
return;
best = NULL;
/* Minimum etx found among neighbors, initially infinity */
minEtx = MAX_METRIC;
/* Metric through current parent, initially infinity */
currentEtx = MAX_METRIC;
dbg("TreeRouting","%s\n",__FUNCTION__);
/* Find best path in table, other than our current */
for (i = 0; i < routingTableActive; i++) {
entry = &routingTable[i];
// Avoid bad entries and 1-hop loops
if (entry->info.parent == INVALID_ADDR || entry->info.parent == my_ll_addr) {
dbg("TreeRouting",
"routingTable[%d]: neighbor: [id: %d parent: %d etx: NO ROUTE]\n",
i, entry->neighbor, entry->info.parent);
continue;
}
/* Compute this neighbor's path metric */
linkEtx = evaluateEtx(call LinkEstimator.getLinkQuality(entry->neighbor));
dbg("TreeRouting",
"routingTable[%d]: neighbor: [id: %d parent: %d etx: %d]\n",
i, entry->neighbor, entry->info.parent, linkEtx);
pathEtx = linkEtx + entry->info.etx;
/* Operations specific to the current parent */
if (entry->neighbor == routeInfo.parent) {
dbg("TreeRouting", " already parent.\n");
currentEtx = pathEtx;
/* update routeInfo with parent's current info */
atomic {
routeInfo.etx = entry->info.etx;
routeInfo.congested = entry->info.congested;
}
continue;
}
/* Ignore links that are congested */
if (entry->info.congested)
continue;
/* Ignore links that are bad */
if (!passLinkEtxThreshold(linkEtx)) {
dbg("TreeRouting", " did not pass threshold.\n");
continue;
}
if (pathEtx < minEtx) {
minEtx = pathEtx;
best = entry;
}
}
//call CollectionDebug.logEventDbg(NET_C_DBG_3, routeInfo.parent, currentEtx, minEtx);
/* Now choose between the current parent and the best neighbor */
/* Requires that:
1. at least another neighbor was found with ok quality and not congested
2. the current parent is congested and the other best route is at least as good
3. or the current parent is not congested and the neighbor quality is better by
the PARENT_SWITCH_THRESHOLD.
Note: if our parent is congested, in order to avoid forming loops, we try to select
a node which is not a descendent of our parent. routeInfo.ext is our parent's
etx. Any descendent will be at least that + 10 (1 hop), so we restrict the
selection to be less than that.
*/
if (minEtx != MAX_METRIC) {
if (currentEtx == MAX_METRIC ||
(routeInfo.congested && (minEtx < (routeInfo.etx + 10))) ||
minEtx + PARENT_SWITCH_THRESHOLD < currentEtx) {
// routeInfo.metric will not store the composed metric.
// since the linkMetric may change, we will compose whenever
// we need it: i. when choosing a parent (here);
// ii. when choosing a next hop
parentChanges++;
dbg("TreeRouting","Changed parent. from %d to %d\n", routeInfo.parent, best->neighbor);
call CollectionDebug.logEventDbg(NET_C_TREE_NEW_PARENT, best->neighbor, best->info.etx, minEtx);
call LinkEstimator.unpinNeighbor(routeInfo.parent);
call LinkEstimator.pinNeighbor(best->neighbor);
call LinkEstimator.clearDLQ(best->neighbor);
atomic {
routeInfo.parent = best->neighbor;
routeInfo.etx = best->info.etx;
routeInfo.congested = best->info.congested;
}
}
}
/* Finally, tell people what happened: */
/* We can only loose a route to a parent if it has been evicted. If it hasn't
* been just evicted then we already did not have a route */
if (justEvicted && routeInfo.parent == INVALID_ADDR)
signal Routing.noRoute();
/* On the other hand, if we didn't have a parent (no currentEtx) and now we
* do, then we signal route found. The exception is if we just evicted the
* parent and immediately found a replacement route: we don't signal in this
* case */
else if (!justEvicted &&
currentEtx == MAX_METRIC &&
minEtx != MAX_METRIC)
signal Routing.routeFound();
justEvicted = FALSE;
}
/* send a beacon advertising this node's routeInfo */
// only posted if running and radioOn
task void sendBeaconTask() {
error_t eval;
if (sending) {
return;
}
beaconMsg->options = 0;
/* Congestion notification: am I congested? */
if (call CtpCongestion.isCongested()) {
beaconMsg->options |= CTP_OPT_ECN;
}
beaconMsg->parent = routeInfo.parent;
if (state_is_root) {
beaconMsg->etx = routeInfo.etx;
}
else if (routeInfo.parent == INVALID_ADDR) {
beaconMsg->etx = routeInfo.etx;
beaconMsg->options |= CTP_OPT_PULL;
} else {
beaconMsg->etx = routeInfo.etx +
evaluateEtx(call LinkEstimator.getLinkQuality(routeInfo.parent));
}
dbg("TreeRouting", "%s parent: %d etx: %d\n",
__FUNCTION__,
beaconMsg->parent,
beaconMsg->etx);
call CollectionDebug.logEventRoute(NET_C_TREE_SENT_BEACON, beaconMsg->parent, 0, beaconMsg->etx);
eval = call BeaconSend.send(AM_BROADCAST_ADDR,
&beaconMsgBuffer,
sizeof(ctp_routing_header_t));
if (eval == SUCCESS) {
sending = TRUE;
} else if (eval == EOFF) {
radioOn = FALSE;
dbg("TreeRoutingCtl","%s running: %d radioOn: %d\n", __FUNCTION__, running, radioOn);
}
}
event void BeaconSend.sendDone(message_t* msg, error_t error) {
if ((msg != &beaconMsgBuffer) || !sending) {
//something smells bad around here
return;
}
sending = FALSE;
}
event void RouteTimer.fired() {
if (radioOn && running) {
post updateRouteTask();
}
}
event void BeaconTimer.fired() {
if (radioOn && running) {
if (!tHasPassed) {
post updateRouteTask(); //always send the most up to date info
post sendBeaconTask();
dbg("RoutingTimer", "Beacon timer fired at %s\n", sim_time_string());
remainingInterval();
}
else {
decayInterval();
}
}
}
ctp_routing_header_t* getHeader(message_t* ONE m) {
return (ctp_routing_header_t*)call BeaconSend.getPayload(m, call BeaconSend.maxPayloadLength());
}
/* Handle the receiving of beacon messages from the neighbors. We update the
* table, but wait for the next route update to choose a new parent */
event message_t* BeaconReceive.receive(message_t* msg, void* payload, uint8_t len) {
am_addr_t from;
ctp_routing_header_t* rcvBeacon;
bool congested;
// Received a beacon, but it's not from us.
if (len != sizeof(ctp_routing_header_t)) {
dbg("LITest", "%s, received beacon of size %hhu, expected %i\n",
__FUNCTION__,
len,
(int)sizeof(ctp_routing_header_t));
return msg;
}
//need to get the am_addr_t of the source
from = call AMPacket.source(msg);
rcvBeacon = (ctp_routing_header_t*)payload;
congested = call CtpRoutingPacket.getOption(msg, CTP_OPT_ECN);
dbg("TreeRouting","%s from: %d [ parent: %d etx: %d]\n",
__FUNCTION__, from,
rcvBeacon->parent, rcvBeacon->etx);
//update neighbor table
if (rcvBeacon->parent != INVALID_ADDR) {
/* If this node is a root, request a forced insert in the link
* estimator table and pin the node. */
if (rcvBeacon->etx == 0) {
dbg("TreeRouting","from a root, inserting if not in table\n");
call LinkEstimator.insertNeighbor(from);
call LinkEstimator.pinNeighbor(from);
}
//TODO: also, if better than my current parent's path etx, insert
routingTableUpdateEntry(from, rcvBeacon->parent, rcvBeacon->etx);
call CtpInfo.setNeighborCongested(from, congested);
}
if (call CtpRoutingPacket.getOption(msg, CTP_OPT_PULL)) {
resetInterval();
}
return msg;
}
/* Signals that a neighbor is no longer reachable. need special care if
* that neighbor is our parent */
event void LinkEstimator.evicted(am_addr_t neighbor) {
routingTableEvict(neighbor);
dbg("TreeRouting","%s\n",__FUNCTION__);
if (routeInfo.parent == neighbor) {
routeInfoInit(&routeInfo);
justEvicted = TRUE;
post updateRouteTask();
}
}
/* Interface UnicastNameFreeRouting */
/* Simple implementation: return the current routeInfo */
command am_addr_t Routing.nextHop() {
return routeInfo.parent;
}
command bool Routing.hasRoute() {
return (routeInfo.parent != INVALID_ADDR);
}
/* CtpInfo interface */
command error_t CtpInfo.getParent(am_addr_t* parent) {
if (parent == NULL)
return FAIL;
if (routeInfo.parent == INVALID_ADDR)
return FAIL;
*parent = routeInfo.parent;
return SUCCESS;
}
command error_t CtpInfo.getEtx(uint16_t* etx) {
if (etx == NULL)
return FAIL;
if (routeInfo.parent == INVALID_ADDR)
return FAIL;
if (state_is_root == 1) {
*etx = 0;
} else {
// path etx = etx(parent) + etx(link to the parent)
*etx = routeInfo.etx + evaluateEtx(call LinkEstimator.getLinkQuality(routeInfo.parent));
}
return SUCCESS;
}
command void CtpInfo.recomputeRoutes() {
post updateRouteTask();
}
command void CtpInfo.triggerRouteUpdate() {
resetInterval();
}
command void CtpInfo.triggerImmediateRouteUpdate() {
resetInterval();
}
command void CtpInfo.setNeighborCongested(am_addr_t n, bool congested) {
uint8_t idx;
if (ECNOff)
return;
idx = routingTableFind(n);
if (idx < routingTableActive) {
routingTable[idx].info.congested = congested;
}
if (routeInfo.congested && !congested)
post updateRouteTask();
else if (routeInfo.parent == n && congested)
post updateRouteTask();
}
command bool CtpInfo.isNeighborCongested(am_addr_t n) {
uint8_t idx;
if (ECNOff)
return FALSE;
idx = routingTableFind(n);
if (idx < routingTableActive) {
return routingTable[idx].info.congested;
}
return FALSE;
}
/* RootControl interface */
/** sets the current node as a root, if not already a root */
/* returns FAIL if it's not possible for some reason */
command error_t RootControl.setRoot() {
bool route_found = FALSE;
route_found = (routeInfo.parent == INVALID_ADDR);
atomic {
state_is_root = 1;
routeInfo.parent = my_ll_addr; //myself
routeInfo.etx = 0;
}
if (route_found)
signal Routing.routeFound();
dbg("TreeRouting","%s I'm a root now!\n",__FUNCTION__);
call CollectionDebug.logEventRoute(NET_C_TREE_NEW_PARENT, routeInfo.parent, 0, routeInfo.etx);
return SUCCESS;
}
command error_t RootControl.unsetRoot() {
atomic {
state_is_root = 0;
routeInfoInit(&routeInfo);
}
dbg("TreeRouting","%s I'm not a root now!\n",__FUNCTION__);
post updateRouteTask();
return SUCCESS;
}
command bool RootControl.isRoot() {
return state_is_root;
}
default event void Routing.noRoute() {
}
default event void Routing.routeFound() {
}
/* This should see if the node should be inserted in the table.
* If the white_bit is set, this means the LL believes this is a good
* first hop link.
* The link will be recommended for insertion if it is better* than some
* link in the routing table that is not our parent.
* We are comparing the path quality up to the node, and ignoring the link
* quality from us to the node. This is because of a couple of things:
* 1. because of the white bit, we assume that the 1-hop to the candidate
* link is good (say, etx=1)
* 2. we are being optimistic to the nodes in the table, by ignoring the
* 1-hop quality to them (which means we are assuming it's 1 as well)
* This actually sets the bar a little higher for replacement
* 3. this is faster
* 4. it doesn't require the link estimator to have stabilized on a link
*/
event bool CompareBit.shouldInsert(message_t *msg, void* payload, uint8_t len, bool white_bit) {
bool found = FALSE;
uint16_t pathEtx;
//uint16_t linkEtx = evaluateEtx(0);
uint16_t neighEtx;
int i;
routing_table_entry* entry;
ctp_routing_header_t* rcvBeacon;
if ((call AMPacket.type(msg) != AM_CTP_ROUTING) ||
(len != sizeof(ctp_routing_header_t)))
return FALSE;
/* 1.determine this packet's path quality */
rcvBeacon = (ctp_routing_header_t*)payload;
if (rcvBeacon->parent == INVALID_ADDR)
return FALSE;
/* the node is a root, recommend insertion! */
if (rcvBeacon->etx == 0) {
return TRUE;
}
pathEtx = rcvBeacon->etx; // + linkEtx;
/* 2. see if we find some neighbor that is worse */
for (i = 0; i < routingTableActive && !found; i++) {
entry = &routingTable[i];
//ignore parent, since we can't replace it
if (entry->neighbor == routeInfo.parent)
continue;
neighEtx = entry->info.etx;
//neighEtx = evaluateEtx(call LinkEstimator.getLinkQuality(entry->neighbor));
found |= (pathEtx < neighEtx);
}
return found;
}
/************************************************************/
/* Routing Table Functions */
/* The routing table keeps info about neighbor's route_info,
* and is used when choosing a parent.
* The table is simple:
* - not fragmented (all entries in 0..routingTableActive)
* - not ordered
* - no replacement: eviction follows the LinkEstimator table
*/
void routingTableInit() {
routingTableActive = 0;
}
/* Returns the index of parent in the table or
* routingTableActive if not found */
uint8_t routingTableFind(am_addr_t neighbor) {
uint8_t i;
if (neighbor == INVALID_ADDR)
return routingTableActive;
for (i = 0; i < routingTableActive; i++) {
if (routingTable[i].neighbor == neighbor)
break;
}
return i;
}
error_t routingTableUpdateEntry(am_addr_t from, am_addr_t parent, uint16_t etx) {
uint8_t idx;
uint16_t linkEtx;
linkEtx = evaluateEtx(call LinkEstimator.getLinkQuality(from));
idx = routingTableFind(from);
if (idx == routingTableSize) {
//not found and table is full
//if (passLinkEtxThreshold(linkEtx))
//TODO: add replacement here, replace the worst
//}
dbg("TreeRouting", "%s FAIL, table full\n", __FUNCTION__);
return FAIL;
}
else if (idx == routingTableActive) {
//not found and there is space
if (passLinkEtxThreshold(linkEtx)) {
atomic {
routingTable[idx].neighbor = from;
routingTable[idx].info.parent = parent;
routingTable[idx].info.etx = etx;
routingTable[idx].info.haveHeard = 1;
routingTable[idx].info.congested = FALSE;
routingTableActive++;
}
dbg("TreeRouting", "%s OK, new entry\n", __FUNCTION__);
} else {
dbg("TreeRouting", "%s Fail, link quality (%hu) below threshold\n", __FUNCTION__, linkEtx);
}
} else {
//found, just update
atomic {
routingTable[idx].neighbor = from;
routingTable[idx].info.parent = parent;
routingTable[idx].info.etx = etx;
routingTable[idx].info.haveHeard = 1;
}
dbg("TreeRouting", "%s OK, updated entry\n", __FUNCTION__);
}
return SUCCESS;
}
/* if this gets expensive, introduce indirection through an array of pointers */
error_t routingTableEvict(am_addr_t neighbor) {
uint8_t idx,i;
idx = routingTableFind(neighbor);
if (idx == routingTableActive)
return FAIL;
routingTableActive--;
for (i = idx; i < routingTableActive; i++) {
routingTable[i] = routingTable[i+1];
}
return SUCCESS;
}
/*********** end routing table functions ***************/
/* Default implementations for CollectionDebug calls.
* These allow CollectionDebug not to be wired to anything if debugging
* is not desired. */
default command error_t CollectionDebug.logEvent(uint8_t type) {
return SUCCESS;
}
default command error_t CollectionDebug.logEventSimple(uint8_t type, uint16_t arg) {
return SUCCESS;
}
default command error_t CollectionDebug.logEventDbg(uint8_t type, uint16_t arg1, uint16_t arg2, uint16_t arg3) {
return SUCCESS;
}
default command error_t CollectionDebug.logEventMsg(uint8_t type, uint16_t msg, am_addr_t origin, am_addr_t node) {
return SUCCESS;
}
default command error_t CollectionDebug.logEventRoute(uint8_t type, am_addr_t parent, uint8_t hopcount, uint16_t etx) {
return SUCCESS;
}
command bool CtpRoutingPacket.getOption(message_t* msg, ctp_options_t opt) {
return ((getHeader(msg)->options & opt) == opt) ? TRUE : FALSE;
}
command void CtpRoutingPacket.setOption(message_t* msg, ctp_options_t opt) {
getHeader(msg)->options |= opt;
}
command void CtpRoutingPacket.clearOption(message_t* msg, ctp_options_t opt) {
getHeader(msg)->options &= ~opt;
}
command void CtpRoutingPacket.clearOptions(message_t* msg) {
getHeader(msg)->options = 0;
}
command am_addr_t CtpRoutingPacket.getParent(message_t* msg) {
return getHeader(msg)->parent;
}
command void CtpRoutingPacket.setParent(message_t* msg, am_addr_t addr) {
getHeader(msg)->parent = addr;
}
command uint16_t CtpRoutingPacket.getEtx(message_t* msg) {
return getHeader(msg)->etx;
}
command void CtpRoutingPacket.setEtx(message_t* msg, uint8_t etx) {
getHeader(msg)->etx = etx;
}
command uint8_t CtpInfo.numNeighbors() {
return routingTableActive;
}
command uint16_t CtpInfo.getNeighborLinkQuality(uint8_t n) {
return (n < routingTableActive)? call LinkEstimator.getLinkQuality(routingTable[n].neighbor):0xffff;
}
command uint16_t CtpInfo.getNeighborRouteQuality(uint8_t n) {
return (n < routingTableActive)? call LinkEstimator.getLinkQuality(routingTable[n].neighbor) + routingTable[n].info.etx:0xfffff;
}
command am_addr_t CtpInfo.getNeighborAddr(uint8_t n) {
return (n < routingTableActive)? routingTable[n].neighbor:AM_BROADCAST_ADDR;
}
}