Wiselib
wiselib.stable/algorithms/routing/dsr/dsr_routing_msg.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 __ALGORITHMS_ROUTING_DSR_ROUTING_MSG_H__
00020 #define __ALGORITHMS_ROUTING_DSR_ROUTING_MSG_H__
00021 
00022 #include "util/serialization/simple_types.h"
00023 
00024 namespace wiselib
00025 {
00026 
00027    template <typename OsModel_P,
00028              typename Radio_P,
00029              typename Path_P>
00030    class DsrRoutingMessage
00031    {
00032    public:
00033       typedef OsModel_P OsModel;
00034       typedef Radio_P Radio;
00035       typedef typename Radio::message_id_t message_id_t;
00036       typedef typename Radio::block_data_t block_data_t;
00037       typedef typename Radio::node_id_t node_id_t;
00038       typedef Path_P Path;
00039       typedef typename Path::iterator PathIterator;
00040       // --------------------------------------------------------------------
00041       inline DsrRoutingMessage();
00042       // --------------------------------------------------------------------
00043       inline DsrRoutingMessage( uint8_t message_id,
00044                         uint16_t source, uint16_t destination,
00045                         uint8_t path_idx, uint8_t l, uint8_t *d );
00046       // --------------------------------------------------------------------
00047       message_id_t msg_id()
00048       { return read<OsModel, block_data_t, message_id_t>( buffer ); };
00049       // --------------------------------------------------------------------
00050       void set_msg_id( message_id_t id )
00051       { write<OsModel, block_data_t, message_id_t>( buffer, id ); }
00052       // --------------------------------------------------------------------
00053       uint8_t path_idx()
00054       { return read<OsModel, block_data_t, uint8_t>(buffer + IDX_POS); }
00055       // --------------------------------------------------------------------
00056       void set_path_idx( uint8_t idx )
00057       { write<OsModel, block_data_t, uint8_t>(buffer + IDX_POS, idx); }
00058       // --------------------------------------------------------------------
00059       void dec_path_idx( void )
00060       { set_path_idx( path_idx() - 1 );  }
00061       // --------------------------------------------------------------------
00062       void inc_path_idx( void )
00063       { set_path_idx( path_idx() + 1 );  }
00064       // --------------------------------------------------------------------
00065       node_id_t source()
00066       { return read<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS); }
00067       // --------------------------------------------------------------------
00068       void set_source( node_id_t src )
00069       { write<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS, src); }
00070       // --------------------------------------------------------------------
00071       node_id_t destination()
00072       { return read<OsModel, block_data_t, node_id_t>(buffer + DEST_POS); }
00073       // --------------------------------------------------------------------
00074       void set_destination( node_id_t dest )
00075       { write<OsModel, block_data_t, node_id_t>(buffer + DEST_POS, dest); }
00076       // --------------------------------------------------------------------
00077       uint8_t entry_cnt()
00078       { return read<OsModel, block_data_t, uint8_t>(buffer + ENTRY_CNT_POS); }
00079       // --------------------------------------------------------------------
00080       void set_entry_cnt( uint8_t entry_cnt )
00081       { write<OsModel, block_data_t, uint8_t>(buffer + ENTRY_CNT_POS, entry_cnt); }
00082       // --------------------------------------------------------------------
00083       void set_path( Path& p )
00084       {
00085          uint8_t psize = payload_size();
00086          block_data_t pdata[psize];
00087          if ( psize > 0 )
00088          {
00089             memcpy( pdata, payload(), psize );
00090          }
00091 
00092          int idx = 0;
00093          for ( PathIterator it = p.begin(); it != p.end(); ++it )
00094          {
00095             int offset = PATH_POS + (idx * sizeof(node_id_t));
00096             write<OsModel, block_data_t, node_id_t>( buffer + offset, *it );
00097             idx++;
00098          }
00099          set_entry_cnt( idx );
00100 
00101          set_payload_size( psize );
00102          if ( psize > 0 )
00103             set_payload( psize, pdata );
00104       }
00105       // -----------------------------------------------------------------------
00106       void path( Path& p )
00107       {
00108          p.clear();
00109          for ( int i = 0; i < entry_cnt(); i++ )
00110          {
00111             node_id_t node;
00112             int offset = PATH_POS + (i * sizeof(node_id_t));
00113             read<OsModel, block_data_t, node_id_t>( buffer + offset, node );
00114             p.push_back( node );
00115          }
00116       }
00117       // --------------------------------------------------------------------
00118       uint8_t payload_size()
00119       {
00120          // offset = PATH_POS + path entries
00121          int offset = PATH_POS + (entry_cnt() * sizeof(node_id_t));
00122          return read<OsModel, block_data_t, uint8_t>(buffer + offset);
00123       }
00124       // --------------------------------------------------------------------
00125       void set_payload_size( uint8_t size )
00126       {
00127          // offset = PATH_POS + path entries
00128          int offset = PATH_POS + (entry_cnt() * sizeof(node_id_t));
00129          write<OsModel, block_data_t, uint8_t>(buffer + offset, size);
00130       }
00131       // -----------------------------------------------------------------------
00132       void set_payload( uint8_t len, block_data_t* data )
00133       {
00134          // offset = PATH_POS + path entries + sizeof payload size
00135          int offset = PATH_POS + (entry_cnt() * sizeof(node_id_t)) + 1;
00136          memcpy( buffer + offset, data, len );
00137       }
00138       // -----------------------------------------------------------------------
00139       block_data_t* payload( void )
00140       {
00141          // offset = PATH_POS + path entries + sizeof payload size
00142          int offset = PATH_POS + (entry_cnt() * sizeof(node_id_t)) + 1;
00143          return buffer + offset;
00144       }
00145       // --------------------------------------------------------------------
00146       size_t buffer_size()
00147       {
00148          // overall size = PATH_POS + path entries + sizeof payload size + payload size
00149          return PATH_POS + (entry_cnt() * sizeof(node_id_t)) + payload_size();
00150       }
00151 
00152    private:
00153       enum data_positions
00154       {
00155          MSG_ID_POS    = 0,
00156          IDX_POS       = MSG_ID_POS + sizeof(message_id_t),
00157          SOURCE_POS    = IDX_POS + 1,
00158          DEST_POS      = SOURCE_POS + sizeof(node_id_t),
00159          ENTRY_CNT_POS = DEST_POS + sizeof(node_id_t),
00160          PATH_POS      = ENTRY_CNT_POS + 1
00161       };
00162 
00163       block_data_t buffer[Radio::MAX_MESSAGE_LENGTH];
00164 
00165    };
00166    // -----------------------------------------------------------------------
00167    template <typename OsModel_P,
00168              typename Radio_P,
00169              typename Path_P>
00170    DsrRoutingMessage<OsModel_P, Radio_P, Path_P>::
00171    DsrRoutingMessage()
00172    {}
00173    // -----------------------------------------------------------------------
00174    template <typename OsModel_P,
00175              typename Radio_P,
00176              typename Path_P>
00177    DsrRoutingMessage<OsModel_P, Radio_P, Path_P>::
00178    DsrRoutingMessage( uint8_t message_id,
00179                       uint16_t source, uint16_t destination,
00180                       uint8_t path_idx, uint8_t len, uint8_t *data )
00181    {
00182       set_msg_id( message_id );
00183       set_path_idx( path_idx );
00184       set_source( source );
00185       set_destination( destination );
00186       set_entry_cnt( 0 );
00187       set_payload_size( len );
00188       set_payload( len, data );
00189    }
00190 
00191 }
00192 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines