Wiselib
wiselib.testing/algorithms/routing/olsr/olsr_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_OLSR_ROUTING_MSG_H__
00020 #define __ALGORITHMS_ROUTING_OLSR_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    class OlsrRoutingMessage
00030    {
00031    public:
00032       typedef OsModel_P OsModel;
00033       typedef Radio_P Radio;
00034       typedef typename Radio::block_data_t block_data_t;
00035       // --------------------------------------------------------------------
00036       inline OlsrRoutingMessage();
00037 
00038       // --------------------------------------------------------------------
00039       inline uint8_t msg_id()
00040       { return read<OsModel, block_data_t, uint8_t>( buffer ); };
00041 
00042      inline uint16_t source()
00043       { return read<OsModel, block_data_t, uint16_t>(buffer + SOURCE_POS); }
00044 
00045      inline uint16_t destination()
00046       { return read<OsModel, block_data_t, uint16_t>(buffer + DEST_POS); }
00047 
00048      inline uint8_t* payload()
00049       { return buffer + PAYLOAD_POS + 1; }
00050 
00051       inline uint8_t payload_size()
00052       { return read<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_POS); }
00053 
00054       inline size_t buffer_size()
00055       { return PAYLOAD_POS + 1 + payload_size(); }
00056      // --------------------------------------------------------------------
00057 
00058       inline void set_msg_id( uint8_t id )
00059       { write<OsModel, block_data_t, uint8_t>( buffer, id ); }
00060 
00061       inline void set_source( uint16_t src )
00062       { write<OsModel, block_data_t, uint16_t>(buffer + SOURCE_POS, src); }
00063 
00064       inline void set_destination( uint16_t dest )
00065       { write<OsModel, block_data_t, uint16_t>(buffer + DEST_POS, dest); }
00066 
00067       inline void set_payload( uint8_t len, uint8_t *buf )
00068       {
00069          write<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_POS, len);
00070          memcpy( buffer + PAYLOAD_POS + 1, buf, len);
00071       }
00072 
00073    // --------------------------------------------------------------------
00074 
00075    private:
00076       inline void set_payload_size( uint8_t len )
00077       { write<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_POS, len); }
00078 
00079       enum data_positions
00080       {
00081          MSG_ID_POS  = 0,
00082          SOURCE_POS  = 1,
00083          DEST_POS    = 3,
00084          PAYLOAD_POS = 5
00085       };
00086 
00087       block_data_t buffer[Radio::MAX_MESSAGE_LENGTH];
00088    };
00089    // -----------------------------------------------------------------------
00090    template<typename OsModel_P,
00091             typename Radio_P>
00092    OlsrRoutingMessage<OsModel_P, Radio_P>::
00093    OlsrRoutingMessage()
00094    {
00095      // Initialization of the msg_id and etc.
00096      // Assignment of the value is given at the instantiation of class RoutingMessage.
00097       set_msg_id( 0 );
00098       set_payload_size( 0 );
00099       set_source( Radio::NULL_NODE_ID );
00100       set_destination( Radio::NULL_NODE_ID );
00101    }
00102 
00103 }
00104 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines