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_DSR_ROUTE_DISCOVERY_MSG_H__ 00020 #define __ALGORITHMS_ROUTING_DSR_ROUTE_DISCOVERY_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 DsrRouteDiscoveryMessage 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 DsrRouteDiscoveryMessage() {}; 00042 // ----------------------------------------------------------------------- 00043 inline DsrRouteDiscoveryMessage( uint8_t msg_id, 00044 uint8_t hops, uint16_t seq_nr, 00045 uint16_t source, uint16_t destination, 00046 uint8_t path_idx ); 00047 // -------------------------------------------------------------------- 00048 message_id_t msg_id() 00049 { return read<OsModel, block_data_t, message_id_t>( buffer ); }; 00050 // -------------------------------------------------------------------- 00051 void set_msg_id( message_id_t id ) 00052 { write<OsModel, block_data_t, message_id_t>( buffer, id ); } 00053 // -------------------------------------------------------------------- 00054 uint8_t hops() 00055 { return read<OsModel, block_data_t, uint8_t>(buffer + HOPS_POS); } 00056 // -------------------------------------------------------------------- 00057 void set_hops( uint8_t hops ) 00058 { write<OsModel, block_data_t, uint8_t>(buffer + HOPS_POS, hops); } 00059 // -------------------------------------------------------------------- 00060 uint16_t sequence_nr() 00061 { return read<OsModel, block_data_t, uint16_t>(buffer + SEQ_POS); } 00062 // -------------------------------------------------------------------- 00063 void set_sequence_nr( uint16_t seq ) 00064 { write<OsModel, block_data_t, uint16_t>(buffer + SEQ_POS, seq); } 00065 // -------------------------------------------------------------------- 00066 node_id_t source() 00067 { return read<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS); } 00068 // -------------------------------------------------------------------- 00069 void set_source( node_id_t src ) 00070 { write<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS, src); } 00071 // -------------------------------------------------------------------- 00072 node_id_t destination() 00073 { return read<OsModel, block_data_t, node_id_t>(buffer + DEST_POS); } 00074 // -------------------------------------------------------------------- 00075 void set_destination( node_id_t dest ) 00076 { write<OsModel, block_data_t, node_id_t>(buffer + DEST_POS, dest); } 00077 // -------------------------------------------------------------------- 00078 uint8_t path_idx() 00079 { return read<OsModel, block_data_t, uint8_t>(buffer + IDX_POS); } 00080 // -------------------------------------------------------------------- 00081 void set_path_idx( uint8_t idx ) 00082 { write<OsModel, block_data_t, uint8_t>(buffer + IDX_POS, idx); } 00083 // ----------------------------------------------------------------------- 00084 void dec_path_idx( void ) 00085 { set_path_idx( path_idx() - 1 ); } 00086 // ----------------------------------------------------------------------- 00087 void inc_path_idx( void ) 00088 { set_path_idx( path_idx() + 1 ); } 00089 // -------------------------------------------------------------------- 00090 uint8_t entry_cnt() 00091 { return read<OsModel, block_data_t, uint8_t>(buffer + ENTRY_CNT_POS); } 00092 // -------------------------------------------------------------------- 00093 void set_entry_cnt( uint8_t entry_cnt ) 00094 { write<OsModel, block_data_t, uint8_t>(buffer + ENTRY_CNT_POS, entry_cnt); } 00095 // -------------------------------------------------------------------- 00096 void set_path( Path& p ) 00097 { 00098 int idx = 0; 00099 for ( PathIterator it = p.begin(); it != p.end(); ++it ) 00100 { 00101 int offset = PATH_POS + (idx * sizeof(node_id_t)); 00102 write<OsModel, block_data_t, node_id_t>( buffer + offset, *it ); 00103 idx++; 00104 } 00105 set_entry_cnt( idx ); 00106 } 00107 // ----------------------------------------------------------------------- 00108 void path( Path& p ) 00109 { 00110 p.clear(); 00111 for ( int i = 0; i < entry_cnt(); i++ ) 00112 { 00113 node_id_t node; 00114 int offset = PATH_POS + (i * sizeof(node_id_t)); 00115 read<OsModel, block_data_t, node_id_t>( buffer + offset, node ); 00116 p.push_back( node ); 00117 } 00118 } 00119 // ----------------------------------------------------------------------- 00120 uint8_t buffer_size( void ) 00121 { 00122 return PATH_POS + (entry_cnt() * sizeof(node_id_t)); 00123 } 00124 00125 private: 00126 enum data_positions 00127 { 00128 MSG_ID_POS = 0, 00129 HOPS_POS = MSG_ID_POS + sizeof(message_id_t), 00130 SEQ_POS = HOPS_POS + 1, 00131 SOURCE_POS = SEQ_POS + 2, 00132 DEST_POS = SOURCE_POS + sizeof(node_id_t), 00133 IDX_POS = DEST_POS + sizeof(node_id_t), 00134 ENTRY_CNT_POS = IDX_POS + 1, 00135 PATH_POS = ENTRY_CNT_POS + 1 00136 }; 00137 00138 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH]; 00139 }; 00140 // ----------------------------------------------------------------------- 00141 template <typename OsModel_P, 00142 typename Radio_P, 00143 typename Path_P> 00144 DsrRouteDiscoveryMessage<OsModel_P, Radio_P, Path_P>:: 00145 DsrRouteDiscoveryMessage( uint8_t msg_id, uint8_t hops, uint16_t seq_nr, 00146 uint16_t source, uint16_t destination, 00147 uint8_t path_idx ) 00148 { 00149 set_msg_id( msg_id ); 00150 set_hops( hops ); 00151 set_sequence_nr( seq_nr ); 00152 set_source( source ); 00153 set_destination( destination ); 00154 set_path_idx( path_idx ); 00155 } 00156 00157 } 00158 #endif