Wiselib
wiselib.testing/algorithms/routing/olsr/olsr_broadcast_hello_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_BROADCAST_HELLO_MSG_H__
00020 #define __ALGORITHMS_ROUTING_OLSR_BROADCAST_HELLO_MSG_H__
00021 
00022 #include "util/serialization/simple_types.h"
00023 
00024 namespace wiselib
00025 {
00026 
00027 
00028 
00029    template<typename OsModel_P,
00030             typename Radio_P,
00031             typename RoutingTableEntry_P>
00032    class OlsrBroadcastHelloMessage
00033    {
00034    public:
00035 
00036       typedef OsModel_P OsModel;
00037       typedef Radio_P Radio;
00038       typedef RoutingTableEntry_P RoutingTableEntry;
00039       typedef typename Radio::block_data_t block_data_t;
00040       typedef typename Radio::node_id_t node_id_t;
00041       // --------------------------------------------------------------------
00042       inline OlsrBroadcastHelloMessage();
00043       // --------------------------------------------------------------------
00044 
00045       inline uint8_t msg_id()
00046       { return read<OsModel, block_data_t, uint8_t>( buffer ); }
00047 
00048       inline uint8_t vtime()
00049       { return read<OsModel, block_data_t, uint8_t>( buffer + VTIME_POS); }
00050 
00051       inline uint16_t msg_size()
00052       { return read<OsModel, block_data_t, uint16_t>( buffer + MSG_SIZE_POS); }
00053 
00054       inline node_id_t originator_addr()
00055       { return read<OsModel, block_data_t, uint32_t>( buffer + ORIGINATOR_ADDR_POS); }
00056 
00057       inline uint8_t ttl()
00058       { return read<OsModel, block_data_t, uint8_t>( buffer + TTL_POS); }
00059 
00060       inline uint8_t hop_count()
00061       { return read<OsModel, block_data_t, uint8_t>( buffer + HOP_COUNT_POS); }
00062 
00063       inline uint16_t msg_seq_num()
00064       { return read<OsModel, block_data_t, uint16_t>( buffer + MSG_SEQ_POS); }
00065 
00066       inline uint8_t htime()
00067       { return read<OsModel, block_data_t, uint8_t>( buffer + HTIME_POS); }
00068 
00069       inline uint8_t willingness()
00070       { return read<OsModel, block_data_t, uint8_t>( buffer + WILLINGNESS_POS); }
00071 
00072       inline int hello_msgs_count()
00073       { return read<OsModel, block_data_t, uint8_t>( buffer + HELLO_MSGS_COUNT_POS); }
00074 
00075       inline uint8_t link_code(int count, int nb_addrs_count_before)
00076       { return read<OsModel, block_data_t, uint8_t>( buffer + LINK_CODE_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before); }
00077 
00078       inline uint16_t link_msg_size(int count, int nb_addrs_count_before)
00079       { return read<OsModel, block_data_t, uint16_t>( buffer + LINK_MSG_SIZE_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before); }
00080 
00081       inline int neighbor_addr_count(int count, int nb_addrs_count_before)
00082       { return read<OsModel, block_data_t, uint8_t>( buffer + NEIGHBOR_ADDR_COUNT_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before); }
00083 
00084      inline node_id_t neighbor_addr_list( int count, int index , int nb_addrs_count_before)
00085       {
00086         unsigned char* a = NULL;
00087         char* b = NULL;
00088         a = buffer + NEIGHBOR_ADDR_LIST_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before + 4*index;
00089         b = (char*)a;
00090         char c = *b;
00091         return int(c);
00092       }
00093 
00094       inline uint8_t neighbor_addr_list_size()
00095       { return read<OsModel, block_data_t, uint8_t>( buffer + NEIGHBOR_ADDR_LIST_FIRST_POS); }
00096 
00097      inline size_t buffer_size()
00098       { return NEIGHBOR_ADDR_LIST_FIRST_POS + 1 + neighbor_addr_list_size(); }
00099 
00100       // --------------------------------------------------------------------
00101 
00102       inline void set_msg_id( uint8_t id )
00103       { write<OsModel, block_data_t, uint8_t>( buffer, id ); }
00104 
00105       inline void set_vtime(uint8_t vtime)
00106       { write<OsModel, block_data_t, uint8_t>( buffer + VTIME_POS, vtime); }
00107 
00108       inline void set_msg_size(uint16_t msg_size)
00109       { write<OsModel, block_data_t, uint16_t>( buffer + MSG_SIZE_POS, msg_size); }
00110 
00111       inline void set_originator_addr(uint32_t originator_addr)
00112       { write<OsModel, block_data_t, uint32_t>( buffer + ORIGINATOR_ADDR_POS, originator_addr); }
00113 
00114       inline void set_ttl(uint8_t ttl)
00115       { write<OsModel, block_data_t, uint8_t>( buffer + TTL_POS, ttl); }
00116 
00117       inline void set_hop_count(uint8_t hop_count)
00118       { write<OsModel, block_data_t, uint8_t>( buffer + HOP_COUNT_POS, hop_count); }
00119 
00120       inline void set_msg_seq_num(uint16_t msg_sequence)
00121       { write<OsModel, block_data_t, uint16_t>( buffer + MSG_SEQ_POS, msg_sequence); }
00122 
00123       inline void set_htime(uint8_t htime)
00124       { write<OsModel, block_data_t, uint8_t>( buffer + HTIME_POS, htime); }
00125 
00126       inline void set_willingness(uint8_t willingness)
00127       { write<OsModel, block_data_t, uint8_t>( buffer + WILLINGNESS_POS, willingness); }
00128 
00129       inline void set_hello_msgs_count(uint8_t hello_msgs_count)
00130       { write<OsModel, block_data_t, uint8_t>( buffer + HELLO_MSGS_COUNT_POS, hello_msgs_count); }
00131 
00132       inline void set_link_code(int count, int nb_addrs_count_before, uint8_t link_code)        // hello_msg[count].link_code
00133       { write<OsModel, block_data_t, uint8_t>( buffer + LINK_CODE_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before, link_code); }
00134 
00135       inline void set_link_msg_size(int count, int nb_addrs_count_before, uint32_t* link_msg_size)  // hello_msg[count].link_msg_size
00136       { memcpy( buffer + LINK_MSG_SIZE_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before, link_msg_size, 2); }
00137 
00138       inline void set_neighbor_addr_count(int count, int nb_addrs_count_before, int neighbor_addr_count) // hello_msg[count].neighbor_addr_count
00139       {
00140         write<OsModel, block_data_t, int>( buffer + NEIGHBOR_ADDR_COUNT_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before, neighbor_addr_count);
00141       }
00142 
00143       inline void set_neighbor_addr_list(int count, int nb_addrs_count_before, int index, node_id_t* data )                //hello_msg[count].neighbor_addr_list[index]
00144       { memcpy( buffer + NEIGHBOR_ADDR_LIST_FIRST_POS + 4*(count-1) + 4*nb_addrs_count_before + 4*index, data, 4 );  }
00145 
00146       // --------------------------------------------------------------------
00147 
00148    private:
00149       enum data_positions
00150       {
00151       MSG_ID_POS                 = 0,
00152       VTIME_POS                  = 1,
00153       MSG_SIZE_POS               = 2,
00154       ORIGINATOR_ADDR_POS        = 4,
00155       TTL_POS                 = 8,
00156       HOP_COUNT_POS              = 9,
00157       MSG_SEQ_POS                = 10,
00158       HTIME_POS                  = 12,
00159       WILLINGNESS_POS            = 13,
00160       HELLO_MSGS_COUNT_POS       = 14,
00161       LINK_CODE_FIRST_POS        = 15,
00162       LINK_MSG_SIZE_FIRST_POS       = 16,
00163       NEIGHBOR_ADDR_COUNT_FIRST_POS    = 18,
00164       NEIGHBOR_ADDR_LIST_FIRST_POS  = 19
00165       };
00166 
00167       block_data_t buffer[Radio::MAX_MESSAGE_LENGTH];
00168    };
00169    // -----------------------------------------------------------------------
00170    template<typename OsModel_P,
00171             typename Radio_P,
00172             typename RoutingTableEntry_P>
00173    OlsrBroadcastHelloMessage<OsModel_P, Radio_P, RoutingTableEntry_P>::
00174    OlsrBroadcastHelloMessage()
00175    {
00176      // Initialization of the msg_id and etc.
00177      // Assignment of the value is given at the instantiation of class BroadcastHelloMessage
00178       set_msg_id(0);
00179       set_vtime (0);
00180       set_originator_addr( Radio::NULL_NODE_ID );
00181       set_ttl(0);
00182       set_hop_count(0);
00183       set_msg_seq_num(0);
00184       set_htime(0);
00185       set_willingness(0);
00186    }
00187 
00188 }
00189 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines