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 ** Improved by: Christoph Knecht, University of Bern 2010 ** 00020 ***************************************************************************/ 00021 #ifndef DIFFIE_HELLMAN_MESSAGE_H 00022 #define DIFFIE_HELLMAN_MESSAGE_H 00023 00024 #include "util/serialization/simple_types.h" 00025 00026 namespace wiselib 00027 { 00028 00029 template<typename OsModel_P, 00030 typename Radio_P> 00031 class DiffieHellmanMessage 00032 { 00033 public: 00034 typedef OsModel_P OsModel; 00035 typedef Radio_P Radio; 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 inline DiffieHellmanMessage(); 00043 // -------------------------------------------------------------------- 00044 inline message_id_t msg_id() 00045 { return read<OsModel, block_data_t, message_id_t>( buffer ); }; 00046 // -------------------------------------------------------------------- 00047 inline void set_msg_id( message_id_t id ) 00048 { write<OsModel, block_data_t, message_id_t>( buffer, id ); } 00049 // -------------------------------------------------------------------- 00050 inline node_id_t node_id() 00051 { return read<OsModel, block_data_t, node_id_t>(buffer + NODE_ID_POS); } 00052 // -------------------------------------------------------------------- 00053 inline void set_node_id( node_id_t id ) 00054 { write<OsModel, block_data_t, node_id_t>(buffer + NODE_ID_POS, id); } 00055 // -------------------------------------------------------------------- 00056 inline node_id_t dest_id() 00057 { return read<OsModel, block_data_t, node_id_t>(buffer + DEST_ID_POS); } 00058 // -------------------------------------------------------------------- 00059 inline void set_dest_id( node_id_t id ) 00060 { write<OsModel, block_data_t, node_id_t>(buffer + DEST_ID_POS, id); } 00061 // -------------------------------------------------------------------- 00062 inline seq_nr_t seq_nr() 00063 { return read<OsModel, block_data_t, seq_nr_t>(buffer + SEQ_NR_POS); } 00064 // -------------------------------------------------------------------- 00065 inline void set_seq_nr( seq_nr_t seq ) 00066 { write<OsModel, block_data_t, seq_nr_t>(buffer + SEQ_NR_POS, seq); } 00067 // -------------------------------------------------------------------- 00068 inline size_t payload_size() 00069 { return read<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS); } 00070 // -------------------------------------------------------------------- 00071 inline block_data_t* payload() 00072 { return buffer + PAYLOAD_POS + sizeof(size_t); } 00073 // -------------------------------------------------------------------- 00074 inline void set_payload( size_t len, block_data_t *buf ) 00075 { 00076 write<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS, len); 00077 memcpy( buffer + PAYLOAD_POS + sizeof(size_t), buf, len); 00078 } 00079 // -------------------------------------------------------------------- 00080 inline size_t buffer_size() 00081 { return PAYLOAD_POS + sizeof(size_t) + payload_size(); } 00082 00083 private: 00084 enum data_positions 00085 { 00086 NODE_ID_POS = sizeof(message_id_t), 00087 DEST_ID_POS = NODE_ID_POS + sizeof(node_id_t), 00088 SEQ_NR_POS = DEST_ID_POS + sizeof(node_id_t), 00089 PAYLOAD_POS = SEQ_NR_POS + sizeof(seq_nr_t) 00090 }; 00091 00092 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH]; 00093 }; 00094 // ----------------------------------------------------------------------- 00095 template<typename OsModel_P, 00096 typename Radio_P> 00097 DiffieHellmanMessage<OsModel_P, Radio_P>:: 00098 DiffieHellmanMessage() 00099 { 00100 set_msg_id( 0 ); 00101 set_node_id( 0 ); 00102 size_t len = 0; 00103 write<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS, len); 00104 } 00105 } 00106 #endif