Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef __PLTT_AGENT_H__ 00020 #define __PLTT_AGENT_H__ 00021 #include "PLTT_config.h" 00022 namespace wiselib 00023 { 00024 template< typename Os_P, 00025 typename Radio_P, 00026 typename AgentID_P, 00027 typename Node_P, 00028 typename IntensitiyNumber_P, 00029 typename Clock_P, 00030 typename Debug_P> 00031 class PLTT_AgentType 00032 { 00033 public: 00034 typedef Os_P Os; 00035 typedef Radio_P Radio; 00036 typedef AgentID_P AgentID; 00037 typedef Node_P Node; 00038 typedef IntensitiyNumber_P IntensityNumber; 00039 typedef Debug_P Debug; 00040 typedef typename Radio::block_data_t block_data_t; 00041 typedef typename Radio::size_t size_t; 00042 typedef Clock_P Clock; 00043 typedef typename Clock::time_t time_t; 00044 typedef typename Radio::node_id_t node_id_t; 00045 typedef PLTT_AgentType<Os, Radio, AgentID, Node, IntensityNumber, Clock, Debug> self_type; 00046 PLTT_AgentType() 00047 {} 00048 PLTT_AgentType( const AgentID& _trid, const Node& _tar, const Node& _tra, const IntensityNumber& _max_inten, AgentID& _raid = 0 ) 00049 { 00050 set_all( _trid, _tar, _tra, _max_inten ); 00051 } 00052 PLTT_AgentType( block_data_t* buff, size_t offset = 0 ) 00053 { 00054 get_from_buffer( buff, offset ); 00055 } 00056 block_data_t* set_buffer_from( block_data_t* buff, size_t offset = 0 ) 00057 { 00058 00059 uint8_t AGENT_ID_POS = 0; 00060 uint8_t TARGET_POS = AGENT_ID_POS + sizeof( AgentID ); 00061 uint8_t TRACKER_POS = TARGET_POS + target.get_buffer_size(); 00062 uint8_t MAX_INTEN_POS = TRACKER_POS + tracker.get_buffer_size(); 00063 #ifdef PLTT_AGENT_TRACKING_METRICS 00064 uint8_t TRACK_START_POS = MAX_INTEN_POS + sizeof( IntensityNumber ); 00065 uint8_t TRACK_END_POS = TRACK_START_POS + sizeof( time_t ); 00066 uint8_t APROX_DETECTION_POS = TRACK_END_POS + sizeof( time_t ); 00067 #ifdef OPT_RELIABLE_TRACKING 00068 uint8_t RELIABLE_AGENT_ID_POS = APROX_DETECTION_POS + sizeof( time_t ); 00069 #endif 00070 #else 00071 #ifdef OPT_RELIABLE_TRACKING 00072 uint8_t RELIABLE_AGENT_ID_POS = MAX_INTEN_POS + sizeof( IntensityNumber ); 00073 #endif 00074 #endif 00075 write<Os, block_data_t, AgentID> (buff + AGENT_ID_POS + offset, agent_id); 00076 target.set_buffer_from( buff, TARGET_POS + offset ); 00077 tracker.set_buffer_from( buff, TRACKER_POS + offset ); 00078 write<Os, block_data_t, IntensityNumber> (buff + MAX_INTEN_POS + offset, max_intensity); 00079 #ifdef PLTT_AGENT_TRACKING_METRICS 00080 write<Os, block_data_t, time_t> (buff + TRACK_START_POS, track_start ); 00081 write<Os, block_data_t, time_t> (buff + TRACK_END_POS, track_end ); 00082 write<Os, block_data_t, time_t> (buff + APROX_DETECTION_POS, aprox_detection ); 00083 #endif 00084 #ifdef OPT_RELIABLE_TRACKING 00085 write<Os, block_data_t, AgentID> (buff + RELIABLE_AGENT_ID_POS, reliable_agent_id ); 00086 #endif 00087 return buff; 00088 } 00089 void get_from_buffer( block_data_t* buff, size_t offset = 0 ) 00090 { 00091 uint8_t AGENT_ID_POS = 0; 00092 uint8_t TARGET_POS = AGENT_ID_POS + sizeof( AgentID ); 00093 uint8_t TRACKER_POS = TARGET_POS + target.get_buffer_size(); 00094 uint8_t MAX_INTEN_POS = TRACKER_POS + tracker.get_buffer_size(); 00095 #ifdef PLTT_AGENT_TRACKING_METRICS 00096 uint8_t TRACK_START_POS = MAX_INTEN_POS + sizeof( IntensityNumber ); 00097 uint8_t TRACK_END_POS = TRACK_START_POS + sizeof( time_t ); 00098 uint8_t APROX_DETECTION_POS = TRACK_END_POS + sizeof( time_t ); 00099 #ifdef OPT_RELIABLE_TRACKING 00100 uint8_t RELIABLE_AGENT_ID_POS = APROX_DETECTION_POS + sizeof( time_t ); 00101 #endif 00102 #else 00103 #ifdef OPT_RELIABLE_TRACKING 00104 uint8_t RELIABLE_AGENT_ID_POS = MAX_INTEN_POS + sizeof( IntensityNumber ); 00105 #endif 00106 #endif 00107 agent_id = read<Os, block_data_t, AgentID> (buff + AGENT_ID_POS + offset); 00108 target.get_from_buffer( buff, TARGET_POS + offset ); 00109 tracker.get_from_buffer( buff, TRACKER_POS + offset ); 00110 max_intensity = read<Os, block_data_t, IntensityNumber> (buff + MAX_INTEN_POS + offset); 00111 #ifdef PLTT_AGENT_TRACKING_METRICS 00112 track_start = read<Os, block_data_t, time_t> (buff + TRACK_START_POS ); 00113 track_end = read<Os, block_data_t, time_t> (buff + TRACK_END_POS ); 00114 aprox_detection = read<Os, block_data_t, time_t> (buff + APROX_DETECTION_POS ); 00115 #endif 00116 #ifdef OPT_RELIABLE_TRACKING 00117 reliable_agent_id = read<Os, block_data_t, AgentID> (buff + RELIABLE_AGENT_ID_POS); 00118 #endif 00119 } 00120 size_t get_buffer_size() 00121 { 00122 uint8_t AGENT_ID_POS = 0; 00123 uint8_t TARGET_POS = AGENT_ID_POS + sizeof( AgentID ); 00124 uint8_t TRACKER_POS = TARGET_POS + target.get_buffer_size(); 00125 uint8_t MAX_INTEN_POS = TRACKER_POS + tracker.get_buffer_size(); 00126 #ifdef PLTT_AGENT_TRACKING_METRICS 00127 uint8_t TRACK_START_POS = MAX_INTEN_POS + sizeof( IntensityNumber ); 00128 uint8_t TRACK_END_POS = TRACK_START_POS + sizeof( time_t ); 00129 uint8_t APROX_DETECTION_POS = TRACK_END_POS + sizeof( time_t ); 00130 #ifdef OPT_RELIABLE_TRACKING 00131 uint8_t RELIABLE_AGENT_ID_POS = APROX_DETECTION_POS + sizeof( time_t ); 00132 return RELIABLE_AGENT_ID_POS + sizeof( AgentID ); 00133 #endif 00134 return APROX_DETECTION_POS + sizeof( time_t ); 00135 #else 00136 #ifdef OPT_RELIABLE_TRACKING 00137 uint8_t RELIABLE_AGENT_ID_POS = MAX_INTEN_POS + sizeof( IntensityNumber ); 00138 return RELIABLE_AGENT_ID_POS + sizeof( AgentID ); 00139 #endif 00140 return MAX_INTEN_POS + sizeof( max_intensity ); 00141 #endif 00142 } 00143 inline self_type& operator=( const self_type& _a) 00144 { 00145 agent_id = _a.agent_id; 00146 target = _a.target; 00147 tracker = _a.tracker; 00148 max_intensity = _a.max_intensity; 00149 #ifdef PLTT_AGENT_TRACKING_METRICS 00150 aprox_detection = _a.aprox_detection; 00151 track_start = _a.track_start; 00152 track_end = _a.track_end; 00153 #endif 00154 #ifdef OPT_RELIABLE_TRACKING 00155 reliable_agent_id = _a.reliable_agent_id; 00156 #endif 00157 return *this; 00158 } 00159 inline AgentID get_agent_id() 00160 { 00161 return agent_id; 00162 } 00163 inline void set_agent_id( const AgentID& _trid ) 00164 { 00165 agent_id = _trid; 00166 } 00167 #ifdef OPT_RELIABLE_TRACKING 00168 inline AgentID get_reliable_agent_id() 00169 { 00170 return reliable_agent_id; 00171 } 00172 inline void set_reliable_agent_id( const AgentID& _trid ) 00173 { 00174 reliable_agent_id = _trid; 00175 } 00176 inline void update_reliable_agent_id() 00177 { 00178 ++reliable_agent_id; 00179 } 00180 #endif 00181 inline Node get_target() 00182 { 00183 return target; 00184 } 00185 inline void set_target( const Node&_n ) 00186 { 00187 target = _n; 00188 } 00189 inline Node get_tracker() 00190 { 00191 return tracker; 00192 } 00193 inline void set_tracker( const Node& _n ) 00194 { 00195 tracker = _n; 00196 } 00197 inline void set_max_intensity( const IntensityNumber& _mi ) 00198 { 00199 max_intensity = _mi; 00200 } 00201 inline IntensityNumber get_max_intensity() 00202 { 00203 return max_intensity; 00204 } 00205 inline void set_all( const AgentID& _trid, const Node& _tar, const Node& _tra, const IntensityNumber& _max_inten, const AgentID _raid ) 00206 { 00207 agent_id = _trid; 00208 target = _tar; 00209 tracker = _tra; 00210 max_intensity = _max_inten; 00211 reliable_agent_id = _raid; 00212 } 00213 void print( Debug& debug, Clock& clock ) 00214 { 00215 debug.debug( "agent_id (size %i), %x\n", sizeof( agent_id ), agent_id ); 00216 target.print( debug ); 00217 tracker.print( debug ); 00218 debug.debug( "max_intensity (size %i), %i\n", sizeof( max_intensity ), max_intensity ); 00219 #ifdef PLTT_AGENT_TRACKING_METRICS 00220 debug.debug( "track start secs (size %i) :, %i\n" , sizeof( uint32_t), clock.seconds( track_start ) ); 00221 debug.debug( "track start ms (size %i) : %i\n", sizeof( uint16_t), clock.milliseconds( track_start ) ); 00222 debug.debug( "track end secs (size %i) : %i\n", sizeof( uint32_t ), clock.seconds( track_end ) ); 00223 debug.debug( "track end ms (size %i) : %i\n", sizeof( uint16_t ), clock.milliseconds( track_end ) ); 00224 debug.debug( "aprox detection secs (size %i) : %i\n", sizeof( uint32_t ), clock.seconds( aprox_detection ) ); 00225 debug.debug( "aprox detection ms (size %i) %i\n", sizeof( uint16_t ), clock.milliseconds( aprox_detection ) ); 00226 #endif 00227 #ifdef OPT_RELIABLE_TRACKING 00228 debug.debug( "reliable_agent_id (size %i) %i\n", sizeof( reliable_agent_id ), reliable_agent_id ); 00229 #endif 00230 } 00231 #ifdef PLTT_AGENT_TRACKING_METRICS 00232 time_t get_track_start( Clock& clock ) 00233 { 00234 #ifdef ISENSE_APP 00235 return time_t( clock.seconds( track_start ), clock.milliseconds( track_start ) ); 00236 #endif 00237 #ifdef SHAWN_APP 00238 return 0; 00239 #endif 00240 } 00241 time_t get_track_end( Clock& clock ) 00242 { 00243 #ifdef ISENSE_APP 00244 return time_t( clock.seconds( track_end ), clock.milliseconds ( track_end ) ); 00245 #endif 00246 #ifdef SHAWN_APP 00247 return 0; 00248 #endif 00249 } 00250 time_t get_aprox_detection( Clock& clock ) 00251 { 00252 #ifdef ISENSE_APP 00253 return time_t( clock.seconds( aprox_detection), clock.milliseconds( aprox_detection ) ); 00254 #endif 00255 #ifdef SHAWN_APP 00256 return 0; 00257 #endif 00258 } 00259 void set_track_start( time_t t ) 00260 { 00261 track_start = t; 00262 } 00263 void set_track_end( time_t t ) 00264 { 00265 track_end = t; 00266 } 00267 void set_aprox_detection( time_t t ) 00268 { 00269 aprox_detection = t; 00270 } 00271 time_t detection_duration( Clock& clock ) 00272 { 00273 uint32_t sec_dd = clock.seconds( aprox_detection ) - clock.seconds( track_start ); 00274 uint16_t ms_dd; 00275 if ( clock.milliseconds( aprox_detection ) < clock.milliseconds( track_start ) ) 00276 { 00277 sec_dd = sec_dd - 1; 00278 ms_dd = ( 1000 + clock.milliseconds( aprox_detection ) ) - clock.milliseconds( track_start ); 00279 } 00280 else 00281 { 00282 ms_dd = clock.milliseconds( aprox_detection ) - clock.milliseconds( track_start ); 00283 } 00284 #ifdef ISENSE_APP 00285 return time_t( sec_dd, ms_dd ); 00286 #endif 00287 #ifdef SHAWN_APP 00288 return 0; 00289 #endif 00290 } 00291 time_t track_duration( Clock& clock ) 00292 { 00293 uint32_t sec_td = clock.seconds( track_end ) - clock.seconds( track_start ); 00294 uint16_t ms_td; 00295 if ( clock.milliseconds( track_end ) < clock.milliseconds( track_start ) ) 00296 { 00297 sec_td = sec_td - 1; 00298 ms_td = ( 1000 + clock.milliseconds( track_end ) ) - clock.milliseconds( track_start ); 00299 } 00300 else 00301 { 00302 ms_td = clock.milliseconds( track_end ) - clock.milliseconds( track_start ); 00303 } 00304 #ifdef ISENSE_APP 00305 return time_t( sec_td, ms_td ); 00306 #endif 00307 #ifdef SHAWN_APP 00308 return 0; 00309 #endif 00310 } 00311 #endif 00312 private: 00313 AgentID agent_id; 00314 Node target; 00315 Node tracker; 00316 IntensityNumber max_intensity; 00317 #ifdef PLTT_AGENT_TRACKING_METRICS 00318 time_t aprox_detection; 00319 time_t track_start; 00320 time_t track_end; 00321 #endif 00322 #ifdef OPT_RELIABLE_TRACKING 00323 AgentID reliable_agent_id; 00324 #endif 00325 }; 00326 } 00327 #endif 00328 00329