39 #include <arpa/inet.h>
41 #include <boost/chrono/chrono_io.hpp>
42 #include <boost/thread/thread.hpp>
55 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
60 mConfigurationSpace(),
64 mLastPacketHeader ( 0x200000f0 ),
65 mTrafficHistory ( 16, 0x00 ),
66 mReceivedControlPacketHeaderHistory ( 4 , 0x00000000 ),
67 mSentControlPacketHeaderHistory ( 4 , 0x00000000 ),
68 mBigEndianHack ( aBigEndianHack )
70 for (
size_t i = 0; i < 10; i++)
74 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
80 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
84 if ( IPbus_major == 2 )
86 bool is_status_request = ( *mReceive.begin() == 0xF1000020 );
87 bool is_resend_request = ( ( *mReceive.begin() & 0xFF0000FF ) == 0xF2000020 );
89 if ( mBigEndianHack || is_status_request || is_resend_request )
91 for ( std::vector<uint32_t>::iterator lIt ( mReceive.begin() ) ; lIt != mReceive.begin() + ( aByteCount>>2 ) ; ++lIt )
93 *lIt = ntohl ( *lIt );
98 std::vector<uint32_t>::const_iterator lBegin, lEnd;
104 log (
Debug() ,
"\n=============================================== RECEIVED ===============================================" );
106 lBegin = mReceive.begin();
107 lEnd = mReceive.begin() + ( aByteCount>>2 );
108 lHostToTargetDebugger.
analyze ( lBegin , lEnd );
113 lBegin = mReceive.begin();
114 lEnd = mReceive.begin() + ( aByteCount>>2 );
116 if ( ! base_type::analyze ( lBegin , lEnd ) )
118 log (
Error() ,
"Found a bad header" );
122 if ( ( base_type::mPacketType == 0 ) && ( mReply.size() != 0 ) )
124 mReplyHistory.push_back ( std::make_pair ( base_type::mPacketCounter , mReply ) );
125 mReplyHistory.pop_front();
128 if ( mReplyDelay > boost::chrono::microseconds(0) )
130 log (
Info() ,
"Sleeping for " , mReplyDelay );
131 boost::this_thread::sleep_for( mReplyDelay );
132 mReplyDelay = boost::chrono::microseconds(0);
133 log (
Info() ,
"Now replying " );
140 log (
Debug() ,
"\n=============================================== SENDING ===============================================" );
142 lBegin = mReply.begin();
144 lTargetToHostDebugger.
analyze ( lBegin , lEnd );
150 if ( IPbus_major == 2 )
152 if ( mBigEndianHack || base_type::mPacketType == 1 )
154 for ( std::vector<uint32_t>::iterator lIt ( mReply.begin() ) ; lIt != mReply.end() ; ++lIt )
156 *lIt = htonl ( *lIt );
163 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
166 if( ! mMemory.size() )
172 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
175 if( ! mMemory.size() )
181 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
184 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
185 mReceivedControlPacketHeaderHistory.pop_front();
187 mReply.push_back ( lExpected );
188 mSentControlPacketHeaderHistory.push_back ( lExpected );
189 mSentControlPacketHeaderHistory.pop_front();
193 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
196 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
197 mReceivedControlPacketHeaderHistory.pop_front();
198 uint32_t lAddress ( aAddress );
200 mReply.push_back ( lExpected );
201 mSentControlPacketHeaderHistory.push_back ( lExpected );
202 mSentControlPacketHeaderHistory.pop_front();
204 for ( ; base_type::mWordCounter!=0 ; --base_type::mWordCounter )
206 mReply.push_back ( GetEndpoint( lAddress ) );
211 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
214 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
215 mReceivedControlPacketHeaderHistory.pop_front();
216 uint32_t lAddress ( aAddress );
218 mReply.push_back ( lExpected );
219 mSentControlPacketHeaderHistory.push_back ( lExpected );
220 mSentControlPacketHeaderHistory.pop_front();
222 for ( ; base_type::mWordCounter!=0 ; --base_type::mWordCounter )
224 mReply.push_back ( GetEndpoint( lAddress++ ) );
229 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
232 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
233 mReceivedControlPacketHeaderHistory.pop_front();
234 uint32_t lAddress ( aAddress );
236 mReply.push_back ( lExpected );
237 mSentControlPacketHeaderHistory.push_back ( lExpected );
238 mSentControlPacketHeaderHistory.pop_front();
240 for ( ; base_type::mWordCounter!=0 ; --base_type::mWordCounter )
242 mReply.push_back ( mConfigurationSpace.at( lAddress++ ) );
247 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
250 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
251 mReceivedControlPacketHeaderHistory.pop_front();
252 uint32_t lAddress ( aAddress );
254 while ( aIt != aEnd )
256 SetEndpoint ( lAddress , *aIt++ );
261 if ( IPbus_major == 1 )
270 mReply.push_back ( lExpected );
271 mSentControlPacketHeaderHistory.push_back ( lExpected );
272 mSentControlPacketHeaderHistory.pop_front();
276 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
279 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
280 mReceivedControlPacketHeaderHistory.pop_front();
281 uint32_t lAddress ( aAddress );
283 while ( aIt != aEnd )
285 SetEndpoint ( lAddress++ , *aIt++ );
290 if ( IPbus_major == 1 )
299 mReply.push_back ( lExpected );
300 mSentControlPacketHeaderHistory.push_back ( lExpected );
301 mSentControlPacketHeaderHistory.pop_front();
305 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
308 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
309 mReceivedControlPacketHeaderHistory.pop_front();
310 uint32_t lAddress ( aAddress );
312 mReply.push_back ( lExpected );
313 mSentControlPacketHeaderHistory.push_back ( lExpected );
314 mSentControlPacketHeaderHistory.pop_front();
316 if ( IPbus_major == 1 )
319 uint32_t lValue( GetEndpoint( lAddress ) );
321 SetEndpoint( lAddress , lValue );
322 mReply.push_back ( lValue );
327 uint32_t lValue( GetEndpoint( lAddress ) );
328 mReply.push_back ( lValue );
330 SetEndpoint( lAddress , lValue );
335 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
338 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
339 mReceivedControlPacketHeaderHistory.pop_front();
340 uint32_t lAddress ( aAddress );
342 mReply.push_back ( lExpected );
343 mSentControlPacketHeaderHistory.push_back ( lExpected );
344 mSentControlPacketHeaderHistory.pop_front();
346 if ( IPbus_major == 1 )
349 uint32_t lValue( GetEndpoint( lAddress ) );
352 SetEndpoint( lAddress , lValue );
353 mReply.push_back ( lValue );
358 uint32_t lValue( GetEndpoint( lAddress ) );
359 mReply.push_back ( lValue );
362 SetEndpoint( lAddress , lValue );
367 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
371 mReceivedControlPacketHeaderHistory.push_back ( base_type::mPacketHeader );
372 mReceivedControlPacketHeaderHistory.pop_front();
374 mReply.push_back ( lExpected );
375 mSentControlPacketHeaderHistory.push_back ( lExpected );
376 mSentControlPacketHeaderHistory.pop_front();
380 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
383 if ( base_type::mPacketCounter != 0 )
385 uint16_t lTemp ( ( ( mLastPacketHeader>>8 ) &0x0000FFFF ) + 1 );
392 if ( base_type::mPacketCounter != lTemp )
394 mTrafficHistory.push_back ( 5 );
395 mTrafficHistory.pop_front();
396 log(
Notice(),
"Dummy hardware received control packet with ID ",
Integer(base_type::mPacketCounter),
", but expected ID ",
Integer(lTemp));
400 mLastPacketHeader = base_type::mPacketHeader;
403 mReply.push_back ( base_type::mPacketHeader );
404 mTrafficHistory.push_back ( 2 );
405 mTrafficHistory.pop_front();
410 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
413 mReply.push_back ( base_type::mPacketHeader );
414 mReply.push_back (
BUFFER_SIZE *
sizeof ( uint32_t ) );
416 uint16_t lTemp ( ( ( mLastPacketHeader>>8 ) &0x0000FFFF ) + 1 );
423 mReply.push_back ( ( mLastPacketHeader & 0xFF0000FF ) | ( ( lTemp <<8 ) & 0x00FFFF00 ) );
424 std::deque< uint8_t >::const_iterator lIt ( mTrafficHistory.begin() );
426 for ( uint32_t i = 0; i != 4 ; ++i )
428 uint32_t lTemp ( 0x00000000 );
430 for ( uint32_t
j = 0;
j != 4 ; ++
j )
433 lTemp |= ( uint32_t ) ( *lIt );
437 mReply.push_back ( lTemp );;
440 for ( std::deque< uint32_t >::const_iterator i = mReceivedControlPacketHeaderHistory.begin(); i != mReceivedControlPacketHeaderHistory.end() ; ++i )
442 mReply.push_back ( *i );
445 for ( std::deque< uint32_t >::const_iterator i = mSentControlPacketHeaderHistory.begin(); i != mSentControlPacketHeaderHistory.end() ; ++i )
447 mReply.push_back ( *i );
450 mTrafficHistory.push_back ( 3 );
451 mTrafficHistory.pop_front();
455 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
458 std::deque< std::pair< uint32_t , std::vector< uint32_t > > >::reverse_iterator lIt = mReplyHistory.rbegin();
460 for ( ; lIt!=mReplyHistory.rend() ; ++lIt )
462 if ( lIt->first == base_type::mPacketCounter )
464 mReply = lIt->second;
469 mTrafficHistory.push_back ( 4 );
470 mTrafficHistory.pop_front();
474 template< u
int8_t IPbus_major , u
int8_t IPbus_minor >
477 mTrafficHistory.push_back ( 5 );
478 mTrafficHistory.pop_front();