Wiselib
|
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_DSDV_BROADCAST_MSG_H__ 00020 #define __ALGORITHMS_ROUTING_DSDV_BROADCAST_MSG_H__ 00021 00022 #include "algorithms/routing/dsdv/dsdv_routing_types.h" 00023 #include "util/serialization/simple_types.h" 00024 00025 namespace wiselib 00026 { 00027 00028 template<typename OsModel_P, 00029 typename Radio_P, 00030 int ENTRY_CNT = ((Radio_P::MAX_MESSAGE_LENGTH - 00031 sizeof(typename Radio_P::message_id_t) - 1) / 00032 (2 * sizeof(typename Radio_P::node_id_t) + 1))> 00033 class DsdvBroadcastMessage 00034 { 00035 public: 00036 enum { 00037 ENTRY_SIZE = (2 * sizeof(typename Radio_P::node_id_t) + 1), 00038 MAX_ENTRIES = ENTRY_CNT 00039 }; 00040 00041 typedef OsModel_P OsModel; 00042 typedef Radio_P Radio; 00043 typedef DsdvRoutingTableValue<OsModel, Radio> DsdvRtValue; 00044 00045 typedef typename Radio_P::message_id_t message_id_t; 00046 typedef typename Radio::block_data_t block_data_t; 00047 typedef typename Radio::node_id_t node_id_t; 00048 // -------------------------------------------------------------------- 00049 inline DsdvBroadcastMessage(); 00050 // -------------------------------------------------------------------- 00051 inline message_id_t msg_id() 00052 { return read<OsModel, block_data_t, message_id_t>( buffer ); }; 00053 // -------------------------------------------------------------------- 00054 inline void set_msg_id( message_id_t id ) 00055 { write<OsModel, block_data_t, message_id_t>( buffer, id ); } 00056 // -------------------------------------------------------------------- 00057 inline uint8_t entry_cnt() 00058 { return read<OsModel, block_data_t, uint8_t>(buffer + ENTRY_CNT_POS); } 00059 // -------------------------------------------------------------------- 00060 inline void set_entry_cnt( uint8_t cnt ) 00061 { write<OsModel, block_data_t, uint8_t>(buffer + ENTRY_CNT_POS, cnt); } 00062 // -------------------------------------------------------------------- 00063 inline void set_entry( int idx, node_id_t node, DsdvRtValue& entry ) 00064 { 00065 if ( idx >= ENTRY_CNT ) 00066 return; 00067 00068 int offset = DATA_POS + (idx * ENTRY_SIZE); 00069 write<OsModel, block_data_t, node_id_t>( buffer + offset, node ); 00070 write<OsModel, block_data_t, node_id_t>( buffer + offset + sizeof(node_id_t), entry.next_hop ); 00071 write<OsModel, block_data_t, uint8_t>( buffer + offset + sizeof(node_id_t) + sizeof(node_id_t), entry.hops ); 00072 }; 00073 // -------------------------------------------------------------------- 00074 inline bool entry( int idx, node_id_t& node, DsdvRtValue& entry ) 00075 { 00076 if (idx >= ENTRY_CNT) 00077 return false; 00078 00079 int offset = DATA_POS + (idx * ENTRY_SIZE); 00080 read<OsModel, block_data_t, node_id_t>( buffer + offset, node ); 00081 read<OsModel, block_data_t, node_id_t>( buffer + offset + sizeof(node_id_t), entry.next_hop ); 00082 read<OsModel, block_data_t, uint8_t>( buffer + offset + sizeof(node_id_t) + sizeof(node_id_t), entry.hops ); 00083 return true; 00084 }; 00085 // -------------------------------------------------------------------- 00086 inline size_t buffer_size() 00087 { return DATA_POS + entry_cnt() * ENTRY_SIZE; } 00088 00089 private: 00090 enum data_positions 00091 { 00092 MSG_ID_POS = 0, 00093 ENTRY_CNT_POS = 1, 00094 DATA_POS = 2 00095 }; 00096 00097 uint8_t buffer[DATA_POS + ENTRY_CNT * ENTRY_SIZE]; 00098 }; 00099 // ----------------------------------------------------------------------- 00100 template<typename OsModel_P, 00101 typename Radio_P, 00102 int ENTRY_CNT> 00103 DsdvBroadcastMessage<OsModel_P, Radio_P, ENTRY_CNT>:: 00104 DsdvBroadcastMessage() 00105 { 00106 set_msg_id( 0 ); 00107 set_entry_cnt( 0 ); 00108 } 00109 00110 } 00111 #endif