Wiselib
wiselib.testing/algorithms/routing/autocast/autocast_message.h
Go to the documentation of this file.
00001 /************************************************************************
00002  ** This file is part of the TriSOS project.
00003  ** Copyright (C) 2009 University of Applied Sciences Lübeck
00004  ** ALL RIGHTS RESERVED.
00005  ************************************************************************/
00006 
00019 #ifndef __AutoCast_Message_H__
00020 #define __AutoCast_Message_H__
00021 
00022 // size of payload is 100 Bytes, it can be larger (it depend on the radio interface)
00023 #define MAX_MESSAGE_SIZE 125
00024 
00025 #define MSG_ID 112
00026 
00027 typedef wiselib::OSMODEL Os;
00028 
00029 namespace wiselib
00030 {
00031 
00032    template<typename OsModel_P,
00033             typename Radio_P,
00034          typename Debug_P = typename OsModel_P::Debug> 
00035    class AutoCast_Message
00036    {
00037    
00038    public:
00039    
00040       typedef OsModel_P OsModel;
00041       typedef Radio_P Radio;
00042       typedef Debug_P Debug;
00043 
00044          typedef typename Radio::node_id_t node_id_t;
00045          typedef typename Radio::block_data_t block_data_t;
00046          typedef typename Radio::size_t size_t;
00047          typedef typename Radio::message_id_t message_id_t;
00048       
00049       AutoCast_Message(uint8_t nrOfbyteOfHash);
00050       ~AutoCast_Message();
00051 
00052       // return the size of the payload from buffer exclusive the data position
00053       inline size_t payload_size()
00054          { 
00055          
00056          size_t numDU = getNrOfDataUnits();
00057          size_t size =0;
00058          // nr of hash values * nr of byte for each hash value
00059          size_t num = (getNrOfStaleHashes() * getTypeOfHash()) + (getNrOfDataHashes() * getTypeOfHash()); // (number of stale hashes * type hash) is added
00060 
00061          block_data_t* ptr_begin = getDataUnit(0) - sizeof(size_t);
00062          block_data_t* ptr_end = getDataUnit(numDU) - sizeof(size_t);
00063 
00064          // distance between the pointer of the first payload and pointer of last payload
00065          size = ptr_end - ptr_begin ;
00066          
00067          // plus the nr of byte for all hash values 
00068          return size + num ; // sizeof(size_t) is deleted ? 
00069       }
00070 
00071       inline block_data_t* getDataUnits()
00072         { 
00073          return buffer + PAYLOAD_POS + sizeof(size_t); 
00074       } 
00075      
00076         inline void setDataUnits( size_t len, block_data_t *buf )
00077         {
00078             write<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS, len);
00079             memcpy( buffer + PAYLOAD_POS + sizeof(size_t), buf, len);
00080         }
00081       
00082       inline uint8_t getMessageId(){
00083          
00084          return read<OsModel, block_data_t, uint8_t>( buffer + MESSAGE_ID );
00085       }
00086 
00087       inline void setMessageId(uint8_t msgId){
00088          
00089          write<OsModel, block_data_t, uint8_t>( buffer + MESSAGE_ID, msgId );
00090       }
00091 
00092       inline uint8_t getNrOfStaleHashes(){
00093          
00094          return read<OsModel, block_data_t, uint8_t>( buffer + NR_OF_STALE_DATA_HASHES );
00095       }
00096 
00097       inline void setNrOfStaleHashes(uint8_t nrOfStaleHashes){
00098          
00099          write<OsModel, block_data_t, uint8_t>( buffer + NR_OF_STALE_DATA_HASHES, nrOfStaleHashes );
00100       }
00101       
00102 
00103       inline uint8_t getNrOfDataHashes(){
00104          
00105          return read<OsModel, block_data_t, uint8_t>( buffer + NR_OF_DATA_HASHES );
00106       }
00107 
00108       inline void setNrOfDataHashes(uint8_t nrOfDataHashes){
00109          
00110          write<OsModel, block_data_t, uint8_t>( buffer + NR_OF_DATA_HASHES, nrOfDataHashes );
00111       }
00112 
00113       inline uint8_t getNrOfDataUnits(){
00114          
00115          return read<OsModel, block_data_t, uint8_t>( buffer + NR_OF_DATA_UNITS );
00116       }
00117 
00118       inline void setNrOfDataUnits(uint8_t nrOfDataUnits){
00119          
00120          write<OsModel, block_data_t, uint8_t>( buffer + NR_OF_DATA_UNITS, nrOfDataUnits );
00121       }
00122       
00123       inline uint8_t getMsgId(){
00124          
00125          return read<OsModel, block_data_t, uint8_t>( buffer + NEXTT_BEACON_MSG_MS );
00126       }
00127 
00128       
00129       inline void setMsgId(uint8_t nextBeaconMsg){
00130          
00131          write<OsModel, block_data_t, uint8_t>( buffer + NEXTT_BEACON_MSG_MS, nextBeaconMsg);
00132       }
00133 
00134       // read the number of byte of hash type in the 4. position from buffer
00135       inline uint8_t getTypeOfHash(){
00136          
00137          return read<OsModel, block_data_t, uint8_t>( buffer + TYPE_OF_HASH_POS );
00138       }
00139       
00140       // write the number of byte of hash type in the 4. position in the buffer
00141       inline void setTypeOfHash(uint8_t nrOfbyte){
00142          
00143          write<OsModel, block_data_t, uint8_t>( buffer + TYPE_OF_HASH_POS, nrOfbyte );
00144       }
00145 
00146       inline size_t buffer_size()
00147          { 
00148          return PAYLOAD_POS + payload_size();
00149       }
00150 
00151       inline size_t addDataUnit(size_t len, block_data_t *dataUnit){
00152 
00153          // check the size of the data unit is smaller then the max message size 
00154          if( len > MAX_MESSAGE_SIZE){
00155          
00156             len = MAX_MESSAGE_SIZE;
00157          }
00158          size_t numDU = getNrOfDataUnits();
00159          // the pointer of the last empty cell in the buffer to save tha next data hash
00160          block_data_t* ptr = getDataUnit(numDU) - sizeof(size_t);
00161 
00162          //first save the size of the data unit with the pointer ptr
00163          write<OsModel, block_data_t, uint8_t>(ptr, len );
00164          ptr += sizeof(len);
00165          // then save the data unit after the size position
00166          memcpy(ptr, dataUnit, len);
00167          numDU++;
00168          setNrOfDataUnits(numDU);
00169 
00170          return len;       
00171 
00172       }
00173 
00174       // return the 
00175       inline block_data_t* getDataUnit(size_t pos){
00176          
00177          uint8_t ppsize =0;
00178          uint8_t num = (getNrOfDataHashes() * getTypeOfHash()) + (getNrOfStaleHashes() * getTypeOfHash());
00179 
00180          while(pos > 0){
00181 
00182             ppsize += read<OsModel, block_data_t, uint8_t>(buffer +  PAYLOAD_POS + ppsize + num) + sizeof(size_t);
00183 
00184             pos--;
00185          }
00186 
00187          return buffer + PAYLOAD_POS +  ppsize + sizeof(size_t) + num;
00188       }
00189 
00190       // this function adds one hash value in buffer withe the bayte size, that defines above
00191       inline size_t addDataHash(block_data_t *dataHash){
00192 
00193          // if already data unit saved (nr of data unit larger then 0) then rteturn 0
00194          // we can not saved hash values after data units
00195          if(getNrOfDataUnits() > 0){
00196             
00197             return 0;
00198          }
00199 
00200          size_t numDH = getNrOfDataHashes();
00201          size_t nrOfBayte = getTypeOfHash();
00202 
00203          // the pointer of the last empty cell in the buffer to save tha next hash value
00204          block_data_t* ptr = getDataHash(numDH);
00205 
00206          memcpy(ptr, dataHash, nrOfBayte);
00207          numDH++;
00208          setNrOfDataHashes(numDH);
00209 
00210          return numDH * nrOfBayte;        
00211 
00212       }
00213 
00214       // give the pointer of the desird position (id of Hash Value)
00215       inline block_data_t* getDataHash(size_t pos){
00216 
00217          if(getNrOfDataUnits() > 0 && getNrOfDataHashes() == 0){
00218 
00219             return 0;
00220          }
00221 
00222          uint8_t ppsize = (getTypeOfHash() * getNrOfStaleHashes()) + getTypeOfHash() * pos  ;
00223          
00224          // return the pointer of PAYLOAD_POS + (number of the byte size * pos)
00225          return buffer + PAYLOAD_POS +  ppsize;
00226       }
00227 
00228       // this function adds one hash value in buffer withe the bayte size, that defines above
00229       inline size_t addStaleDataHash(block_data_t *StaleDataHash){
00230 
00231          // if already data unit saved (nr of data unit larger then 0) then rteturn 0
00232          // we can not saved hash values after data units
00233          if(getNrOfDataUnits() > 0 || getNrOfDataHashes() > 0){
00234             
00235             return 0;
00236          }
00237 
00238          size_t numSDH = getNrOfStaleHashes();
00239          size_t nrOfBayte = getTypeOfHash();
00240 
00241          // the pointer of the last empty cell in the buffer to save tha next hash value
00242          block_data_t* ptr = getStaleDataHash(numSDH);
00243 
00244          memcpy(ptr, StaleDataHash, nrOfBayte);
00245          numSDH++;
00246          setNrOfStaleHashes(numSDH);
00247 
00248          return numSDH * nrOfBayte;       
00249 
00250       }
00251 
00252       // give the pointer of the desird position (id of Hash Value)
00253       inline block_data_t* getStaleDataHash(size_t pos){
00254 
00255          if( (getNrOfDataUnits() > 0 && getNrOfStaleHashes() == 0) || 
00256                getNrOfDataHashes() > 0 && getNrOfStaleHashes() == 0){
00257 
00258             return 0;
00259          }
00260 
00261          uint8_t ppsize = getTypeOfHash() * pos ;
00262          
00263          // return the pointer of PAYLOAD_POS + (number of the byte size * pos)
00264          return buffer + PAYLOAD_POS +  ppsize;
00265       }
00266    
00267    private:
00268 
00269       // the differant position of elements in the buffer
00270       enum data_positions{
00271 
00272          MESSAGE_ID = 0,
00273          NEXTT_BEACON_MSG_MS = MESSAGE_ID + 1,
00274          NR_OF_STALE_DATA_HASHES = NEXTT_BEACON_MSG_MS + 1,
00275          NR_OF_DATA_HASHES = NR_OF_STALE_DATA_HASHES + 1,
00276          NR_OF_DATA_UNITS = NR_OF_DATA_HASHES + 1,
00277          TYPE_OF_HASH_POS = NR_OF_DATA_UNITS + 1,
00278          PAYLOAD_POS = TYPE_OF_HASH_POS + 1
00279 
00280       };
00281 
00282       block_data_t buffer[MAX_MESSAGE_SIZE];
00283 
00284    };
00285    
00286    /* constructor
00287       
00288 
00289    */
00290    template<typename OsModel_P,
00291             typename Radio_P,
00292           typename Debug_P>
00293    AutoCast_Message<OsModel_P, Radio_P, Debug_P>::
00294 	AutoCast_Message(uint8_t nrOfbyteOfHash)
00295    {
00296       setMessageId(MSG_ID);
00297       setMsgId(0);
00298       setNrOfDataUnits(0);
00299       setNrOfDataHashes(0);
00300       setNrOfStaleHashes(0);
00301       // number of byte of hash´value, wil be one time in the 4. position in buffer
00302       setTypeOfHash(nrOfbyteOfHash);
00303      
00304    }
00305 
00306    template<typename OsModel_P,
00307             typename Radio_P,
00308           typename Debug_P>
00309    AutoCast_Message<OsModel_P, Radio_P,Debug_P>::
00310 	~AutoCast_Message()
00311    {
00312      
00313    }
00314    
00315    
00316 }
00317 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines