Wiselib
|
00001 /* 00002 * File: reliablemsg.h 00003 * Author: amaxilatis 00004 * 00005 * Created on July 27, 2010 00006 */ 00007 00008 #ifndef _RELIABLEMSG_H 00009 #define _RELIABLEMSG_H 00010 00011 #include <util/serialization/serialization.h> 00012 00013 namespace wiselib { 00014 00015 template < typename OsModel_P, typename Radio_P> // Template Parameters: and the underluying Radio (Not the Reliable Radio) 00016 class ReliableMsg { 00017 typedef OsModel_P OsModel; 00018 typedef Radio_P Radio; 00019 typedef typename Radio::block_data_t block_data_t; 00020 typedef typename Radio::message_id_t message_id_t; 00021 00022 public: 00023 00024 // message ids 00025 00026 enum message_types { 00027 RELIABLE_MESSAGE = 101, // message with ack demand 00028 ACK_MESSAGE = 102, // ack message response 00029 BROADCAST_MESSAGE = 103 // message whithout ack demand 00030 00031 }; 00032 00033 enum data_positions { 00034 MSG_ID_POS = 0, // message id position inside the message [uint8] 00035 SEQ_NUM_POS = 1, // seq_number position inside the message [1]+[2] [uint16] 00036 PAYLOAD_POS = 3, // start of message payload 00037 }; 00038 00039 enum Restrictions { 00040 MAX_MESSAGE_LENGTH = Radio::MAX_MESSAGE_LENGTH - PAYLOAD_POS 00042 }; 00043 00044 // -------------------------------------------------------------------- 00045 00046 ReliableMsg() { 00047 set_msg_id(BROADCAST_MESSAGE); 00048 set_seq_number(0); 00049 set_payload_size(0); 00050 }; 00051 00052 // -------------------------------------------------------------------- 00053 // get the message id 00054 00055 inline message_id_t msg_id() { 00056 return read<OsModel, block_data_t, uint8_t > (buffer + MSG_ID_POS); 00057 }; 00058 // -------------------------------------------------------------------- 00059 00060 // set the message id 00061 00062 inline void set_msg_id(message_id_t id) { 00063 write<OsModel, block_data_t, uint8_t > (buffer + MSG_ID_POS, id); 00064 }; 00065 // -------------------------------------------------------------------- 00066 // get the seq_number_ 00067 00068 inline uint16_t seq_number() { 00069 return read<OsModel, block_data_t, uint16_t > (buffer + SEQ_NUM_POS); 00070 }; 00071 // -------------------------------------------------------------------- 00072 // set the seq_number_ 00073 00074 inline void set_seq_number(uint16_t seq_number) { 00075 write<OsModel, block_data_t, uint16_t > (buffer + SEQ_NUM_POS, seq_number); 00076 } 00077 // -------------------------------------------------------------------- 00078 00079 inline uint8_t payload_size() { 00080 return read<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS); 00081 } 00082 // -------------------------------------------------------------------- 00083 00084 inline uint8_t* payload() { 00085 return buffer + PAYLOAD_POS + 1; 00086 } 00087 // -------------------------------------------------------------------- 00088 00089 inline void set_payload(uint8_t len, uint8_t *buf) { 00090 uint8_t loc[len]; 00091 memcpy(loc,buf,len); 00092 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00093 memcpy(buffer + PAYLOAD_POS + 1, loc, len); 00094 } 00095 // -------------------------------------------------------------------- 00096 00097 inline size_t buffer_size() { 00098 return PAYLOAD_POS + 1 + payload_size(); 00099 } 00100 00101 // returns the size of the ack message containing #count sequence number 00102 00103 00104 00105 00106 00107 00108 00109 private: 00110 00111 inline void set_payload_size(uint8_t len) { 00112 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00113 } 00114 00115 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH ]; // buffer for the message data 00116 00117 00118 }; 00119 00120 } 00121 00122 #endif /* _RELIABLEMSG_H */