otsdaq_components  v2_01_00
FEOtsUDPTemplateInterface_interface.cc
1 #include "otsdaq-components/FEInterfaces/FEOtsUDPTemplateInterface.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.h"
4 #include "otsdaq-core/Macros/InterfacePluginMacros.h"
5 #include <iostream>
6 #include <set>
7 
8 using namespace ots;
9 
10 #undef __MF_SUBJECT__
11 #define __MF_SUBJECT__ "FE-FEOtsUDPTemplateInterface"
12 
13 //========================================================================================================================
14 FEOtsUDPTemplateInterface::FEOtsUDPTemplateInterface(const std::string& interfaceUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& interfaceConfigurationPath)
15 : Socket (
16  theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("HostIPAddress").getValue<std::string>()
17  , theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("HostPort").getValue<unsigned int>())
18 , FEVInterface (interfaceUID, theXDAQContextConfigTree, interfaceConfigurationPath)
19 , OtsUDPHardware (theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("InterfaceIPAddress").getValue<std::string>()
20  , theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("InterfacePort").getValue<unsigned int>())
21 , OtsUDPFirmwareDataGen(theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("FirmwareVersion").getValue<unsigned int>())
22 {
23  universalAddressSize_ = 8;
24  universalDataSize_ = 8;
25 }
26 
27 //========================================================================================================================
28 FEOtsUDPTemplateInterface::~FEOtsUDPTemplateInterface(void)
29 {}
30 
31 //========================================================================================================================
32 void FEOtsUDPTemplateInterface::configure(void)
33 {
34  __CFG_COUT__ << "configure" << std::endl;
35  __CFG_COUT__ << "Clearing receive socket buffer: " << OtsUDPHardware::clearReadSocket() << " packets cleared." << std::endl;
36 
37  std::string sendBuffer;
38  std::string recvBuffer;
39  uint64_t readQuadWord;
40 
41  __CFG_COUT__ << "Configuration Path Table: " <<
42  theXDAQContextConfigTree_.getNode(theConfigurationPath_).getConfigurationName() <<
43  "-v" <<
44  theXDAQContextConfigTree_.getNode(theConfigurationPath_).getConfigurationVersion() <<
45  std::endl;
46 
47  __CFG_COUT__ << "Interface name: " <<
48  theXDAQContextConfigTree_.getNode(theConfigurationPath_) << __E__;
49 
50  __CFG_COUT__ << "Configured Firmware Version: " <<
51  theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("FirmwareVersion").getValue<unsigned int>()
52  << std::endl;
53 
54  __CFG_COUT__ << "Setting Destination IP: " <<
55  theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToIPAddress").getValue<std::string>()
56  << std::endl;
57  __CFG_COUT__ << "And Destination Port: " <<
58  theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToPort").getValue<unsigned int>()
59  << std::endl;
60 
61  OtsUDPFirmwareCore::setDataDestination(sendBuffer,
62  theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToIPAddress").getValue<std::string>(),
63  theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToPort").getValue<uint64_t>()
64  );
65  OtsUDPHardware::write(sendBuffer);
66 
67  __CFG_COUT__ << "Reading back burst dest MAC/IP/Port: " << std::endl;
68 
69  OtsUDPFirmwareCore::readDataDestinationMAC(sendBuffer);
70  OtsUDPHardware::read(sendBuffer,readQuadWord);
71 
72  OtsUDPFirmwareCore::readDataDestinationIP(sendBuffer);
73  OtsUDPHardware::read(sendBuffer,readQuadWord);
74 
75  OtsUDPFirmwareCore::readDataDestinationPort(sendBuffer);
76  OtsUDPHardware::read(sendBuffer,readQuadWord);
77 
78 
79 
80  OtsUDPFirmwareCore::readControlDestinationPort(sendBuffer);
81  OtsUDPHardware::read(sendBuffer,readQuadWord);
82 
83  //Run Configure Sequence Commands
84  FEVInterface::runSequenceOfCommands("LinkToConfigureSequence");
85 
86  __CFG_COUT__ << "Done with ots Template configuring." << std::endl;
87 }
88 
89 //========================================================================================================================
90 //void FEOtsUDPTemplateInterface::configureDetector(const DACStream& theDACStream)
91 //{
92 // __CFG_COUT__ << "\tconfigureDetector" << std::endl;
93 //}
94 
95 //========================================================================================================================
96 void FEOtsUDPTemplateInterface::halt(void)
97 {
98  __CFG_COUT__ << "\tHalt" << std::endl;
99  stop();
100 }
101 
102 //========================================================================================================================
103 void FEOtsUDPTemplateInterface::pause(void)
104 {
105  __CFG_COUT__ << "\tPause" << std::endl;
106  stop();
107 }
108 
109 //========================================================================================================================
110 void FEOtsUDPTemplateInterface::resume(void)
111 {
112  __CFG_COUT__ << "\tResume" << std::endl;
113  start("");
114 }
115 
116 //========================================================================================================================
117 void FEOtsUDPTemplateInterface::start(std::string )//runNumber)
118 {
119  __CFG_COUT__ << "\tStart" << std::endl;
120 
121 
122  //Run Start Sequence Commands
123  FEVInterface::runSequenceOfCommands("LinkToStartSequence");
124 
125  std::string sendBuffer;
126  OtsUDPFirmwareCore::startBurst(sendBuffer);
127  OtsUDPHardware::write(sendBuffer);
128 }
129 
130 //========================================================================================================================
131 void FEOtsUDPTemplateInterface::stop(void)
132 {
133  __CFG_COUT__ << "\tStop" << std::endl;
134 
135  //Run Stop Sequence Commands
136 
137  FEVInterface::runSequenceOfCommands("LinkToStopSequence");
138 
139  std::string sendBuffer;
140  OtsUDPFirmwareCore::stopBurst(sendBuffer);
141  OtsUDPHardware::write(sendBuffer);
142 }
143 
144 //========================================================================================================================
145 bool FEOtsUDPTemplateInterface::running(void)
146 {
147  __CFG_COUT__ << "\tRunning" << std::endl;
148 
149  int state = -1;
150  while(WorkLoop::continueWorkLoop_)
151  {
152  //while running
153  //play with the LEDs at address 0x1003
154 
155  ++state;
156  if(state < 8)
157  sleep(1);
158  else
159  break;
160  }
161 
162  // //example!
163  // //play with array of 8 LEDs at address 0x1003
164 
165  //
166  // bool flashLEDsWhileRunning = false;
167  // if(flashLEDsWhileRunning)
168  // {
169  // std::string writeBuffer;
170  // int state = -1;
171  // while(WorkLoop::continueWorkLoop_)
172  // {
173  // //while running
174  // //play with the LEDs at address 0x1003
175  //
176  // ++state;
177  // if(state < 8)
178  // {
179  // writeBuffer.resize(0);
180  // OtsUDPFirmwareCore::write(writeBuffer, 0x1003,1<<state);
181  // OtsUDPHardware::write(writeBuffer);
182  // }
183  // else if(state%2 == 1 && state < 11)
184  // {
185  // writeBuffer.resize(0);
186  // OtsUDPFirmwareCore::write(writeBuffer, 0x1003, 0xFF);
187  // OtsUDPHardware::write(writeBuffer);
188  // }
189  // else if(state%2 == 0 && state < 11)
190  // {
191  // writeBuffer.resize(0);
192  // OtsUDPFirmwareCore::write(writeBuffer, 0x1003,0);
193  // OtsUDPHardware::write(writeBuffer);
194  // }
195  // else
196  // state = -1;
197  //
198  // sleep(1);
199  // }
200  // }
201 
202  return false;
203 }
204 
205 //========================================================================================================================
206 //NOTE: buffer for address must be at least size universalAddressSize_
207 //NOTE: buffer for returnValue must be max UDP size to handle return possibility
208 int ots::FEOtsUDPTemplateInterface::universalRead(char *address, char *returnValue)
209 {
210  __CFG_COUT__ << "address size " << universalAddressSize_ << std::endl;
211 
212  __CFG_COUT__ << "Request: ";
213  for(unsigned int i=0;i<universalAddressSize_;++i)
214  printf("%2.2X",(unsigned char)address[i]);
215  std::cout << std::endl;
216 
217  std::string readBuffer, sendBuffer;
218  OtsUDPFirmwareCore::readAdvanced(sendBuffer,address,1 /*size*/);
219 
220  //OtsUDPHardware::read(FSSRFirmware::universalRead(address), readBuffer) < 0;
221  try
222  {
223  OtsUDPHardware::read(sendBuffer, readBuffer); // data reply
224  }
225  catch(std::runtime_error &e)
226  {
227  __CFG_COUT__ << "Caught it! This is when it's getting time out error" << std::endl;
228  __CFG_COUT_ERR__ << e.what() << std::endl;
229  return -1;
230  }
231 
232  __CFG_COUT__ << "Result SIZE: " << readBuffer.size() << std::endl;
233  std::memcpy(returnValue,readBuffer.substr(2).c_str(),universalDataSize_);
234  return 0;
235 }
236 
237 //========================================================================================================================
238 //NOTE: buffer for address must be at least size universalAddressSize_
239 //NOTE: buffer for writeValue must be at least size universalDataSize_
240 void ots::FEOtsUDPTemplateInterface::universalWrite(char* address, char* writeValue)
241 {
242  __CFG_COUT__ << "address size " << universalAddressSize_ << std::endl;
243  __CFG_COUT__ << "data size " << universalDataSize_ << std::endl;
244  __CFG_COUT__ << "Sending: ";
245  for(unsigned int i=0;i<universalAddressSize_;++i)
246  printf("%2.2X",(unsigned char)address[i]);
247  std::cout << std::endl;
248 
249  std::string sendBuffer;
250  OtsUDPFirmwareCore::writeAdvanced(sendBuffer,address,writeValue,1 /*size*/);
251  OtsUDPHardware::write(sendBuffer); // data request
252 }
253 
254 DEFINE_OTS_INTERFACE(FEOtsUDPTemplateInterface)