Wiselib
|
00001 #ifndef __ALGORITHMS_END_TO_END_COMMUNICATION_END_TO_END_COMMUNICATION_MSG_H__ 00002 #define __ALGORITHMS_END_TO_END_COMMUNICATION_END_TO_END_COMMUNICATION_MSG_H__ 00003 00004 #include "util/serialization/simple_types.h" 00005 00006 namespace wiselib 00007 { 00008 00009 template<typename OsModel_P, typename Radio_P> 00010 class CommunicationMessage 00011 { 00012 public: 00013 typedef OsModel_P OsModel; 00014 typedef Radio_P Radio; 00015 00016 typedef typename Radio::block_data_t block_data_t; 00017 typedef typename Radio::node_id_t node_id_t; 00018 typedef typename Radio::message_id_t message_id_t; 00019 00020 enum { 00021 END_TO_END_MESSAGE = 245, 00022 NODE_IN_CLUSTER = 246, 00023 }; 00024 //-------------------------------------------------------------------- 00025 inline CommunicationMessage( node_id_t src=Radio::NULL_NODE_ID, node_id_t dest=Radio::NULL_NODE_ID ) 00026 { 00027 set_msg_id( END_TO_END_MESSAGE ); 00028 set_payload_size( 0 ); 00029 set_seq_no( cur_seq_no++ ); 00030 set_source( src ); 00031 set_dest( dest ); 00032 } 00033 // -------------------------------------------------------------------- 00034 inline message_id_t msg_id() 00035 { return read<OsModel, block_data_t, message_id_t>( buffer + MSG_ID_POS ); }; 00036 // -------------------------------------------------------------------- 00037 inline uint8_t seq_no() 00038 { return read<OsModel, block_data_t, uint8_t>(buffer + SEQ_NO_POS); } 00039 // -------------------------------------------------------------------- 00040 inline uint8_t payload_size() 00041 { return read<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_SIZE_POS); } 00042 // -------------------------------------------------------------------- 00043 inline node_id_t source() 00044 { return read<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS); } 00045 // -------------------------------------------------------------------- 00046 inline void set_source( node_id_t src ) 00047 { write<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS, src); } 00048 // ----------------------------------------------------------------------- 00049 inline node_id_t dest() 00050 { return read<OsModel, block_data_t, node_id_t>(buffer + DESTINATION_POS); } 00051 // -------------------------------------------------------------------- 00052 inline void set_dest( node_id_t dest ) 00053 { write<OsModel, block_data_t, node_id_t>(buffer + DESTINATION_POS, dest); } 00054 // ----------------------------------------------------------------------- 00055 00056 inline void set_payload( uint8_t len, block_data_t* data ) 00057 { 00058 set_payload_size( len ); 00059 if (len > 0) 00060 memcpy( buffer + PAYLOAD_POS, data, len ); 00061 } 00062 // ----------------------------------------------------------------------- 00063 inline block_data_t* payload( void ) 00064 { return buffer + PAYLOAD_POS; } 00065 // -------------------------------------------------------------------- 00066 inline uint8_t buffer_size() 00067 { return PAYLOAD_POS + payload_size(); }; 00068 inline void set_msg_id( message_id_t id ) 00069 { write<OsModel, block_data_t, message_id_t>( buffer+MSG_ID_POS, id ); } 00070 00071 00072 private: 00073 static uint8_t cur_seq_no; 00074 00075 inline void set_payload_size( uint8_t len ) 00076 { write<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_SIZE_POS, len); } 00077 // -------------------------------------------------------------------- 00078 inline void set_seq_no( uint8_t seq_no ) 00079 { write<OsModel, block_data_t, uint8_t>(buffer + SEQ_NO_POS, seq_no); } 00080 // -------------------------------------------------------------------- 00081 // -------------------------------------------------------------------- 00082 00083 enum data_positions 00084 { 00085 MSG_ID_POS = 0, 00086 SOURCE_POS = sizeof( message_id_t ), 00087 DESTINATION_POS = SOURCE_POS + sizeof( node_id_t ), 00088 SEQ_NO_POS = DESTINATION_POS + sizeof( node_id_t ), 00089 PAYLOAD_SIZE_POS = SEQ_NO_POS + sizeof( uint8_t ), 00090 PAYLOAD_POS = PAYLOAD_SIZE_POS + sizeof( uint8_t ), 00091 }; 00092 00093 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH]; 00094 00095 public: 00096 enum { 00097 00098 MAX_MESSAGE_LENGTH = Radio::MAX_MESSAGE_LENGTH, 00099 MAX_PAYLOAD_LENGTH = Radio::MAX_MESSAGE_LENGTH - PAYLOAD_POS,// Maximal length of the payload. 00100 HEADER_LENGTH = PAYLOAD_POS, 00101 }; 00102 }; 00103 00104 // ----------------------------------------------------------------------- 00105 00106 template<typename OsModel_P, typename Radio_P> 00107 uint8_t 00108 CommunicationMessage<OsModel_P, Radio_P>::cur_seq_no = 0; 00109 } 00110 #endif