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 set_timestamp( 0 ); 00033 } 00034 // -------------------------------------------------------------------- 00035 inline message_id_t msg_id() 00036 { return read<OsModel, block_data_t, message_id_t>( buffer + MSG_ID_POS ); }; 00037 // -------------------------------------------------------------------- 00038 inline uint8_t seq_no() 00039 { return read<OsModel, block_data_t, uint8_t>(buffer + SEQ_NO_POS); } 00040 // -------------------------------------------------------------------- 00041 inline uint8_t payload_size() 00042 { return read<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_SIZE_POS); } 00043 // -------------------------------------------------------------------- 00044 inline node_id_t source() 00045 { return read<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS); } 00046 // -------------------------------------------------------------------- 00047 inline void set_source( node_id_t src ) 00048 { write<OsModel, block_data_t, node_id_t>(buffer + SOURCE_POS, src); } 00049 // ----------------------------------------------------------------------- 00050 inline node_id_t dest() 00051 { return read<OsModel, block_data_t, node_id_t>(buffer + DESTINATION_POS); } 00052 // -------------------------------------------------------------------- 00053 inline void set_dest( node_id_t dest ) 00054 { write<OsModel, block_data_t, node_id_t>(buffer + DESTINATION_POS, dest); } 00055 // ----------------------------------------------------------------------- 00056 inline uint16_t timestamp() 00057 { return read<OsModel, block_data_t, node_id_t>(buffer + TIMESTAMP_POS); } 00058 // -------------------------------------------------------------------- 00059 inline void set_timestamp( uint16_t timestamp ) 00060 { write<OsModel, block_data_t, node_id_t>(buffer + TIMESTAMP_POS, timestamp); } 00061 // ----------------------------------------------------------------------- 00062 00063 inline void set_payload( uint8_t len, block_data_t* data ) 00064 { 00065 set_payload_size( len ); 00066 if (len > 0) 00067 memcpy( buffer + PAYLOAD_POS, data, len ); 00068 } 00069 // ----------------------------------------------------------------------- 00070 inline block_data_t* payload( void ) 00071 { return buffer + PAYLOAD_POS; } 00072 // -------------------------------------------------------------------- 00073 inline uint8_t buffer_size() 00074 { return PAYLOAD_POS + payload_size(); }; 00075 inline void set_msg_id( message_id_t id ) 00076 { write<OsModel, block_data_t, message_id_t>( buffer+MSG_ID_POS, id ); } 00077 00078 00079 private: 00080 static uint8_t cur_seq_no; 00081 00082 inline void set_payload_size( uint8_t len ) 00083 { write<OsModel, block_data_t, uint8_t>(buffer + PAYLOAD_SIZE_POS, len); } 00084 // -------------------------------------------------------------------- 00085 inline void set_seq_no( uint8_t seq_no ) 00086 { write<OsModel, block_data_t, uint8_t>(buffer + SEQ_NO_POS, seq_no); } 00087 // -------------------------------------------------------------------- 00088 // -------------------------------------------------------------------- 00089 00090 enum data_positions 00091 { 00092 MSG_ID_POS = 0, 00093 SOURCE_POS = sizeof( message_id_t ), 00094 DESTINATION_POS = SOURCE_POS + sizeof( node_id_t ), 00095 TIMESTAMP_POS = DESTINATION_POS + sizeof( uint16_t ), 00096 SEQ_NO_POS = TIMESTAMP_POS + sizeof( node_id_t ), 00097 PAYLOAD_SIZE_POS = SEQ_NO_POS + sizeof( uint8_t ), 00098 PAYLOAD_POS = PAYLOAD_SIZE_POS + sizeof( uint8_t ), 00099 }; 00100 00101 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH]; 00102 00103 public: 00104 enum { 00105 00106 MAX_MESSAGE_LENGTH = Radio::MAX_MESSAGE_LENGTH, 00107 MAX_PAYLOAD_LENGTH = Radio::MAX_MESSAGE_LENGTH - PAYLOAD_POS,// Maximal length of the payload. 00108 HEADER_LENGTH = PAYLOAD_POS, 00109 }; 00110 }; 00111 00112 // ----------------------------------------------------------------------- 00113 00114 template<typename OsModel_P, typename Radio_P> 00115 uint8_t 00116 CommunicationMessage<OsModel_P, Radio_P>::cur_seq_no = 0; 00117 } 00118 #endif