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