1 #include "otsdaq-components/DAQHardware/OtsUDPHardware.h"
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/MessageFacility/MessageFacility.h"
11 OtsUDPHardware::OtsUDPHardware(std::string boardIPAddress,
12 unsigned int boardPort,
17 , OtsUDPBoard_(boardIPAddress, boardPort)
31 OtsUDPHardware::OtsUDPHardware(std::string hostIPAddress,
32 unsigned int hostPort,
33 std::string OtsUDPHardwareIPAddress,
34 unsigned int OtsUDPHardwarePort,
37 : Socket(hostIPAddress, hostPort)
39 , OtsUDPBoard_(OtsUDPHardwareIPAddress, OtsUDPHardwarePort)
44 __COUT__ <<
"Socket for hardware is initialized at IP:Port "
45 << OtsUDPBoard_.getIPAddress() <<
":" << OtsUDPBoard_.getPort() << __E__;
56 OtsUDPHardware::~OtsUDPHardware(
void) {}
59 void OtsUDPHardware::write(
const std::string& sendBuffer)
try
85 if(TransceiverSocket::send(OtsUDPBoard_, sendBuffer, verbose_) < 0)
87 __SS__ <<
"Write failed to hardware at IP:Port " << OtsUDPBoard_.getIPAddress()
88 <<
":" << OtsUDPBoard_.getPort() << __E__;
92 catch(std::runtime_error& e)
98 __SS__ <<
"Unrecognized exception caught!" << std::endl;
103 void OtsUDPHardware::write(
const std::vector<std::string>& sendBuffer)
105 for(
const auto& it : sendBuffer)
135 void OtsUDPHardware::writeAndAcknowledge(
const std::string& buffer,
136 int timeoutSeconds)
try
166 TransceiverSocket::send(OtsUDPBoard_, buffer, verbose_);
169 if(timeoutSeconds < 0)
172 if(TransceiverSocket::receive(acknowledgment_, timeoutSeconds) < 0)
174 __SS__ <<
"writeAndAcknowledge failed from hardware at IP:Port "
175 << OtsUDPBoard_.getIPAddress() <<
":" << OtsUDPBoard_.getPort()
176 <<
". Timeout period of " << timeoutSeconds
177 <<
" seconds reached without response." << std::endl;
178 __COUT_ERR__ <<
"\n" << ss.str() << std::endl;
199 if((acknowledgment_[0] >> 4))
201 __SS__ <<
"Error in OTS protocol encountered! " << std::setfill(
'0')
202 << std::setw(2) << std::hex << (((int16_t)acknowledgment_[0]) & 0xff)
203 <<
"-" << std::dec << __E__;
207 catch(std::runtime_error& e)
213 __SS__ <<
"Unrecognized exception caught!" << std::endl;
214 __COUT_ERR__ <<
"\n" << ss.str() << std::endl;
219 void OtsUDPHardware::writeAndAcknowledge(
const std::vector<std::string>& buffer,
222 for(
const auto& it : buffer)
247 writeAndAcknowledge(it);
253 void OtsUDPHardware::read(
const std::string& sendBuffer,
254 std::string& receiveBuffer,
255 int timeoutSeconds)
try
258 int clearedPackets = OtsUDPHardware::clearReadSocket();
260 __COUT__ <<
"Cleared receive socket buffer: " << clearedPackets
261 <<
" packets cleared." << std::endl;
264 __COUT__ <<
"Sending" << std::endl;
265 TransceiverSocket::send(OtsUDPBoard_, sendBuffer, verbose_);
267 if(timeoutSeconds < 0)
270 __COUT__ <<
"Receiving" << std::endl;
271 if(TransceiverSocket::receive(
272 receiveBuffer, timeoutSeconds, 0 , verbose_) < 0)
274 __SS__ <<
"Read failed from hardware at IP : Port " << OtsUDPBoard_.getIPAddress()
275 <<
" : " << OtsUDPBoard_.getPort() <<
". Timeout period of "
276 << timeoutSeconds <<
" seconds reached without response." << __E__;
294 if((receiveBuffer[0] >> 5))
296 __SS__ <<
"Error in OTS protocol encountered! " << std::setfill(
'0')
297 << std::setw(2) << std::hex << (((int16_t)receiveBuffer[0]) & 0xff) <<
"-"
298 << std::dec << __E__;
302 catch(std::runtime_error& e)
308 __SS__ <<
"Unrecognized exception caught!" << std::endl;
313 void OtsUDPHardware::read(
const std::vector<std::string>& sendBuffers,
314 std::vector<std::string>& receiveBuffers,
317 receiveBuffers.resize(sendBuffers.size());
318 auto receiveBufferIterator = receiveBuffers.begin();
319 for(
const auto& sendBuffer : sendBuffers)
320 read(sendBuffer, *(receiveBufferIterator++));
324 void OtsUDPHardware::read(
const std::string& sendBuffer,
325 uint64_t& receiveQuadWord,
328 std::string receiveBuffer;
329 read(sendBuffer, receiveBuffer);
332 if(receiveBuffer.length() != 10)
334 __SS__ <<
"Read uint64_t quad-word failed. Invalid size of received buffer: "
335 << receiveBuffer.length() <<
" != " << 10 << std::endl;
344 receiveQuadWord = *((uint64_t*)&receiveBuffer[2]);
351 void OtsUDPHardware::read(
const std::string& sendBuffer,
352 std::vector<uint64_t>& receiveQuadWords,
355 receiveQuadWords.resize(0);
357 std::string receiveBuffer;
358 read(sendBuffer, receiveBuffer);
361 if((receiveBuffer.length() - 2) % 8 != 0)
363 __SS__ <<
"Read vector of uint64_t quad-word failed. Invalid size of received "
365 << receiveBuffer.length() <<
" - 2) % 8 != 0" << std::endl;
369 for(
unsigned int i = 2; i < receiveBuffer.length(); i += 8)
371 receiveQuadWords.push_back(uint64_t());
376 receiveQuadWords[receiveQuadWords.size() - 1] = *((uint64_t*)&receiveBuffer[i]);
381 void OtsUDPHardware::read(
const std::vector<std::string>& sendBuffers,
382 std::vector<std::vector<uint64_t> >& receiveQuadWordsVector,
385 receiveQuadWordsVector.resize(
388 std::string receiveBuffer;
391 for(
unsigned int b = 0; b < sendBuffers.size(); ++b)
393 receiveQuadWordsVector[b].resize(0);
395 read(sendBuffers[b], receiveBuffer);
398 if((receiveBuffer.length() - 2) % 8 != 0)
400 __SS__ <<
"Read vector of uint64_t quad-word failed. Invalid size of "
402 << receiveBuffer.length() <<
" - 2) % 8 != 0" << std::endl;
407 for(
unsigned int i = 2; i < receiveBuffer.length(); i += 8)
409 receiveQuadWordsVector[b].push_back(uint64_t());
410 std::copy_n((
char*)&receiveBuffer[i],
412 &receiveQuadWordsVector[b][receiveQuadWordsVector[b].size() - 1]);
424 int OtsUDPHardware::clearReadSocket()
426 std::string dummerReceiveBuffer;
432 while(TransceiverSocket::receive(dummerReceiveBuffer,
441 __COUT__ <<
"I am crashing while trying to read the socket...strange!"