00001 #ifndef _NODEDESCR_H__
00002 #define _NODEDESCR_H__
00003
00004 #include <cassert>
00005
00006
00007 #include <stdint.h>
00008
00009 #include <gea/API.h>
00010 #include <gea/Time.h>
00011
00012 #include <awds/BasePacket.h>
00013 #include <awds/NodeId.h>
00014 #include <awds/beacon.h>
00015 #include <awds/settings.h>
00016
00017 #include <iostream>
00018
00019
00020 namespace awds {
00021 struct NodeDescr {
00022
00023 static bool verbose;
00024
00025 NodeId id;
00026 BasePacket *lastBeacon;
00027 gea::AbsTime lastBeaconTime;
00028 gea::Duration beaconInterval;
00029 gea::AbsTime linkValidity;
00030
00031 u_int32_t beaconHist;
00032 bool active;
00033 bool mpr;
00034
00042 bool updateActive() {
00043
00044 linkValidity = lastBeaconTime + gea::Duration( beaconInterval * NR_BEACON_TRIGGER_FAIL );
00045 const bool last12received = (beaconHist > 0xFFF00000UL);
00046
00047 if ( !active && last12received) {
00048 active = true;
00049 if (verbose) {
00050 GEA.dbg() << "neighbor "
00051 << Beacon(*lastBeacon).getSrc() << " became active" << std::endl;
00052 }
00053 return true;
00054 } else
00055 return false;
00056 }
00057
00058
00068 bool updateInactive() {
00069
00070 const bool lastBeacon2old = (linkValidity <= GEA.lastEventTime);
00071
00072 if ( active && lastBeacon2old ) {
00073 active = false;
00074 if (verbose) {
00075 GEA.dbg() << "neighbor " << Beacon(*lastBeacon).getSrc()
00076 << " became inactive (last beacon too old)"
00077 << std::endl;
00078 }
00079 return true;
00080 } else
00081 return false;
00082 }
00083
00084 bool isGood() {
00085 return active;
00086 }
00087
00088
00092 bool isBidiGood(const NodeId& myId) {
00093 if (!lastBeacon) {
00094 return false;
00095 }
00096 Beacon beacon(*lastBeacon);
00097 if (!beacon.hasNeigh(myId)) {
00098 return false;
00099 }
00100 return isGood();
00101 }
00102
00103 bool isExpired() {
00104 return !active && (lastBeaconTime + (beaconInterval * 32)
00105 < GEA.lastEventTime);
00106 }
00107
00108 NodeDescr() : beaconInterval(0.) {}
00109
00110 void init(const NodeId& _id, BasePacket *p, gea::AbsTime t) {
00111 active = false;
00112 mpr = true;
00113 beaconHist = 0;
00114 this->id = _id;
00115 lastBeacon = p;
00116 lastBeaconTime = t;
00117 }
00118
00119
00120
00121
00122 unsigned char quality() const {
00123 uint32_t x = beaconHist;
00124
00125 unsigned char n = 0;
00126
00127
00128
00129
00130 if (x) {
00131 do {
00132 ++n;
00133 } while (0 != (x = x&(x-1)));
00134 }
00135
00136 assert(n > 0);
00137 return n;
00138 }
00139 };
00140 }
00141
00142
00143 #endif //NODEDESCR_H__
00144
00145
00146
00147
00148
00149