Wiselib
wiselib.testing/algorithms/routing/dymo/dymo_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_DYMO_ROUTING_MSG_H__
00020 #define __ALGORITHMS_ROUTING_DYMO_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 DYMORoutingMessage
00031    {
00032    public:
00033       typedef OsModel_P OsModel;
00034       typedef Radio_P Radio;
00035       typedef typename Radio::block_data_t block_data_t;
00036       typedef Path_P Path;
00037       // --------------------------------------------------------------------
00038       inline DYMORoutingMessage();
00039       // --------------------------------------------------------------------
00040       inline DYMORoutingMessage( uint8_t message_id,
00041                         uint16_t source, uint16_t destination,
00042                         uint8_t path_idx, uint8_t l, uint8_t *d );
00043       // --------------------------------------------------------------------
00044       inline uint8_t msg_id()
00045       { return read<OsModel, block_data_t, uint8_t>( buffer ); };
00046       // --------------------------------------------------------------------
00047       inline void set_msg_id( uint8_t id )
00048       { write<OsModel, block_data_t, uint8_t>( buffer, id ); }
00049       // --------------------------------------------------------------------
00050       inline uint8_t path_idx()
00051       { return read<OsModel, block_data_t, uint8_t>(buffer + IDX_POS); }
00052       // --------------------------------------------------------------------
00053       inline void set_path_idx( uint8_t idx )
00054       { write<OsModel, block_data_t, uint8_t>(buffer + IDX_POS, idx); }
00055       // --------------------------------------------------------------------
00056       inline void dec_path_idx( void )
00057       { set_path_idx( path_idx() - 1 );  }
00058       // --------------------------------------------------------------------
00059       inline void inc_path_idx( void )
00060       { set_path_idx( path_idx() + 1 );  }
00061       // --------------------------------------------------------------------
00062       inline uint16_t source()
00063       { return read<OsModel, block_data_t, uint16_t>(buffer + SOURCE_POS); }
00064       // --------------------------------------------------------------------
00065       inline void set_source( uint16_t src )
00066       { write<OsModel, block_data_t, uint16_t>(buffer + SOURCE_POS, src); }
00067       // --------------------------------------------------------------------
00068       inline uint16_t destination()
00069       { return read<OsModel, block_data_t, uint16_t>(buffer + DEST_POS); }
00070       // --------------------------------------------------------------------
00071       inline void set_destination( uint16_t dest )
00072       { write<OsModel, block_data_t, uint16_t>(buffer + DEST_POS, dest); }
00073       // --------------------------------------------------------------------
00074       inline void set_path( Path& p )
00075       {
00076          // TODO: Use serialization!
00077          memcpy( buffer + PATH_POS, &p, sizeof(p) );
00078       }
00079       // -----------------------------------------------------------------------
00080       inline Path* path( void )
00081       {
00082          // TODO: Use serialization!
00083          return (Path*)(buffer + PATH_POS);
00084       }
00085       // --------------------------------------------------------------------
00086       inline uint8_t payload_size()
00087       { return read<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_POS); }
00088       // -----------------------------------------------------------------------
00089       inline void set_payload( uint8_t len, block_data_t* data )
00090       { memcpy( buffer + PAYLOAD_POS + 1, data, len ); }
00091       // -----------------------------------------------------------------------
00092       inline block_data_t* payload( void )
00093       { return buffer + PAYLOAD_POS + 1; }
00094       // --------------------------------------------------------------------
00095       inline size_t buffer_size()
00096       { return PAYLOAD_POS + 1 + payload_size(); }
00097 
00098    private:
00099       enum data_positions
00100       {
00101          MSG_ID_POS  = 0,
00102          IDX_POS     = 1,
00103          SOURCE_POS  = 2,
00104          DEST_POS    = 4,
00105          PATH_POS    = 6,
00106          PAYLOAD_POS = 6 + sizeof(Path)
00107       };
00108 
00109       block_data_t buffer[Radio::MAX_MESSAGE_LENGTH];
00110 
00111    };
00112    // -----------------------------------------------------------------------
00113    template <typename OsModel_P,
00114              typename Radio_P,
00115              typename Path_P>
00116    DYMORoutingMessage<OsModel_P, Radio_P, Path_P>::
00117    DYMORoutingMessage()
00118    {};
00119    // -----------------------------------------------------------------------
00120    template <typename OsModel_P,
00121              typename Radio_P,
00122              typename Path_P>
00123    DYMORoutingMessage<OsModel_P, Radio_P, Path_P>::
00124    DYMORoutingMessage( uint8_t message_id,
00125                       uint16_t source, uint16_t destination,
00126                       uint8_t path_idx, uint8_t len, uint8_t *data )
00127    {
00128       set_msg_id( message_id );
00129       set_path_idx( path_idx );
00130       set_source( source );
00131       set_destination( destination );
00132       set_payload( len, data );
00133    };
00134 
00135 }
00136 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines