Wiselib
|
00001 /* 00002 * Sensor.h 00003 * 00004 * Created on: Nov 17, 2010 00005 * Author: wiselib 00006 */ 00007 00008 #ifndef SENSOR_H_ 00009 #define SENSOR_H_ 00010 00011 #include "util/pstl/vector_static.h" 00012 #include "MessageDestination.h" 00013 #include <map> 00014 #include "algorithms/neighbor_discovery/echo.h" 00015 00016 00017 typedef wiselib::Echo<Os, Os::TxRadio, Os::Timer, Os::Debug> NeighborDiscovery; 00018 typedef wiselib::vector_static<wiselib::OSMODEL, nodeid_t, 10> vector_static; 00019 00020 00021 class Sensor: public MessageDestination 00022 { 00023 public: 00024 Sensor(); 00025 virtual ~Sensor(); 00026 00027 virtual error_code_t initialize(Timer::self_pointer_t timer, 00028 Radio::self_pointer_t radio, 00029 MessageQueue* mqueue, 00030 Os::Debug::self_pointer_t debug, 00031 Os::Clock::self_pointer_t clock, 00032 NeighborDiscovery& neighbor_discovery); 00033 00034 virtual error_code_t handle(TopologyMessage* msg); 00035 00036 nodeid_t cluster_head(); 00037 nodeid_t parent(); 00038 uint8_t hops(); 00039 nodeid_t cluster_id(); 00040 00041 protected: 00042 void doWork(void*); 00043 void doBlink(void*); 00044 00045 void scheduleWorkCallback(); 00046 void scheduleBlinkCallback(); 00047 00048 bool shouldAccept(Message*); 00049 uint8_t distanceToNode(nodeid_t node); 00050 void resetTopology(); 00051 bool shouldAssumeLeadership(const topology_record_t&); 00052 error_code_t handleAllMessages(); 00053 nodeid_t findLeader(); 00054 error_code_t broadcastTopology(); 00055 private: 00056 static const int BLINK_INTERVAL=1000; // every second 00057 static const int WORK_INTERVAL=10000; // every 10 seconds 00058 static const uint8_t LEADER_RADIUS=6; 00059 static const uint8_t INFINITE_DISTANCE = 0xFF; 00060 00061 Os::Clock::self_pointer_t _clock; 00062 Timer::self_pointer_t _timer; 00063 Radio::self_pointer_t _radio; 00064 Os::Debug::self_pointer_t _debug; 00065 MessageQueue* _mqueue; 00066 NeighborDiscovery * neighbor_discovery_; 00067 00068 bool _led_state; 00069 nodeid_t _id; 00070 nodeid_t _parent; 00071 bool _leader; 00072 // bool _candidate; 00073 nodeid_t leader_id; 00074 00075 void findMyNeighbors(); 00076 // Calculating new topology from information in the Messages continer 00077 void calculateNewTopology(); 00078 bool isNeighbor(nodeid_t other); 00079 vector_static myNeigbours; 00080 typedef std::map<nodeid_t, topology_record_t> topology_container_t; 00081 typedef topology_container_t::iterator topology_iterator; 00082 topology_container_t _topology; 00083 00084 // MSG Container holds the last msg recived from each node. 00085 typedef std::map<nodeid_t, TopologyMessage*> message_container_t; 00086 typedef message_container_t::iterator message_iterator; 00087 message_container_t _messages; 00088 00089 00090 }; 00091 00092 #endif /* SENSOR_H_ */