Wiselib
wiselib.testing/radio/fragmenting/testvolume.h
Go to the documentation of this file.
00001 /* 
00002  * File:   testvolume.h
00003  * Author: amaxilatis
00004  *
00005  * Created on August 2, 2010, 2:32 PM
00006  */
00007 
00008 #ifndef _TESTVOLUME_H
00009 #define  _TESTVOLUME_H
00010 
00011 #include "util/delegates/delegate.hpp"
00012 #include <iostream>
00013 #include <string.h>
00014 //#define TIME_DELAY 2000
00015 //#define COUNTER_MAX 1000
00016 
00017 namespace wiselib {
00018 
00019     template<typename OsModel_P,
00020     typename Radio_P,
00021     typename Timer_P,
00022     typename Debug_P>
00023     class testvolume {
00024     public:
00025 
00026         typedef int cluster_id_t;
00027         typedef int cluster_level_t; //quite useless within current scheme, supported for compatibility issues
00028         typedef OsModel_P OsModel;
00029         typedef Radio_P Radio;
00030         
00031         typedef Debug_P Debug;
00032         typedef Timer_P Timer;
00033 
00034         typedef testvolume<OsModel_P, Radio_P, Timer_P, Debug_P> self_t;
00035 
00036         typedef typename Radio::node_id_t node_id_t;
00037         typedef typename Radio::size_t size_t;
00038         typedef typename Radio::block_data_t block_data_t;
00039 
00040 
00041 
00042  
00043 
00044         void enable(void);
00045         void disable(void);
00046 
00047 
00048    
00049         void receive(node_id_t receiver, size_t len, block_data_t *data);
00050 
00051       
00052         void send_messages(void * node) {
00053             int BIG_MESSAGE = 30000;
00054 
00055             //if (counter_*150>BIG_MESSAGE) return;
00056             if (counter_==10) return;
00057 
00058             int items = counter_*100-1;
00059             uint8_t payload[2*items+2];
00060             payload[0]=items%256;
00061             payload[1]=items/256;
00062             for (int i=0;i<items;i++) {
00063                 payload[2+i*2] = i%256;
00064                 payload[2+i*2+1] = i/256;
00065             }
00066 
00067              debug().debug("Application::values [");
00068              for (int i=0;i<items;i++){
00069              //    debug().debug(" %d",payload[2+i*2]+payload[2+i*2+1]*256);
00070 
00071              }
00072              debug().debug("]\n");
00073 
00074 
00075             debug().debug("Application::send node= %d dest= %d  message_size=%d\n",radio().id(),node,2*items);
00076 
00077 
00078 
00079             radio().send(/*Radio_t::BROADCAST_ADDRESS*/ (node_id_t)node ,2*(items+1)*sizeof(uint8_t),payload);
00080 
00081             counter_++;
00082             timer().template set_timer<self_t, &self_t::send_messages > (
00083                     3000, this, (void*) node);
00084 
00085         }
00086 
00089 
00090         testvolume()
00091 
00092         {
00093         }
00094 
00095         ~testvolume() {
00096         }
00098 
00099 
00100 
00101         void init (Radio& radio , Timer& timer , Debug& debug){
00102             radio_ = &radio;
00103             timer_ = &timer;
00104             debug_ = &debug;
00105         };
00106     private:
00107         int callback_id_;
00108         int counter_;
00109 
00110 
00111 
00112         Radio * radio_;
00113         Timer * timer_;
00114         Debug * debug_;
00115 
00116 
00117         Radio& radio() {
00118             return *radio_;
00119         }
00120 
00121         Timer& timer() {
00122             return *timer_;
00123         }
00124 
00125         Debug& debug() {
00126             return *debug_;
00127         }
00128         
00129     };
00130 
00131     template<typename OsModel_P,
00132     typename Radio_P,
00133     typename Timer_P,
00134     typename Debug_P>
00135     void
00136     testvolume<OsModel_P, Radio_P, Timer_P, Debug_P>::
00137     enable(void) {
00138 
00139         
00140         radio().enable_radio();
00141         debug().debug( "Booting up");
00142         debug().debug( ".");
00143 
00144         callback_id_ = radio().template reg_recv_callback<self_t,
00145                 &self_t::receive > (this);
00146         debug().debug( ".");
00147         counter_=1;
00148         debug().debug( ".");
00149 
00150         debug().debug( "OK!\n");
00151 
00152 
00153         if (radio().id() == 9) {
00154 
00155             send_messages((void*)2);
00156             //send_messages((void*)1);
00157             
00158 
00159 
00160 
00161         }
00162 
00163     }
00164 
00165     template<typename OsModel_P,
00166     typename Radio_P,
00167     typename Timer_P,
00168     typename Debug_P>
00169     void
00170     testvolume<OsModel_P, Radio_P, Timer_P, Debug_P>::
00171     disable(void) {
00172         
00173         
00174     }
00175 
00176     template<typename OsModel_P,
00177     typename Radio_P,
00178     typename Timer_P,
00179     typename Debug_P>
00180     void
00181     testvolume<OsModel_P, Radio_P, Timer_P, Debug_P>::
00182     receive(node_id_t from, size_t len, block_data_t* data) {
00183 
00184         if (from==radio().id())
00185             return;
00186         else{
00187              uint16_t count = data[0]+data[1]*256;
00188              debug().debug("Application::receive count= %d\n",count);
00189 
00190              uint8_t * values = new uint8_t[count*2];
00191 
00192              memcpy(values,data+2,count*2);
00193              uint16_t prev=values[0]+values[1]*256,current;
00194              bool ok=true;
00195 
00196              for (int i=1;i<count;i++){
00197                  
00198                  current = values[i*2]+values[i*2+1]*256;
00199                  if(prev+1!=current){
00200                     debug().debug("Application::receive error\n");
00201                     ok=false;
00202                  }
00203                  prev=current;
00204 
00205              }
00206              if (ok){
00207                 debug().debug("Application::receive status= ok count= %d\n",count);
00208              }
00209 
00210 
00211         }
00212 
00213     }
00214 
00215 
00216 }
00217 
00218 #endif   /* _TESTVOLUME_H */
00219 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines