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 __TRIANGULATION_MSG_H__ 00020 #define __TRIANGULATION_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 Arithmatic_P> 00030 class TriangulationMessage 00031 { 00032 public: 00033 typedef OsModel_P OsModel; 00034 typedef Radio_P Radio; 00035 typedef Arithmatic_P Arithmatic; 00036 typedef uint16_t seq_nr_t; 00037 typedef typename Radio::node_id_t node_id_t; 00038 typedef typename Radio::block_data_t block_data_t; 00039 typedef typename Radio::size_t size_t; 00040 typedef typename Radio::message_id_t message_id_t; 00041 00042 // bool ack; 00043 node_id_t destination; 00044 00045 // -------------------------------------------------------------------- 00046 inline TriangulationMessage(); 00047 // -------------------------------------------------------------------- 00048 inline message_id_t msg_id() 00049 { return read<OsModel, block_data_t, message_id_t>( buffer ); }; 00050 // -------------------------------------------------------------------- 00051 inline void set_msg_id( message_id_t id ) 00052 { write<OsModel, block_data_t, message_id_t>( buffer, id ); } 00053 // -------------------------------------------------------------------- 00054 inline node_id_t node_id() 00055 { return read<OsModel, block_data_t, node_id_t>(buffer + NODE_ID_POS); } 00056 // -------------------------------------------------------------------- 00057 inline void set_node_id( node_id_t id ) 00058 { write<OsModel, block_data_t, node_id_t>(buffer + NODE_ID_POS, id); } 00059 // -------------------------------------------------------------------- 00060 inline seq_nr_t seq_nr() 00061 { return read<OsModel, block_data_t, seq_nr_t>(buffer + SEQ_NR_POS); } 00062 // -------------------------------------------------------------------- 00063 inline void set_seq_nr( seq_nr_t seq ) 00064 { write<OsModel, block_data_t, seq_nr_t>(buffer + SEQ_NR_POS, seq); } 00065 // -------------------------------------------------------------------- 00066 00067 inline void set_trust(int t) 00068 { write<OsModel, block_data_t, int>(buffer + TRUST_POS, t); } 00069 00070 inline int trust() 00071 { return read<OsModel, block_data_t, int>(buffer + TRUST_POS); } 00072 00073 inline void set_result(int t) 00074 { write<OsModel, block_data_t, int>(buffer + RESULT_POS, t); } 00075 00076 inline int result() 00077 { return read<OsModel, block_data_t, int>(buffer + RESULT_POS); } 00078 00079 inline void set_id(node_id_t t) 00080 { write<OsModel, block_data_t, node_id_t>(buffer + ID_POS, t); } 00081 00082 inline node_id_t id() 00083 { return read<OsModel, block_data_t, node_id_t>(buffer + ID_POS); } 00084 00085 00086 // -------------------------------------------------------------------- 00087 inline Arithmatic distance() 00088 { return read<OsModel, block_data_t, Arithmatic>(buffer + DISTANCE_POS); } 00089 // -------------------------------------------------------------------- 00090 inline void set_distance( Arithmatic dist ) 00091 { write<OsModel, block_data_t, Arithmatic>(buffer + DISTANCE_POS, dist); } 00092 00093 inline size_t payload_size() 00094 { return read<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS); } 00095 // -------------------------------------------------------------------- 00096 inline block_data_t* payload() 00097 { return buffer + PAYLOAD_POS + sizeof(size_t); } 00098 // -------------------------------------------------------------------- 00099 inline void set_payload( size_t len, block_data_t *buf ) 00100 { 00101 write<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS, len); 00102 memcpy( buffer + PAYLOAD_POS + sizeof(size_t), buf, len); 00103 } 00104 // -------------------------------------------------------------------- 00105 inline size_t buffer_size() 00106 { return PAYLOAD_POS + sizeof(size_t) + payload_size(); } 00107 00108 private: 00109 enum data_positions 00110 { 00111 NODE_ID_POS = sizeof(message_id_t), 00112 SEQ_NR_POS = NODE_ID_POS + sizeof(node_id_t), 00113 TRUST_POS = SEQ_NR_POS + sizeof(seq_nr_t), 00114 RESULT_POS = TRUST_POS + sizeof(int), 00115 ID_POS = RESULT_POS + sizeof(int), 00116 DISTANCE_POS = TRUST_POS + sizeof(int), 00117 PAYLOAD_POS = DISTANCE_POS + sizeof(Arithmatic) 00118 }; 00119 00120 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH]; 00121 }; 00122 // ----------------------------------------------------------------------- 00123 template<typename OsModel_P, 00124 typename Radio_P, 00125 typename Arithmatic_P > 00126 TriangulationMessage<OsModel_P, Radio_P, Arithmatic_P>:: 00127 TriangulationMessage() 00128 { 00129 set_msg_id( 0 ); 00130 set_node_id( 0 ); 00131 size_t len = 0; 00132 // ack = false; 00133 destination = -1; 00134 write<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS, len); 00135 } 00136 00137 } 00138 #endif