Wiselib
wiselib.testing/algorithms/privacy/privacy.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 #ifndef __PRIVACY_H__
00020 #define __PRIVACY_H__
00021 
00022 #include "util/delegates/delegate.hpp"
00023 #include "privacy_config.h"
00024 
00025 namespace wiselib
00026 {
00027 template<   typename Os_P,
00028          typename Radio_P,
00029          typename Timer_P,
00030          typename B_Uart_P,
00031          typename PrivacyMessage_P,
00032          typename PrivacyMessageList_P,
00033          typename Debug_P>
00034    class PrivacyType
00035    {
00036    public:
00037       typedef Os_P Os;
00038       typedef Radio_P Radio;
00039       typedef Timer_P Timer;
00040       typedef B_Uart_P B_Uart;
00041       typedef PrivacyMessage_P PrivacyMessage;
00042       typedef PrivacyMessageList_P PrivacyMessageList;
00043       typedef Debug_P Debug;
00044       typedef PrivacyType<Os, Radio, Timer, B_Uart, PrivacyMessage, PrivacyMessageList, Debug> self_type;
00045       typedef typename Radio::node_id_t node_id_t;
00046       typedef typename Radio::size_t size_t;
00047       typedef typename Radio::block_data_t block_data_t;
00048       typedef typename Radio::message_id_t message_id_t;
00049       typedef typename Timer::millis_t millis_t;
00050       typedef typename Radio::TxPower TxPower;
00051       typedef typename PrivacyMessageList::iterator PrivacyMessageListIterator;
00052       typedef delegate3<void, node_id_t, size_t, block_data_t*> event_notifier_delegate_t;
00053       struct callback_element{ event_notifier_delegate_t callback; uint16_t callback_id; };
00054       typedef wiselib::vector_static <Os, callback_element, 20> CallbackContainer;
00055       typedef typename CallbackContainer::iterator CallbackContainerIterator;
00056       // -----------------------------------------------------------------------
00057       void init( Radio& radio, Debug& debug, B_Uart& b_uart, Timer& timer )
00058       {
00059          radio_ = &radio;
00060          debug_ = &debug;
00061          b_uart_= &b_uart;
00062          timer_ = &timer;
00063       }
00064       PrivacyType()
00065          :radio_callback_id_     ( 0 ),
00066           uart_callback_id_      ( 0 )
00067       {}
00068       // -----------------------------------------------------------------------
00069       ~PrivacyType()
00070       {}
00071       // -----------------------------------------------------------------------
00072       void enable( void )
00073       {
00074          #ifdef ISENSE_PRIVACY_DEBUG
00075          debug().debug( "Privacy %x: Boot \n", radio().id() );
00076          #endif
00077          radio().enable_radio();
00078          b_uart().enable_serial_comm();
00079          radio_callback_id_ = radio().template reg_recv_callback<self_type, &self_type::radio_receive>( this );
00080          uart_callback_id_ = b_uart().template reg_read_callback<self_type, &self_type::uart_receive>( this );
00081          uart_read_write = 0;
00082          process_request();
00083       }
00084       // -----------------------------------------------------------------------
00085       void disable( void )
00086       {
00087          #ifdef ISENSE_PRIVACY_DEBUG
00088          debug().debug( "Private %x: Disable \n", radio().id() );
00089          #endif
00090          radio().unreg_recv_callback( radio_callback_id_ );
00091          radio().disable();
00092          b_uart().unreg_read_callback( uart_callback_id_ );
00093          b_uart().disable_serial_comm();
00094       }
00095       // -----------------------------------------------------------------------
00096       void radio_receive( node_id_t from, size_t len, block_data_t *data )
00097       {
00098          message_id_t msg_id = *data;
00099          if ( ( msg_id == PRIVACY_DECRYPTION_REQUEST_ID ) ||
00100             ( msg_id == PRIVACY_ENCRYPTION_REQUEST_ID ) ||
00101             ( msg_id == PRIVACY_RANDOMIZE_REQUEST_ID ) )
00102          {
00103             PrivacyMessage *message = ( PrivacyMessage* )data;
00104             #ifdef ISENSE_PRIVACY_DEBUG
00105             debug().debug( "Privacy %x: Radio received - received request from %x\n", radio().id(), from );
00106             debug().debug( " message\n ");
00107             debug().debug( "msg id %i (size %i )\n", msg_id, sizeof( msg_id ));
00108             debug().debug( "req id %x (size %i)\n", message->request_id(), sizeof( uint16_t) );
00109             debug().debug( "pay len %i (size %i)\n", message->payload_size(), sizeof( size_t ) );
00110             #endif
00111             message_list.push_back( *message );
00112          }
00113          else if ( msg_id == PRIVACY_UNREGISTER )
00114          {
00115             PrivacyMessage *message = ( PrivacyMessage* )data;
00116             for ( CallbackContainerIterator i = privacy_callbacks.begin(); i != privacy_callbacks.end(); ++i )
00117             {
00118                if ( i->callback_id == message->request_id() ) { privacy_callbacks.erase( i ); }
00119             }
00120          }
00121       }
00122       //------------------------------------------------------------------------
00123       void uart_receive( size_t len, block_data_t* buff )
00124       {
00125          message_id_t msg_id = *buff;
00126          if ( ( msg_id == PRIVACY_DECRYPTION_REQUEST_ID ) ||
00127             ( msg_id == PRIVACY_ENCRYPTION_REQUEST_ID ) ||
00128             ( msg_id == PRIVACY_RANDOMIZE_REQUEST_ID ) )
00129          {
00130             PrivacyMessage* message = ( PrivacyMessage* )buff;
00131             notify_privacy_callbacks( len, buff );
00132             send_privacy( len, buff );
00133             for ( PrivacyMessageListIterator i = message_list.begin(); i != message_list.end(); ++i )
00134             {
00135                #ifdef ISENSE_PRIVACY_DEBUG
00136                debug().debug("In the loops comp %x with %x", i->request_id(), message->request_id(); );
00137                #endif
00138                if ( i->request_id() == message->request_id() )
00139                {
00140                   message_list.erase( i );
00141                   uart_read_write = 0;
00142                }
00143             }
00144          }
00145       }
00146       //------------------------------------------------------------------------
00147       template<class T, void (T::*TMethod)( node_id_t, size_t, block_data_t* )>
00148       uint8_t reg_privacy_callback( uint16_t callback_id, T *obj_pnt )
00149       {
00150          callback_element ce;
00151          ce.callback_id = callback_id;
00152          ce.callback = event_notifier_delegate_t::template from_method<T, TMethod>( obj_pnt );
00153          privacy_callbacks.push_back ( ce );
00154          return 0;
00155       }
00156       //------------------------------------------------------------------------
00157       void notify_privacy_callbacks( size_t len, block_data_t* buff )
00158       {
00159          for ( CallbackContainerIterator i = privacy_callbacks.begin(); i != privacy_callbacks.end(); ++i )
00160          {
00161             (i->callback )( radio().id(), len, buff );
00162          }
00163       }
00164       // -----------------------------------------------------------------------
00165       void send_privacy( size_t len, block_data_t* buff )
00166       {
00167          message_id_t msg_id = *buff;
00168          if ( ( msg_id == PRIVACY_DECRYPTION_REPLY_ID ) ||
00169             ( msg_id == PRIVACY_ENCRYPTION_REPLY_ID ) ||
00170             ( msg_id == PRIVACY_RANDOMIZE_REPLY_ID ) )
00171          {
00172             TxPower power;
00173             power.set_dB( 0 );
00174             radio().set_power( power );
00175             radio().send( Radio::BROADCAST_ADDRESS, len, buff );
00176          }
00177       }
00178       // -----------------------------------------------------------------------
00179       void process_request( void* data = NULL )
00180       {
00181          #ifdef ISENSE_PRIVACY_DEBUG
00182          debug().debug( "Privacy %x: UART read write atomic - Message list of %i elements", radio().id(), message_list.size() );
00183          #endif
00184          if ( uart_read_write == 0 )
00185          {
00186             PrivacyMessageListIterator i = message_list.begin();
00187             b_uart().write( i->buffer_size(), i->buffer() );
00188             uart_read_write = 1;
00189          }
00190          else
00191          {
00192             timer().template set_timer<self_type, &self_type::process_request>( 50, this, ( void* )data );
00193          }
00194 
00195       }
00196       // -----------------------------------------------------------------------
00197       Radio& radio()
00198       {
00199          return *radio_;
00200       }
00201       // -----------------------------------------------------------------------
00202       Debug& debug()
00203       {
00204          return *debug_;
00205       }
00206       // -----------------------------------------------------------------------
00207       Timer& timer()
00208       {
00209          return *timer_;
00210       }
00211       // -----------------------------------------------------------------------
00212       B_Uart& b_uart()
00213       {
00214          return *b_uart_;
00215       }
00216       // -----------------------------------------------------------------------
00217    private:
00218       Radio * radio_;
00219       Debug * debug_;
00220       B_Uart * b_uart_;
00221       Timer * timer_;
00222       enum MessageIds
00223       {
00224          PRIVACY_DECRYPTION_REQUEST_ID = 100,
00225          PRIVACY_ENCRYPTION_REQUEST_ID = 110,
00226          PRIVACY_RANDOMIZE_REQUEST_ID = 120,
00227          PRIVACY_DECRYPTION_REPLY_ID = 130,
00228          PRIVACY_ENCRYPTION_REPLY_ID = 140,
00229          PRIVACY_RANDOMIZE_REPLY_ID = 150,
00230          PRIVACY_UNREGISTER = 160
00231       };
00232       uint32_t radio_callback_id_;
00233       uint32_t uart_callback_id_;
00234       CallbackContainer privacy_callbacks;
00235       PrivacyMessageList message_list;
00236       uint8_t uart_read_write;
00237       };
00238 }
00239 #endif
00240 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines