otsdaq_demo  v2_05_02_indev
myNewInterface_interface.cc
1 #include <iostream>
2 #include <set>
3 #include "otsdaq-demo/FEInterfaces/myNewInterface.h"
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/Macros/InterfacePluginMacros.h"
6 #include "otsdaq/MessageFacility/MessageFacility.h"
7 
8 using namespace ots;
9 
10 #undef __MF_SUBJECT__
11 #define __MF_SUBJECT__ "FE-myNewInterface"
12 
13 //==============================================================================
14 myNewInterface::myNewInterface(const std::string& interfaceUID,
15  const ConfigurationTree& theXDAQContextConfigTree,
16  const std::string& interfaceConfigurationPath)
17  : Socket(theXDAQContextConfigTree.getNode(interfaceConfigurationPath)
18  .getNode("HostIPAddress")
19  .getValue<std::string>(),
20  theXDAQContextConfigTree.getNode(interfaceConfigurationPath)
21  .getNode("HostPort")
22  .getValue<unsigned int>())
23  , FEOtsUDPTemplateInterface(
24  interfaceUID, theXDAQContextConfigTree, interfaceConfigurationPath)
25 {
26  // register FE Macro Functions
27  registerFEMacroFunction(
28  "testFunction", // feMacroName
29  static_cast<FEVInterface::frontEndMacroFunction_t>(
30  &myNewInterface::testFunction), // feMacroFunction
31  std::vector<std::string>{"arg1", "arg2", "arg3"}, // namesOfInputArgs
32  std::vector<std::string>{"oarg1", "oarg2"}, // namesOfOutputArgs
33  255); // requiredUserPermissions
34  registerFEMacroFunction(
35  "testFunction2",
36  static_cast<FEVInterface::frontEndMacroFunction_t>(&myNewInterface::testFunction),
37  std::vector<std::string>{"targ1"},
38  std::vector<std::string>{"toarg1"},
39  1);
40 
41  std::vector<frontEndMacroInArg_t> argsIn;
42  argsIn.push_back(frontEndMacroInArg_t("arg1", "val1"));
43  argsIn.push_back(frontEndMacroInArg_t("arg2", "val2"));
44  argsIn.push_back(frontEndMacroInArg_t("arg3", "val3"));
45 
46  __COUT__ << std::endl;
47  __COUT__ << argsIn[0].first << std::endl;
48 
49  __COUT__ << std::endl;
50 
51  __COUT__ << __COUT_HDR_P__ << "# of args = " << argsIn.size() << std::endl;
52  for(auto& argIn : argsIn)
53  __COUT__ << argIn.first << ": " << argIn.second << std::endl;
54 
55  std::vector<std::string> returnStrings;
56  std::vector<frontEndMacroOutArg_t> argsOut;
57 
58  std::string outputArgs = "oarg1,oarg2,";
59  {
60  std::istringstream inputStream(outputArgs);
61  std::string argName;
62  while(getline(inputStream, argName, ','))
63  {
64  __COUT__ << "argName " << argName << std::endl;
65 
66  returnStrings.push_back(std::string("test"));
67  argsOut.push_back(FEVInterface::frontEndMacroOutArg_t(
68  argName, returnStrings[returnStrings.size() - 1]));
69  //
70  // __COUT__ << argsOut[argsOut.size()-1].first << std::endl;
71  // __COUT__ << argsOut[argsOut.size()-1].second << std::endl;
72  }
73  }
74 
75  auto mapOfFEMacroIt = mapOfFEMacroFunctions_.find("testFunction");
76  if(mapOfFEMacroIt != mapOfFEMacroFunctions_.end())
77  {
78  (this->*(mapOfFEMacroIt->second.macroFunction_))(argsIn, argsOut);
79  __COUT__ << "Made it " << std::endl;
80  for(auto& arg : argsOut)
81  __COUT__ << arg.first << ": " << arg.second << std::endl;
82  }
83 
84  // std::function<void(std::vector<std::pair<std::string,std::string> >)> my =
85  // &myNewInterface::testFunction;
86 }
87 
88 //==============================================================================
89 myNewInterface::~myNewInterface(void) {}
90 
91 //==============================================================================
92 void myNewInterface::testFunction(__ARGS__)
93 {
94  __COUT__ << "in...\n"
95  << __COUT_HDR_P__ << "# of input args = " << argsIn.size() << std::endl;
96  __COUT__ << "in...\n"
97  << __COUT_HDR_P__ << "# of output args = " << argsOut.size() << std::endl;
98  for(auto& argIn : argsIn)
99  __COUT__ << argIn.first << ": " << argIn.second << std::endl;
100 
101  // unsigned int numberOfTriggers = __GET_ARG_IN__("numberOfTriggers",unsigned int);
102  // unsigned int signalHiDuration = __GET_ARG_IN__("signalHiDuration",unsigned int);
103  // unsigned int signalLoDuration = __GET_ARG_IN__("signalLoDuration",unsigned int);
104  // std::string& triggersWereLaunched = __GET_ARG_OUT__("triggersWereLaunched");
105  // std::string numberOfTriggersStr = __GET_ARG_IN__("numberOfTriggers",std::string);
106  //
107  // __COUTV__(numberOfTriggers);
108  // __COUTV__(signalHiDuration);
109  // __COUTV__(signalLoDuration);
110  // __COUTV__(numberOfTriggersStr);
111 
112  // __COUT__ << "triggersWereLaunched " << triggersWereLaunched << __E__;
113  // triggersWereLaunched = "Done!";
114  // __COUT__ << "triggersWereLaunched " << triggersWereLaunched << __E__;
115  // __SET_ARG_OUT__("triggersWereLaunched",42.2f);//,unsigned int);
116  // __COUT__ << "triggersWereLaunched " << triggersWereLaunched << __E__;
117  // __SET_ARG_OUT__("triggersWereLaunched",42.2f);//,float);
118  // __COUT__ << "triggersWereLaunched " << triggersWereLaunched << __E__;
119 
120  for(unsigned int i = 0; i < argsOut.size(); ++i)
121  argsOut[i].second = argsOut[i].first + "-NewValue";
122 }
123 
124 //==============================================================================
125 void myNewInterface::configure(void)
126 {
127  __COUT__ << "configure" << std::endl;
128  __COUT__ << "Clearing receive socket buffer: " << OtsUDPHardware::clearReadSocket()
129  << " packets cleared." << std::endl;
130 
131  std::string writeBuffer;
132  // std::string readBuffer;
133 
134  __COUT__ << "Setting Destination IP: "
135  << theXDAQContextConfigTree_.getNode(theConfigurationPath_)
136  .getNode("StreamToIPAddress")
137  .getValue<std::string>()
138  << std::endl;
139  __COUT__ << "And Destination Port: "
140  << theXDAQContextConfigTree_.getNode(theConfigurationPath_)
141  .getNode("StreamToPort")
142  .getValue<unsigned int>()
143  << std::endl;
144 
145  writeBuffer.resize(0);
146  OtsUDPFirmwareCore::setDataDestination(
147  writeBuffer,
148  theXDAQContextConfigTree_.getNode(theConfigurationPath_)
149  .getNode("StreamToIPAddress")
150  .getValue<std::string>(),
151  theXDAQContextConfigTree_.getNode(theConfigurationPath_)
152  .getNode("StreamToPort")
153  .getValue<uint64_t>());
154  OtsUDPHardware::write(writeBuffer);
155 
156  // __COUT__ << "CCD Test CLOCK Freq" <<
157  // theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("CCDClockFrequency").getValue<uint32_t>()
158  //<< std::endl;
159  //
160  // int CcdClockAddress = 0x4;
161  // int CddClockValue =
162  // theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("CCDClockFrequency").getValue<uint32_t>();
163  //
164  //
165  // writeBuffer.resize(0);
166  // OtsUDPFirmware::write(writeBuffer,CcdClockAddress,CddClockValue);
167  // OtsUDPHardware::write(writeBuffer);
168 
169  //
170  //
171  // __COUT__ << "Reading back burst dest MAC/IP/Port: " << std::endl;
172  // writeBuffer.resize(0);
173  // OtsUDPFirmware::readBurstDestinationMAC(writeBuffer);
174  // OtsUDPHardware::read(writeBuffer,readBuffer);
175  // writeBuffer.resize(0);
176  // OtsUDPFirmware::readBurstDestinationIP(writeBuffer);
177  // OtsUDPHardware::read(writeBuffer,readBuffer);
178  // writeBuffer.resize(0);
179  // OtsUDPFirmware::readBurstDestinationPort(writeBuffer);
180  // OtsUDPHardware::read(writeBuffer,readBuffer);
181  //
182  //
183  __COUT__ << "Done with configuring." << std::endl;
184 }
185 
186 //==============================================================================
187 // void myNewInterface::configureDetector(const DACStream& theDACStream)
188 //{
189 // __COUT__ << "\tconfigureDetector" << std::endl;
190 //}
191 
192 //==============================================================================
193 void myNewInterface::halt(void)
194 {
195  __COUT__ << "\tHalt" << std::endl;
196  stop();
197 }
198 
199 //==============================================================================
200 void myNewInterface::pause(void)
201 {
202  __COUT__ << "\tPause" << std::endl;
203  stop();
204 }
205 
206 //==============================================================================
207 void myNewInterface::resume(void)
208 {
209  __COUT__ << "\tResume" << std::endl;
210  start("");
211 }
212 
213 //==============================================================================
214 void myNewInterface::start(std::string) // runNumber)
215 {
216  __COUT__ << "\tStart" << std::endl;
217  OtsUDPHardware::write(OtsUDPFirmware::startBurst());
218 }
219 
220 //==============================================================================
221 void myNewInterface::stop(void)
222 {
223  __COUT__ << "\tStop" << std::endl;
224  OtsUDPHardware::write(OtsUDPFirmware::stopBurst());
225 }
226 
227 //==============================================================================
228 bool myNewInterface::running(void)
229 {
230  __COUT__ << "\running" << std::endl;
231 
232  // //example!
233  // //play with array of 8 LEDs at address 0x1003
234 
235  bool flashLEDsWhileRunning = false;
236  if(flashLEDsWhileRunning)
237  {
238  std::string writeBuffer;
239  int state = -1;
240  while(WorkLoop::continueWorkLoop_)
241  {
242  // while running
243  // play with the LEDs at address 0x1003
244 
245  ++state;
246  if(state < 8)
247  {
248  writeBuffer.resize(0);
249  OtsUDPFirmware::write(writeBuffer, 0x1003, 1 << state);
250  OtsUDPHardware::write(writeBuffer);
251  }
252  else if(state % 2 == 1 && state < 11)
253  {
254  writeBuffer.resize(0);
255  OtsUDPFirmware::write(writeBuffer, 0x1003, 0xFF);
256  OtsUDPHardware::write(writeBuffer);
257  }
258  else if(state % 2 == 0 && state < 11)
259  {
260  writeBuffer.resize(0);
261  OtsUDPFirmware::write(writeBuffer, 0x1003, 0);
262  OtsUDPHardware::write(writeBuffer);
263  }
264  else
265  state = -1;
266 
267  sleep(1);
268  }
269  }
270 
271  return false;
272 }
273 
274 //==============================================================================
275 // NOTE: buffer for address must be at least size universalAddressSize_
276 // NOTE: buffer for returnValue must be max UDP size to handle return possibility
277 int ots::myNewInterface::universalRead(char* address, char* returnValue)
278 {
279  __COUT__ << "address size " << universalAddressSize_ << std::endl;
280 
281  __COUT__ << "Request: ";
282  for(unsigned int i = 0; i < universalAddressSize_; ++i)
283  printf("%2.2X", (unsigned char)address[i]);
284  std::cout << std::endl;
285 
286  std::string readBuffer(universalDataSize_, 0); // 0 fill to correct number of bytes
287 
288  // OtsUDPHardware::read(FSSRFirmware::universalRead(address), readBuffer) < 0;
289  if(OtsUDPHardware::read(OtsUDPFirmware::universalRead(address), readBuffer) <
290  0) // data reply
291  {
292  __COUT__ << "Caught it! This is when it's getting time out error" << std::endl;
293  return -1;
294  }
295  __COUT__ << "Result SIZE: " << readBuffer.size() << std::endl;
296  std::memcpy(returnValue, readBuffer.substr(2).c_str(), universalDataSize_);
297  return 0;
298 }
299 
300 //==============================================================================
301 // NOTE: buffer for address must be at least size universalAddressSize_
302 // NOTE: buffer for writeValue must be at least size universalDataSize_
303 void ots::myNewInterface::universalWrite(char* address, char* writeValue)
304 {
305  __COUT__ << "address size " << universalAddressSize_ << std::endl;
306  __COUT__ << "data size " << universalDataSize_ << std::endl;
307  __COUT__ << "Sending: ";
308  for(unsigned int i = 0; i < universalAddressSize_; ++i)
309  printf("%2.2X", (unsigned char)address[i]);
310  std::cout << std::endl;
311 
312  OtsUDPHardware::write(
313  OtsUDPFirmware::universalWrite(address, writeValue)); // data request
314 }
315 
316 DEFINE_OTS_INTERFACE(myNewInterface)