Wiselib
|
00001 /* 00002 * File: volumemsg.h 00003 * Author: amaxilatis 00004 * 00005 * Created on August 2, 2010, 1:35 PM 00006 */ 00007 00008 #ifndef _VOLUMEMSG_H 00009 #define _VOLUMEMSG_H 00010 00011 namespace wiselib { 00012 00013 template <typename OsModel_P,typename Radio_P > // Template Parameters: and the underluying Radio (Not the Reliable Radio) 00014 class VolumeMsg { 00015 typedef OsModel_P OsModel; 00016 typedef Radio_P Radio; 00017 00018 00019 typedef typename Radio::block_data_t block_data_t; 00020 typedef typename Radio::node_id_t node_id_t; 00021 typedef typename Radio::message_id_t message_id_t; 00022 public: 00023 00024 00025 00026 // message ids 00027 00028 enum message_types { 00029 VOLUME_MESSAGE = 201, // message that is part of a bigger payload 00030 SINGLE_MESSAGE = 202 // message containing a complete payload 00031 }; 00032 00033 enum { 00034 MSG_ID_POS = 0, // message id position inside the message [uint8] 00035 SEQ_NUM_POS = 1, // seq_number position inside the message [3]+[4] [uint16] 00036 FRAGMENT_POS = 3, // id of the fragment [uint8] 00037 TOTAL_FRAGMENTS_POS = 4, // total number of fragments that belong to the payload [uint8] 00038 PAYLOAD_POS = 5 // start of message payload 00039 00040 }; 00041 00042 enum{ 00043 FRAGMENT_SIZE = Radio::MAX_MESSAGE_LENGTH-PAYLOAD_POS-1, 00044 MESSAGE_LENGTH = Radio::MAX_MESSAGE_LENGTH, 00045 MAX_MESSAGE_LENGTH = (FRAGMENT_SIZE)*256 00046 }; 00047 00048 // -------------------------------------------------------------------- 00049 00050 VolumeMsg() { 00051 }; 00052 // -------------------------------------------------------------------- 00053 00054 VolumeMsg(node_id_t source, 00055 node_id_t destination, uint16_t seq_no, size_t payload_size, uint8_t * data) { 00056 00057 }; 00058 // -------------------------------------------------------------------- 00059 ~VolumeMsg(){ 00060 }; 00061 00062 // -------------------------------------------------------------------- 00063 // get the message id 00064 00065 inline message_id_t msg_id() { 00066 return read<OsModel, block_data_t, uint8_t > (buffer + MSG_ID_POS); 00067 }; 00068 // -------------------------------------------------------------------- 00069 00070 // set the message id 00071 00072 inline void set_msg_id(message_id_t id) { 00073 write<OsModel, block_data_t, uint8_t > (buffer + MSG_ID_POS, id); 00074 }; 00075 00076 // -------------------------------------------------------------------- 00077 // get the seq_number_ 00078 00079 inline uint16_t seq_number() { 00080 return read<OsModel, block_data_t, uint16_t > (buffer + SEQ_NUM_POS); 00081 }; 00082 // -------------------------------------------------------------------- 00083 // set the seq_number_ 00084 00085 inline void set_seq_number(uint16_t seq_number) { 00086 write<OsModel, block_data_t, uint16_t > (buffer + SEQ_NUM_POS, seq_number); 00087 } 00088 00089 // -------------------------------------------------------------------- 00090 // get the fragment number 00091 00092 inline uint8_t fragment_id() { 00093 return read<OsModel, block_data_t, uint8_t > (buffer + FRAGMENT_POS); 00094 }; 00095 // -------------------------------------------------------------------- 00096 // set the fragment_number 00097 00098 inline void set_fragment_id(uint8_t frag_number) { 00099 write<OsModel, block_data_t, uint8_t > (buffer + FRAGMENT_POS, frag_number); 00100 } 00101 00102 // -------------------------------------------------------------------- 00103 // get the total fragments number 00104 00105 inline uint8_t fragments() { 00106 return read<OsModel, block_data_t, uint8_t > (buffer + TOTAL_FRAGMENTS_POS); 00107 }; 00108 // -------------------------------------------------------------------- 00109 // set the fragment_number 00110 00111 inline void set_fragments(uint8_t fragments) { 00112 write<OsModel, block_data_t, uint8_t > (buffer + TOTAL_FRAGMENTS_POS, fragments); 00113 } 00114 // -------------------------------------------------------------------- 00115 00116 inline uint8_t payload_size() { 00117 return read<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS); 00118 } 00119 // -------------------------------------------------------------------- 00120 00121 inline uint8_t* payload() { 00122 return buffer + PAYLOAD_POS + 1; 00123 } 00124 // -------------------------------------------------------------------- 00125 00126 inline void set_payload(uint8_t len, uint8_t *buf) { 00127 uint8_t loc[len]; 00128 memcpy(loc,buf,len); 00129 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00130 memcpy(buffer + PAYLOAD_POS + 1, loc, len); 00131 } 00132 // -------------------------------------------------------------------- 00133 00134 inline size_t buffer_size() { 00135 return PAYLOAD_POS + 1 + payload_size(); 00136 } 00137 00138 // returns the size of the ack message containing #count sequence number 00139 00140 00141 00142 00143 00144 private: 00145 00146 inline void set_payload_size(uint8_t len) { 00147 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00148 } 00149 00150 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH ]; // buffer for the message data 00151 00152 00153 }; 00154 00155 } 00156 00157 00158 #endif /* _VOLUMEMSG_H */ 00159