Wiselib
wiselib.testing/algorithms/tracking/PLTT_target.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 ** This file is part of the generic algorithm library Wiselib.           **
00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004 **                                                                       **
00005 ** The Wiselib is free software: you can redistribute it and/or modify   **
00006 ** it under the terms of the GNU Lesser General Public License as        **
00007 ** published by the Free Software Foundation, either version 3 of the    **
00008 ** License, or (at your option) any later version.                       **
00009 **                                                                       **
00010 ** The Wiselib is distributed in the hope that it will be useful,        **
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013 ** GNU Lesser General Public License for more details.                   **
00014 **                                                                       **
00015 ** You should have received a copy of the GNU Lesser General Public      **
00016 ** License along with the Wiselib.                                       **
00017 ** If not, see <http://www.gnu.org/licenses/>.                           **
00018 ***************************************************************************/
00019 
00020 #ifndef __PLTT_TARGET_H__
00021 #define __PLTT_TARGET_H__
00022 
00023 #include "PLTT_config.h"
00024 #include "PLTT_message.h"
00025 #ifdef PLTT_SECURE
00026    #include "util/delegates/delegate.hpp"
00027 #endif
00028 
00029 namespace wiselib
00030 {
00031    template<typename Os_P,
00032       typename PLTT_Trace_P,
00033       typename Node_P,
00034       typename Timer_P,
00035       typename Radio_P,
00036 #ifdef PLTT_SECURE
00037       typename PrivacyMessage_P,
00038 #endif
00039       typename Clock_P,
00040       typename PLTT_TargetSpreadMetric_P,
00041       typename Debug_P>
00042    class PLTT_TargetType
00043    {
00044    public:
00045       typedef Os_P Os;
00046       typedef Radio_P Radio;
00047       typedef Debug_P Debug;
00048       typedef Node_P Node;
00049       typedef PLTT_Trace_P PLTT_Trace;
00050       typedef Timer_P Timer;
00051       typedef Clock_P Clock;
00052       typedef PLTT_TargetSpreadMetric_P PLTT_TargetSpreadMetric;
00053 #ifdef PLTT_SECURE
00054       typedef PrivacyMessage_P PrivacyMessage;
00055       typedef PLTT_TargetType<Os, PLTT_Trace, Node, Timer, Radio, PrivacyMessage, Clock, PLTT_TargetSpreadMetric, Debug> self_type;
00056 #else
00057       typedef PLTT_TargetType<Os, PLTT_Trace, Node, Timer, Radio, Clock, PLTT_TargetSpreadMetric, Debug> self_type;
00058 #endif
00059       typedef typename Radio::node_id_t node_id_t;
00060       typedef typename Radio::size_t size_t;
00061       typedef typename Radio::block_data_t block_data_t;
00062       typedef typename Radio::message_id_t message_id_t;
00063       typedef typename Timer::millis_t millis_t;
00064       typedef typename PLTT_Trace::PLTT_TraceData PLTT_TraceData;
00065       typedef typename PLTT_Trace::TimesNumber TimesNumber;
00066       typedef typename Radio::TxPower TxPower;
00067       typedef PLTT_MessageType<Os, Radio> Message;
00068 #ifdef PLTT_SECURE
00069       typedef delegate3<void, node_id_t, size_t, block_data_t*> event_notifier_delegate_t;
00070 #endif
00071       void init( Radio& radio, Timer& timer, Clock& clock, Debug& debug )
00072       {
00073          radio_ = &radio;
00074          timer_ = &timer;
00075          debug_ = &debug;
00076          clock_ = &clock;
00077       }
00078       Node* get_self()
00079       {
00080          return &self;
00081       }
00082       void set_self( Node _n )
00083       {
00084          self = _n;
00085       }
00086       // -----------------------------------------------------------------------
00087       PLTT_TargetType()
00088       {}
00089       // -----------------------------------------------------------------------
00090       PLTT_TargetType( PLTT_Trace _t, millis_t _s, int16_t _tp )
00091       {
00092          target_trace = _t;
00093          spread_milis = _s;
00094          trans_power.set_dB( _tp );
00095          target_trace.set_start_time( 0 );
00096       }
00097       // -----------------------------------------------------------------------
00098       ~PLTT_TargetType()
00099       {}
00100       // -----------------------------------------------------------------------
00101       void enable( void )
00102       {
00103          radio().enable_radio();
00104          #ifdef ISENSE_PLTT_TARGET_DEBUG_MISC
00105          debug().debug( "PLTT_Target %x: Boot \n", self.get_id() );
00106          #endif
00107          target_trace.set_target_id( self.get_id() );
00108          timer().template set_timer<self_type, &self_type::send_trace>( spread_milis, this, 0 );
00109 #ifdef PLTT_TARGET_SPREAD_METRICS
00110 #ifndef MARIOS_DEMO
00111          timer().template set_timer<self_type, &self_type::print_metrics>( 10000, this, 0 );
00112 #endif
00113 #endif
00114       }
00115       // -----------------------------------------------------------------------
00116       void disable( void )
00117       {
00118          #ifdef ISENSE_PLTT_TARGET_DEBUG_MISC
00119          debug().debug( "PLTT_Target %x: Disable \n", self.id );
00120          #endif
00121          #ifdef PLTT_SECURE
00122          privacy_message.set_msg_id( PRIVACY_UNREGISTER );
00123          privacy_message.set_request_id( self.id() );
00124          privacy_message.set_payload( 0, NULL );
00125          privacy_radio_callback( 999, privacy_message.buffer_size(), privacy_message.buffer()  );
00126          #endif
00127          radio().disable();
00128       }
00129       // -----------------------------------------------------------------------
00130       void send( node_id_t destination, size_t len, block_data_t *data )
00131       {
00132          radio().set_power( trans_power );
00133          Message message;
00134          message.set_msg_id( PLTT_DETECTION_ID );
00135          message.set_payload( len, data );
00136          radio().send( destination, message.buffer_size(), (block_data_t*)&message );
00137       }
00138       // -----------------------------------------------------------------------
00139 #ifdef PLTT_SECURE
00140       // -----------------------------------------------------------------------
00141       void send_trace( void* userdata = NULL)
00142       {
00143          #ifdef ISENSE_PLTT_TARGET_DEBUG_SEND
00144          debug().debug( "PLTT_Target %x: Send Trace to randomizer \n", self.get_id() );
00145          #endif
00146          debug().debug( " sec: %i ms: %i\n", clock().time().sec(), clock().time().ms() );
00147          block_data_t buffer_trace[Radio::MAX_MESSAGE_LENGTH];
00148          block_data_t* buff_trace = buffer_trace;
00149          buff_trace = target_trace.set_buffer_from( buff_trace );
00150          size_t len_trace = target_trace.get_buffer_size();
00151          privacy_message.set_msg_id( PRIVACY_RANDOMIZE_REQUEST_ID );
00152          privacy_message.set_request_id( self.get_id() );
00153          privacy_message.set_payload( len_trace, buff_trace );
00154          privacy_radio_callback( self.get_id(), privacy_message.buffer_size(), privacy_message.buffer()  );
00155       }
00156       // -----------------------------------------------------------------------
00157       void randomize_callback( node_id_t from, size_t len, block_data_t* buff )
00158       {
00159          #ifdef ISENSE_PLTT_SECURE_TARGET_DEBUG
00160          debug().debug( "PLTT_Target %x: Randomize callback \n", self.get_id() );
00161          #endif
00162          block_data_t buffer[Radio::MAX_MESSAGE_LENGTH];
00163          memcpy( buffer, buff, len );
00164          PrivacyMessage* pm = ( PrivacyMessage* )buffer;
00165          PLTT_Trace t( pm->payload() );
00166          t.print( debug() );
00167          if ( pm -> request_id() == self.get_id() )
00168          {
00169             #ifdef PLTT_TARGET_MINI_RUN
00170             if ( target_trace.get_start_time() < target_mini_run_times )
00171             {
00172             #endif
00173                send( Radio::BROADCAST_ADDRESS, len, buffer );
00174                target_trace.update_start_time();
00175                timer().template set_timer<self_type, &self_type::send_trace>( spread_milis, this, 0 );
00176                #ifdef PLTT_TARGET_SPREAD_METRICS
00177                target_metrics.inc_trace_messages_send();
00178                target_metrics.inc_trace_messages_bytes_send( len + sizeof( message_id_t ) + sizeof( size_t ) );
00179                #endif
00180             #ifdef PLTT_TARGET_MINI_RUN
00181             }
00182             #endif
00183          }
00184       }
00185       //------------------------------------------------------------------------
00186       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00187       uint8_t reg_privacy_radio_callback( T *obj_pnt )
00188       {
00189          privacy_radio_callback = event_notifier_delegate_t::template from_method<T, TMethod>( obj_pnt );
00190          return 0;
00191       }
00192       // -----------------------------------------------------------------------
00193 #else
00194       // -----------------------------------------------------------------------
00195       void send_trace( void* userdata = NULL)
00196       {
00197          block_data_t buffer[Radio::MAX_MESSAGE_LENGTH];
00198          block_data_t* buff = buffer;
00199          target_trace.set_buffer_from( buff );
00200          size_t len = target_trace.get_buffer_size();
00201          #ifdef ISENSE_PLTT_TARGET_DEBUG_SEND
00202          debug().debug( "PLTT_Target %x: Send Trace \n", self.get_id() );
00203          #endif
00204          #ifdef PLTT_TARGET_MINI_RUN
00205          if ( target_trace.get_start_time() < target_mini_run_times )
00206          {
00207          #endif
00208             send( Radio::BROADCAST_ADDRESS, len, (uint8_t*)buff );
00209             target_trace.update_start_time();
00210             debug().debug("%i:%i:%i", self.get_id(), 69, 94 );
00211             timer().template set_timer<self_type, &self_type::send_trace>( spread_milis, this, 0 );
00212             #ifdef PLTT_TARGET_SPREAD_METRICS
00213             target_metrics.inc_trace_messages_send();
00214             target_metrics.inc_trace_messages_bytes_send( len + sizeof( message_id_t ) + sizeof( size_t ) );
00215             #endif
00216          #ifdef PLTT_TARGET_MINI_RUN
00217          }
00218          #endif
00219 
00220       }
00221       // -----------------------------------------------------------------------
00222 #endif
00223 #ifdef PLTT_TARGET_SPREAD_METRICS
00224       void print_metrics( void* userdata = NULL )
00225       {
00226          debug().debug( " TSMN\t%i\t%i ", target_metrics.get_trace_messages_send(), target_metrics.get_trace_messages_bytes_send() );
00227          debug().debug( " TSMP\t%i\t%i ", target_metrics_periodic.get_trace_messages_send(), target_metrics_periodic.get_trace_messages_bytes_send() );
00228          target_metrics_periodic.reset();
00229          timer().template set_timer<self_type, &self_type::print_metrics>( metrics_timer, this, 0 );
00230       }
00231       // -----------------------------------------------------------------------
00232       void set_metrics_timer( millis_t _t )
00233       {
00234          metrics_timer = _t;
00235       }
00236 #endif
00237 #ifdef PLTT_TARGET_MINI_RUN
00238       void set_mini_run_times( uint8 _t )
00239       {
00240          target_mini_run_times = _t;
00241       }
00242 #endif
00243    private:
00244       Radio& radio()
00245       {
00246          return *radio_; 
00247       }
00248       Timer& timer()
00249       {
00250          return *timer_; 
00251       }
00252       Debug& debug()
00253       {
00254          return *debug_; 
00255       }
00256       Clock& clock()
00257       {
00258          return *clock_;
00259       }
00260       Radio * radio_;
00261       Timer * timer_;
00262       Debug * debug_;
00263       Clock * clock_;
00264       enum MessageIds
00265       {
00266          PLTT_DETECTION_ID = 11
00267 #ifdef PLTT_SECURE
00268          ,PRIVACY_RANDOMIZE_REQUEST_ID = 120,
00269          PRIVACY_UNREGISTER = 160
00270 #endif
00271       };
00272       PLTT_Trace target_trace;
00273       millis_t spread_milis;
00274       TxPower trans_power;
00275       Node self;
00276 #ifdef PLTT_SECURE
00277       event_notifier_delegate_t privacy_radio_callback;
00278       PrivacyMessage privacy_message;
00279 #endif
00280 #ifdef PLTT_TARGET_SPREAD_METRICS
00281       PLTT_TargetSpreadMetric target_metrics;
00282       PLTT_TargetSpreadMetric target_metrics_periodic;
00283       millis_t metrics_timer;
00284 #endif
00285 #ifdef PLTT_TARGET_MINI_RUN
00286       uint8_t target_mini_run_times;
00287 #endif
00288    };
00289 
00290 }
00291 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines