Wiselib
|
00001 /* 00002 * BGU_Sensor.h 00003 * 00004 * Created on: Dec 1, 2010 00005 * Author: Guy Leshem 00006 */ 00007 00008 #ifndef SENSOR_H_ 00009 #define SENSOR_H_ 00010 00011 #define SENSOR_DEBUG 00012 #define SENSOR_MSG_RECV_DEBUG 00013 #define VISOR_DEBUG 00014 #define DEBUG 00015 00016 00017 #include "algorithms/bgu_clustering/MessageDestination.h" 00018 #include "algorithms/bgu_clustering/TopologyMessage.h" 00019 #include "algorithms/bgu_clustering/MessageQueue.h" 00020 #include "algorithms/neighbor_discovery/echo.h" 00021 #include "algorithms/cluster/clustering_types.h" 00022 #include "util/pstl/vector_static.h" 00023 #include "util/pstl/priority_queue.h" 00024 #include "util/pstl/pair.h" 00025 #include "util/pstl/map_static_vector.h" 00026 #include "internal_interface/routing_table/routing_table_static_array.h" 00027 #include "util/delegates/delegate.hpp" 00028 #include "algorithms/cluster/clustering_types.h" 00029 00030 namespace wiselib 00031 { 00037 template< typename OsModel_P, 00038 typename Radio_P = typename OsModel_P::Radio, 00039 typename Timer_P = typename OsModel_P::Timer, 00040 typename Clock_P = typename OsModel_P::Clock, 00041 typename Debug_P = typename OsModel_P::Debug> 00042 class Sensor : public MessageDestination 00043 { 00044 public: 00045 // Type definitions. 00046 // Type definition of the used Templates. 00047 typedef OsModel_P OsModel; 00048 typedef Radio_P Radio; 00049 typedef Timer_P Timer; 00050 typedef Clock_P Clock; 00051 typedef Debug_P Debug; 00052 typedef Sensor<OsModel, Radio, Timer, Clock, Debug> self_type; 00053 typedef wiselib::Echo<Os, Os::TxRadio, Os::Timer, Os::Debug> NeighborDiscovery; 00054 typedef wiselib::vector_static<wiselib::OSMODEL, nodeid_t, 10> vector_static; 00055 typedef self_type* self_pointer_t; 00056 typedef delegate1<void, int> cluster_delegate_t; 00057 // Basic types definition. 00058 typedef typename Radio::node_id_t node_id_t; 00059 typedef typename Radio::size_t size_t; 00060 typedef typename Radio::block_data_t block_data_t; 00061 typedef typename Timer::millis_t millis_t; 00062 00063 // -------------------------------------------------------------------- 00064 // Public method declaration. | 00065 // -------------------------------------------------------------------- 00066 00069 Sensor(){ } 00070 00073 ~Sensor(); 00074 00086 error_code_t init(typename Timer::self_pointer_t timer, 00087 typename Radio::self_pointer_t radio, 00088 MessageQueue* mqueue, 00089 Os::Debug::self_pointer_t debug, 00090 Os::Clock::self_pointer_t clock, 00091 NeighborDiscovery& neighbor_discovery); 00092 00099 error_code_t handle(TopologyMessage* msg); 00100 00105 nodeid_t cluster_head(); 00106 00110 nodeid_t parent(); 00111 00115 uint8_t hops(); 00116 00120 nodeid_t cluster_id(); 00121 00122 // Callbacks. each iteration there will be sent a call back - 0 - something changed. 00127 template<class T, void(T::*TMethod)(int) > 00128 inline int reg_changed_callback(T *obj_pnt) { 00129 state_changed_callback_ = cluster_delegate_t::from_method<T, TMethod > (obj_pnt); 00130 return state_changed_callback_; 00131 } 00132 00136 void unreg_changed_callback(int idx) { 00137 state_changed_callback_ = cluster_delegate_t(); 00138 } 00139 00140 protected: 00144 void doWork(void*); 00148 void doBlink(void*); 00149 00156 bool shouldAccept(Message* msg); 00157 00160 void resetTopology(); 00161 00169 bool shouldAssumeLeadership(const topology_record_t& rec); 00170 00175 error_code_t handleAllMessages(); 00176 00182 nodeid_t findLeader(); 00183 00188 error_code_t broadcastTopology(); 00189 00197 uint8_t distanceToNode(nodeid_t node); 00198 00201 void findMyNeighbors(); 00202 00205 void scheduleWorkCallback() 00206 { 00207 _timer->template set_timer<self_type, &self_type::doWork > (WORK_INTERVAL, this, NULL); 00208 } 00209 00212 void scheduleBlinkCallback() 00213 { 00214 _timer->template set_timer<self_type, &self_type::doBlink > (BLINK_INTERVAL, this, NULL); 00215 } 00216 00219 void calculateNewTopology(); 00220 00227 bool isNeighbor(nodeid_t other); 00228 00229 00230 private: 00231 static const int BLINK_INTERVAL=1000; //Time (in ms) between changing the state of the sensor's LED 00232 static const int WORK_INTERVAL=10000; //Time (in ms) between updating the local topology information 00233 static const uint8_t LEADER_RADIUS=6; //Distance (in 'hops') where a leader for this node may be elected 00234 static const uint8_t INFINITE_DISTANCE = 0xFF; //@see distanceToNode 00235 static const size_t MAX_TOPOLOGY_SIZE = 10; //The max. number of nodes in the local topology information 00236 static const size_t MAX_MSG_QUEUE_SIZE = 10; //the max. number of messages in the message queue. 00237 00238 cluster_delegate_t state_changed_callback_; 00239 Os::Clock::self_pointer_t _clock; 00240 typename Timer::self_pointer_t _timer; 00241 typename Radio::self_pointer_t _radio; 00242 Os::Debug::self_pointer_t _debug; 00243 MessageQueue* _mqueue; 00244 NeighborDiscovery * neighbor_discovery_; 00245 00246 bool _led_state; 00247 nodeid_t _id; 00248 nodeid_t _parent; 00249 bool _leader; 00250 nodeid_t leader_id; 00251 uint8_t randomNum; 00252 00253 vector_static myNeigbours; 00254 typedef wiselib::MapStaticVector <Os, nodeid_t, topology_record_t, MAX_TOPOLOGY_SIZE> topology_container_t; 00255 typedef typename topology_container_t::iterator topology_iterator; 00256 topology_container_t _topology; 00257 00258 // MSG Container holds the last msg recived from each node. 00259 typedef wiselib::MapStaticVector <Os, nodeid_t, TopologyMessage*, MAX_MSG_QUEUE_SIZE> message_container_t; 00260 typedef typename message_container_t::iterator message_iterator; 00261 message_container_t _messages; 00262 }; 00263 00264 template<typename T1, typename T2> 00265 wiselib::pair<T1, T2> 00266 make_pair(const T1& first, const T2& second) 00267 { 00268 return wiselib::pair<T1, T2>(first, second); 00269 } 00270 00271 00272 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00273 error_code_t 00274 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::init(typename Sensor::Timer::self_pointer_t timer, typename Sensor::Radio::self_pointer_t radio, 00275 MessageQueue* mqueue, 00276 Os::Debug::self_pointer_t debug, 00277 wiselib::OSMODEL::Clock::self_pointer_t clock, 00278 NeighborDiscovery& neighbor_discovery) 00279 { 00280 00281 _timer = timer; 00282 _radio = radio; 00283 _mqueue = mqueue; 00284 _debug = debug; 00285 _clock = clock; 00286 _id = _radio->id(); 00287 neighbor_discovery_ = &neighbor_discovery; 00288 neighbor_discovery_->init( *_radio, *_clock, *_timer, *_debug ); 00289 neighbor_discovery_->enable(); 00290 scheduleBlinkCallback(); 00291 scheduleWorkCallback(); 00292 randomNum = 0; 00293 leader_id=-1; 00294 return ecSuccess; 00295 } 00296 00297 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00298 error_code_t 00299 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::handle(TopologyMessage* msg) 00300 { 00301 message_iterator existing_message_it = _messages.find(msg->senderId()); 00302 if (existing_message_it != _messages.end()) 00303 { 00304 TopologyMessage* existing_message = existing_message_it->second; 00305 _messages.erase(msg->senderId()); 00306 delete existing_message; 00307 } 00308 // _parent=msg->senderId(); 00309 _messages.insert(make_pair(msg->senderId(),msg)); 00310 00311 return ecSuccess; 00312 } 00313 00314 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00315 void 00316 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::calculateNewTopology() 00317 { 00318 resetTopology(); 00319 00320 for (message_iterator it_m = _messages.begin(); it_m != _messages.end(); ++it_m) 00321 { 00322 TopologyMessage* msg = (it_m->second); 00323 for (TopologyMessage::iterator it = msg->begin(); it != msg->end(); ++it) 00324 { 00325 topology_record_t rec = *it; 00326 rec.distance += 1; //add the distance to the originator 00327 if (rec.distance > LEADER_RADIUS) 00328 { 00329 continue;//this node is too far away 00330 } 00331 00332 if (distanceToNode(rec.nodeid) > rec.distance) 00333 { 00334 //printf("distance to node is greater than current distance!\n"); 00335 00336 00337 topology_iterator temp_it = _topology.find(rec.nodeid); 00338 temp_it->second.distance=rec.distance; 00339 00340 topology_record_t& self_record = _topology.find(_radio->id())->second; 00341 topology_record_t& sender_record = _topology.find(msg->senderId())->second; 00342 00343 if ((leader_id == sender_record.leader) && (rec.nodeid == leader_id)) _parent=msg->senderId(); 00344 if (distanceToNode(leader_id) == 1) _parent = leader_id; 00345 if (leader_id == _radio->id()) _parent = -1; 00346 00347 self_record.parent=_parent; 00348 00349 topology_iterator tti; 00350 // if theres already a topology remove it 00351 if ((tti = _topology.find(rec.nodeid)) != _topology.end() ){ 00352 //printf("already have node %d in topology\n", rec.nodeid); 00353 //_topology.erase(rec.nodeid); 00354 tti->second.will_be_leader += rec.is_leader; 00355 } 00356 00357 if(_topology.find(rec.nodeid) == _topology.end()){ 00358 _topology.insert(make_pair(rec.nodeid, rec)); 00359 //printf("inserting node %d [%d] to topology\n", rec.nodeid, rec.is_leader); 00360 }else break; 00361 } 00362 00363 } 00364 } 00365 00366 topology_iterator it = _topology.begin(); 00367 for(;it != _topology.end();it++) 00368 { 00369 //printf("election for %d \n", it->second.nodeid); 00370 if(it->second.will_be_leader > 0) 00371 it->second.is_leader = 1; 00372 else 00373 it->second.is_leader = 0; 00374 it->second.will_be_leader = 0; 00375 } 00376 00377 _debug->debug( "MyID is: %d => My Leader is %d My Parent is %d and i know that:\n" ,_radio->id() ,leader_id,_parent); 00378 for (topology_iterator it = _topology.begin(); it != _topology.end(); ++it) 00379 { 00380 topology_record_t rec = it->second; 00381 rec.is_leader = (rec.leader == rec.nodeid); 00382 _debug->debug( "[%d] Id: %d | distance: %d | isLeader %d | leader: %d | parent %d\n" , 00383 _radio->id(),rec.nodeid ,rec.distance ,rec.is_leader ,rec.leader ,rec.parent); 00384 } 00385 00386 _debug->debug("\n [%d] Parent %d Hops %d CluseterID %d ClusterHead %d\n\n",_radio->id(), parent(), hops(), cluster_id(), cluster_head()); 00387 } 00388 00389 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00390 void 00391 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::findMyNeighbors(){ 00392 _debug->debug( "Node %d beighbors are:[ " ,_radio->id()); 00393 for ( NeighborDiscovery::iterator_t it = neighbor_discovery_->neighborhood.begin(); 00394 it!= neighbor_discovery_->neighborhood.end(); ++it) 00395 { 00396 // Print only the neighbors we have bidi links 00397 if (it->bidi) 00398 _debug->debug( " %d [%d]" , it->id, 255-it->last_lqi); 00399 } 00400 _debug->debug( " ]\n"); 00401 } 00402 00403 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00404 bool 00405 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::isNeighbor(nodeid_t other){ 00406 for ( NeighborDiscovery::iterator_t it = neighbor_discovery_->neighborhood.begin(); 00407 it!= neighbor_discovery_->neighborhood.end(); ++it) 00408 { 00409 if ((other==it->id) &&(it->bidi)) return true; 00410 00411 } 00412 00413 return false; 00414 } 00415 00416 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00417 void 00418 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::doWork 00419 (void*/*unused*/) 00420 { 00421 00422 //act on all accumulated messages 00423 if (handleAllMessages() == ecSuccess) 00424 { 00425 calculateNewTopology(); 00426 00427 //do leader selection 00428 leader_id = findLeader(); 00429 _leader = (leader_id == _radio->id()); 00430 00431 00432 //printf("%d OKOK: my leader is: %d\n", _radio->id(), leader_id); 00433 00434 00435 for(topology_iterator it = _topology.begin(); it != _topology.end(); it++) 00436 { 00437 if(it->second.nodeid == leader_id) 00438 continue; 00439 it->second.is_leader = 0; 00440 } 00441 00442 //update self entry in topology 00443 /* 00444 topology_record_t* self_record = _topology.find(_radio->id())->second; 00445 self_record.is_leader = _leader; 00446 self_record.leader = leader_id; 00447 */ 00448 00449 00450 topology_iterator me = _topology.find(_radio->id()); 00451 me->second.is_leader = _leader; 00452 me->second.leader = leader_id; 00453 00454 if (state_changed_callback_) 00455 {// BROADCASE SOMETHING CHANGED 00456 state_changed_callback_(CLUSTER_HEAD_CHANGED); 00457 state_changed_callback_(NODE_LEFT); 00458 state_changed_callback_(NODE_JOINED); 00459 } 00460 //broadcast the updated topology 00461 if (broadcastTopology() == ecSuccess) 00462 { 00463 findMyNeighbors(); 00464 //re-schedule this function 00465 scheduleWorkCallback(); 00466 } 00467 } 00468 00469 } 00470 00471 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00472 void 00473 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::doBlink(void*/*unused*/) 00474 { 00475 _led_state = !_led_state; 00477 scheduleBlinkCallback(); 00478 } 00479 00480 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00481 bool 00482 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::shouldAccept(Message* msg) 00483 { 00484 TopologyMessage* _msg; 00485 _msg = (TopologyMessage*) msg; //we can perform this cast because there's only one type of message. 00486 return isNeighbor(_msg->senderId()); 00487 } 00488 00489 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00490 uint8_t 00491 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::distanceToNode(nodeid_t node) 00492 { 00493 topology_iterator it = _topology.find(node); 00494 if (it == _topology.end()) 00495 { 00496 return INFINITE_DISTANCE; 00497 } 00498 return it->second.distance; 00499 } 00500 00501 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00502 void 00503 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::resetTopology() 00504 { 00505 /* 00506 _topology.clear(); 00507 _leader = false; 00508 topology_record_t self; 00509 self.distance=0; 00510 self.is_leader=false; 00511 self.leader=0; 00512 self.nodeid=_radio->id(); 00513 self.parent=-1; 00514 */ 00515 topology_record_t self; 00516 self.distance = 0; 00517 self.is_leader = (leader_id == _radio->id()); 00518 self.leader = leader_id; 00519 self.will_be_leader = 1; 00520 self.nodeid = _radio->id(); 00521 self.parent = -1; 00522 //printf("after reset: isL: %d, wBL: %d, L:%d, P: %d\n", self.is_leader, self.will_be_leader, self.leader, self.parent); 00523 00524 _topology.clear(); 00525 _leader = false; 00526 00527 _topology.insert(make_pair(self.nodeid, self)); 00528 } 00529 00530 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00531 bool 00532 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::shouldAssumeLeadership(const topology_record_t& record) 00533 { 00534 //2 leaders fighting - selecting the one with better lqi 00535 // uint16_t me_lqi,his_lqi; 00536 // 00537 // for ( NeighborDiscovery::iterator_t it = neighbor_discovery_->neighborhood.begin(); 00538 // it!= neighbor_discovery_->neighborhood.end(); ++it) 00539 // { 00540 // if (it->id == record.nodeid) his_lqi = it->last_lqi; 00541 // if (it->id == _radio->id()) me_lqi = it->last_lqi; 00542 // } 00543 // 00544 // printf("******************* [%d] - me %d - [%d] His %d \n",_radio->id(),me_lqi,record.nodeid,his_lqi); 00545 // //********************** 00546 00547 // chosing leader with higher ID. 00548 return record.nodeid < _radio->id(); 00549 } 00550 00551 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00552 error_code_t 00553 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::handleAllMessages() 00554 { 00555 Message* recv_msg = _mqueue->nextMessage(); 00556 //_debug->debug("1111111111111111111111111111111111111111111111 HERE!!!! \n"); 00557 while (recv_msg != NULL) 00558 { 00559 if (shouldAccept(recv_msg)) 00560 { 00561 00562 error_code_t errcode = recv_msg->applyTo(this); 00563 00564 if (errcode != ecSuccess) 00565 { 00566 return errcode; 00567 } 00568 } 00569 recv_msg = _mqueue->nextMessage(); 00570 } 00571 00572 return ecSuccess; 00573 } 00574 00575 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00576 nodeid_t 00577 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::findLeader() 00578 { 00579 00580 nodeid_t _leader_id = _radio->id(); 00581 topology_iterator me = _topology.begin(); 00582 uint8_t leaderRequired = (me->second.is_leader ? 0 : 1); 00583 00584 //printf("current cluster id is: %d\n", cluster_id()); 00585 00586 for (topology_iterator it = _topology.begin(); it != _topology.end(); ++it) 00587 { 00588 00589 //printf("TESTING NODE: %d: dist: %d, [%d]\n", it->second.nodeid, it->second.distance, it->second.is_leader); 00590 00591 if (it->second.distance > LEADER_RADIUS) 00592 { 00593 continue; //node is too far away for leader decisions. advance to the next node. 00594 } 00595 00596 //if ((it->second.is_leader) && (!shouldAssumeLeadership(it->second))) 00597 if((it->second.is_leader)) 00598 { 00599 // ************************CHOOSE LEADER ACCORDING TO CONNECTEVITY - not tested.*********************** 00600 // uint16_t me_lqi,his_lqi; 00601 // for ( NeighborDiscovery::iterator_t itt = neighbor_discovery_->neighborhood.begin(); 00602 // itt!= neighbor_discovery_->neighborhood.end(); ++itt) 00603 // { 00604 // if (itt->id == it->second.nodeid) his_lqi = itt->total_beacons; 00605 // if (itt->id == _radio->id()) me_lqi = itt->total_beacons; 00606 // } 00607 // 00608 //printf("******************* [%d] - me %d - [%d] His %d \n",_radio->id(),me_lqi,it->second.nodeid,his_lqi); 00609 // *********************************************************** 00610 00611 if(_leader_id < it->second.nodeid) // should be replace with better selection! 00612 { 00613 _leader_id = it->second.nodeid; 00614 leaderRequired = 0; 00615 //break; //we found our leader. nothing else left to do. 00616 } 00617 else if(leaderRequired) 00618 { 00619 leaderRequired = 0; 00620 _leader_id = it->second.nodeid; 00621 } 00622 } 00623 } 00624 00625 return _leader_id; 00626 } 00627 00628 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00629 error_code_t 00630 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::broadcastTopology() 00631 { 00632 TopologyMessage send_msg(_radio->id()); 00633 00634 for (topology_iterator it = _topology.begin(); it != _topology.end(); ++it) 00635 { 00636 //printf("%d --> [%d, %d]\n", _radio->id(), it->second.nodeid, it->second.is_leader); 00637 send_msg.addTopologyRecord(it->second); 00638 } 00639 00640 uint8_t buffer[Radio::MAX_MESSAGE_LENGTH]; 00641 send_msg.serialize(buffer, ARRSIZE(buffer)); 00642 //_debug->debug( "sending msg from: %d \n", send_msg.id); 00643 //_debug->debug( "the buffer contain this data: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", buffer[0], buffer[1],buffer[2], buffer[3],buffer[4], buffer[5],buffer[6], buffer[7],buffer[8], buffer[9], buffer[10], buffer[11],buffer[12], buffer[13],buffer[14], buffer[15],buffer[16], buffer[17],buffer[18], buffer[19], buffer[20], buffer[21],buffer[22], buffer[23],buffer[24], buffer[25],buffer[26], buffer[27],buffer[28], buffer[29], buffer[30], buffer[31],buffer[32], buffer[33],buffer[34], buffer[35],buffer[36], buffer[37],buffer[38], buffer[39], buffer[40], buffer[41],buffer[42], buffer[43],buffer[44], buffer[45],buffer[46], buffer[47],buffer[48], buffer[49], buffer[50], buffer[51],buffer[52], buffer[53],buffer[54], buffer[55],buffer[56], buffer[57],buffer[58], buffer[59], buffer[60], buffer[61],buffer[62], buffer[63],buffer[64], buffer[65],buffer[66], buffer[67],buffer[68], buffer[69], buffer[70], buffer[71],buffer[72], buffer[73],buffer[74], buffer[75],buffer[76], buffer[77],buffer[78], buffer[79], buffer[80], buffer[81],buffer[82], buffer[83],buffer[84], buffer[85],buffer[86], buffer[87],buffer[88], buffer[89], buffer[90], buffer[91],buffer[92], buffer[93],buffer[94], buffer[95],buffer[96], buffer[97],buffer[98], buffer[99], buffer[100], buffer[101],buffer[102], buffer[103],buffer[104], buffer[105],buffer[106], buffer[107],buffer[108], buffer[109], buffer[110], buffer[111],buffer[112], buffer[113],buffer[114], buffer[115],buffer[116], buffer[117],buffer[118], buffer[119], buffer[120], buffer[121],buffer[122], buffer[123],buffer[124], buffer[125],buffer[126], buffer[127],buffer[128], buffer[129], buffer[130],buffer[131],buffer[132], buffer[133],buffer[134], buffer[135],buffer[136], buffer[137],buffer[138], buffer[139], buffer[140], buffer[141],buffer[142], buffer[143],buffer[144], buffer[145],buffer[146], buffer[147],buffer[148], buffer[149], buffer[150], buffer[151],buffer[152], buffer[153],buffer[154], buffer[155],buffer[156], buffer[157],buffer[158], buffer[159], buffer[160], buffer[161],buffer[162], buffer[163],buffer[164], buffer[165],buffer[166], buffer[167],buffer[168], buffer[169], buffer[170], buffer[171],buffer[172], buffer[173],buffer[174], buffer[175],buffer[176], buffer[177],buffer[178], buffer[179], buffer[180], buffer[181],buffer[182], buffer[183],buffer[184], buffer[185],buffer[186], buffer[187],buffer[188], buffer[189], buffer[190], buffer[191],buffer[192], buffer[193],buffer[194], buffer[195],buffer[196], buffer[197],buffer[198], buffer[199], buffer[200], buffer[201],buffer[202], buffer[203],buffer[204], buffer[205],buffer[206], buffer[207],buffer[208], buffer[209],buffer[210], buffer[211],buffer[212], buffer[213],buffer[214], buffer[215],buffer[216], buffer[217],buffer[218], buffer[219], buffer[220], buffer[221],buffer[222], buffer[223],buffer[224], buffer[225],buffer[226], buffer[227],buffer[228], buffer[229], buffer[230], buffer[231],buffer[232], buffer[233],buffer[234], buffer[235],buffer[236], buffer[237],buffer[238], buffer[239], buffer[240], buffer[241],buffer[242], buffer[243],buffer[244], buffer[245],buffer[246], buffer[247],buffer[248], buffer[249], buffer[250], buffer[251],buffer[252], buffer[253],buffer[254], buffer[255],buffer[256],buffer[257], buffer[258], buffer[259],buffer[260]); 00644 //printf(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX %d, %d %d %d %d %d %d %d %d %d\n ",buffer[0], buffer[1],buffer[2], buffer[3],buffer[4], buffer[5],buffer[6], buffer[7],buffer[8], buffer[9]); 00645 _radio->send(Radio::BROADCAST_ADDRESS,Radio::MAX_MESSAGE_LENGTH, buffer); 00646 00647 // sizeof(buffer)/sizeof(typename Radio::block_data_t), buffer); 00648 00649 return ecSuccess; 00650 } 00651 00653 00654 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00655 nodeid_t 00656 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::cluster_head(){ 00657 return leader_id; 00658 } 00659 00660 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00661 nodeid_t 00662 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P>::parent(){ 00663 return _parent; 00664 } 00665 00666 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00667 uint8_t 00668 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::hops(){ 00669 00670 return (_topology.find(leader_id))->second.distance; 00671 } 00672 00673 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00674 nodeid_t 00675 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::cluster_id(){ 00676 return leader_id; 00677 } 00678 00679 template<typename OsModel_P, typename Radio_P, typename Timer_P, typename Clock_P, typename Debug_P > 00680 Sensor<OsModel_P,Radio_P,Timer_P,Clock_P,Debug_P >::~Sensor(){ 00681 for (message_iterator it = _messages.begin(); it != _messages.end(); ++it) 00682 { 00683 delete it->second; 00684 } 00685 _messages.clear(); 00686 } 00687 00688 00689 } 00690 #endif