00001 #include "otsdaq-components/FEInterfaces/FEOtsUDPTemplateInterface.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutMacros.h"
00004 #include "otsdaq-core/Macros/InterfacePluginMacros.h"
00005 #include <iostream>
00006 #include <set>
00007
00008 using namespace ots;
00009
00010
00011 FEOtsUDPTemplateInterface::FEOtsUDPTemplateInterface(const std::string& interfaceUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& interfaceConfigurationPath)
00012 : Socket (
00013 theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("HostIPAddress").getValue<std::string>()
00014 , theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("HostPort").getValue<unsigned int>())
00015 , FEVInterface (interfaceUID, theXDAQContextConfigTree, interfaceConfigurationPath)
00016 , OtsUDPHardware (theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("InterfaceIPAddress").getValue<std::string>()
00017 , theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("InterfacePort").getValue<unsigned int>())
00018 , OtsUDPFirmwareDataGen(theXDAQContextConfigTree.getNode(interfaceConfigurationPath).getNode("FirmwareVersion").getValue<unsigned int>())
00019 {
00020
00021
00022 universalAddressSize_ = 8;
00023 universalDataSize_ = 8;
00024
00025
00026 if(1)
00027 {
00028
00029 FEVInterface::registerFEMacroFunction("varTest2",
00030 static_cast<FEVInterface::frontEndMacroFunction_t>(&FEOtsUDPTemplateInterface::varTest2),
00031 std::vector<std::string>{"myOtherArg"},
00032 std::vector<std::string>{"myArg","outArg1"},
00033 1);
00034
00035
00036
00037 FEVInterface::registerFEMacroFunction("varTest",
00038 static_cast<FEVInterface::frontEndMacroFunction_t>(&FEOtsUDPTemplateInterface::varTest),
00039 std::vector<std::string>{"myOtherArg"},
00040 std::vector<std::string>{"myArg","outArg1"},
00041 1);
00042
00043 std::vector<frontEndMacroArg_t> argsIn;
00044 __SET_ARG_IN__("myOtherArg",(unsigned int)5);
00045
00046 std::vector<frontEndMacroArg_t> argsOut;
00047
00048 __FE_COUTV__(StringMacros::vectorToString(argsIn));
00049
00050 runSelfFrontEndMacro("varTest2",
00051 argsIn,argsOut);
00052
00053 __FE_COUTV__(StringMacros::vectorToString(argsOut));
00054 __FE_COUTV__(FEVInterface::getFEMacroArgument(
00055 argsOut,"outArg1"));
00056
00057 {
00058 std::string a = FEVInterface::getFEMacroArgument(
00059 argsOut,"myArg");
00060 double b = getFEMacroArgumentValue<double>(
00061 argsOut,"outArg1");
00062 unsigned short c = getFEMacroArgumentValue<unsigned short>(
00063 argsOut,"outArg1");
00064 __FE_COUTV__(a);
00065 __FE_COUTV__(b);
00066 __FE_COUTV__(c);
00067 }
00068 }
00069
00070 }
00071
00072
00073 FEOtsUDPTemplateInterface::~FEOtsUDPTemplateInterface(void)
00074 {}
00075
00076
00077 void FEOtsUDPTemplateInterface::configure(void)
00078 {
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 __FE_COUT__ << "configure" << __E__;
00171 __FE_COUT__ << "Clearing receive socket buffer: " << OtsUDPHardware::clearReadSocket() << " packets cleared." << __E__;
00172
00173 std::string sendBuffer;
00174 std::string recvBuffer;
00175 uint64_t readQuadWord;
00176
00177 __FE_COUT__ << "Configuration Path Table: " <<
00178 theXDAQContextConfigTree_.getNode(theConfigurationPath_).getConfigurationName() <<
00179 "-v" <<
00180 theXDAQContextConfigTree_.getNode(theConfigurationPath_).getConfigurationVersion() <<
00181 __E__;
00182
00183 __FE_COUT__ << "Interface name: " <<
00184 theXDAQContextConfigTree_.getNode(theConfigurationPath_) << __E__;
00185
00186 __FE_COUT__ << "Configured Firmware Version: " <<
00187 theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("FirmwareVersion").getValue<unsigned int>()
00188 << __E__;
00189
00190 __FE_COUT__ << "Setting Destination IP: " <<
00191 theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToIPAddress").getValue<std::string>()
00192 << __E__;
00193 __FE_COUT__ << "And Destination Port: " <<
00194 theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToPort").getValue<unsigned int>()
00195 << __E__;
00196
00197 OtsUDPFirmwareCore::setDataDestination(sendBuffer,
00198 theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToIPAddress").getValue<std::string>(),
00199 theXDAQContextConfigTree_.getNode(theConfigurationPath_).getNode("StreamToPort").getValue<uint64_t>()
00200 );
00201 OtsUDPHardware::write(sendBuffer);
00202
00203 __FE_COUT__ << "Reading back burst dest MAC/IP/Port: " << __E__;
00204
00205 OtsUDPFirmwareCore::readDataDestinationMAC(sendBuffer);
00206 OtsUDPHardware::read(sendBuffer,readQuadWord);
00207
00208 OtsUDPFirmwareCore::readDataDestinationIP(sendBuffer);
00209 OtsUDPHardware::read(sendBuffer,readQuadWord);
00210
00211 OtsUDPFirmwareCore::readDataDestinationPort(sendBuffer);
00212 OtsUDPHardware::read(sendBuffer,readQuadWord);
00213
00214
00215
00216 OtsUDPFirmwareCore::readControlDestinationPort(sendBuffer);
00217 OtsUDPHardware::read(sendBuffer,readQuadWord);
00218
00219
00220 FEVInterface::runSequenceOfCommands("LinkToConfigureSequence");
00221
00222 __FE_COUT__ << "Done with ots Template configuring." << __E__;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232 void FEOtsUDPTemplateInterface::halt(void)
00233 {
00234 __FE_COUT__ << "\tHalt" << __E__;
00235 stop();
00236 }
00237
00238
00239 void FEOtsUDPTemplateInterface::pause(void)
00240 {
00241 __FE_COUT__ << "\tPause" << __E__;
00242 stop();
00243 }
00244
00245
00246 void FEOtsUDPTemplateInterface::resume(void)
00247 {
00248 __FE_COUT__ << "\tResume" << __E__;
00249 start("");
00250 }
00251
00252
00253 void FEOtsUDPTemplateInterface::start(std::string )
00254 {
00255 __FE_COUT__ << "\tStart" << __E__;
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 FEVInterface::runSequenceOfCommands("LinkToStartSequence");
00278
00279 std::string sendBuffer;
00280 OtsUDPFirmwareCore::startBurst(sendBuffer);
00281 OtsUDPHardware::write(sendBuffer);
00282 }
00283
00284
00285 void FEOtsUDPTemplateInterface::stop(void)
00286 {
00287 __FE_COUT__ << "\tStop" << __E__;
00288
00289
00290
00291 FEVInterface::runSequenceOfCommands("LinkToStopSequence");
00292
00293 std::string sendBuffer;
00294 OtsUDPFirmwareCore::stopBurst(sendBuffer);
00295 OtsUDPHardware::write(sendBuffer);
00296 }
00297
00298
00299 bool FEOtsUDPTemplateInterface::running(void)
00300 {
00301 __FE_COUT__ << "\tRunning" << __E__;
00302
00303
00304
00305
00306
00307 int state = -1;
00308 while(WorkLoop::continueWorkLoop_)
00309 {
00310
00311
00312
00313 ++state;
00314 if(state < 8)
00315 sleep(1);
00316 else
00317 {
00318
00319
00320
00321
00322
00323 break;
00324 }
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 return false;
00368 }
00369
00370
00371
00372
00373 void ots::FEOtsUDPTemplateInterface::universalRead(char *address, char *returnValue)
00374 {
00375 __FE_COUT__ << "address size " << universalAddressSize_ << __E__;
00376
00377 __FE_COUT__ << "Universal Read Address: ";
00378 for(unsigned int i=0;i<universalAddressSize_;++i)
00379 printf("%2.2X",(unsigned char)address[i]);
00380 std::cout << __E__;
00381
00382 std::string readBuffer, sendBuffer;
00383 OtsUDPFirmwareCore::readAdvanced(sendBuffer,address,1 );
00384
00385 OtsUDPHardware::read(sendBuffer, readBuffer);
00386
00387 __FE_COUT__ << "Result SIZE: " << readBuffer.size() << __E__;
00388 std::memcpy(returnValue,readBuffer.substr(2).c_str(),universalDataSize_);
00389
00390 __FE_COUT__ << "Universal Read Data: ";
00391 for(unsigned int i=0;i<universalDataSize_;++i)
00392 printf("%2.2X",(unsigned char)returnValue[i]);
00393 std::cout << __E__;
00394
00395 }
00396
00397
00398
00399
00400 void ots::FEOtsUDPTemplateInterface::universalWrite(char* address, char* writeValue)
00401 {
00402 __FE_COUT__ << "address size " << universalAddressSize_ << __E__;
00403 __FE_COUT__ << "data size " << universalDataSize_ << __E__;
00404 __FE_COUT__ << "Universal Write Address: ";
00405 for(unsigned int i=0;i<universalAddressSize_;++i)
00406 printf("%2.2X",(unsigned char)address[i]);
00407 std::cout << __E__;
00408 __FE_COUT__ << "Universal Write Data: ";
00409 for(unsigned int i=0;i<universalDataSize_;++i)
00410 printf("%2.2X",(unsigned char)writeValue[i]);
00411 std::cout << __E__;
00412
00413 std::string sendBuffer;
00414 OtsUDPFirmwareCore::writeAdvanced(sendBuffer,address,writeValue,1 );
00415 OtsUDPHardware::write(sendBuffer);
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425 void FEOtsUDPTemplateInterface::varTest(__ARGS__)
00426 {
00427 __FE_COUT__ << "# of input args = " << argsIn.size() << __E__;
00428 __FE_COUT__ << "# of output args = " << argsOut.size() << __E__;
00429 for(auto &argIn:argsIn)
00430 __FE_COUT__ << argIn.first << ": " << argIn.second << __E__;
00431
00432
00433 {
00434 char *address = new char[universalAddressSize_]{0};
00435 char *data = new char[universalDataSize_]{0};
00436 uint64_t macroAddress;
00437 uint64_t macroData;
00438 std::map<std::string ,uint64_t > macroArgs;
00439
00440
00441 macroAddress = 0x1002; memcpy(address,¯oAddress,8);
00442 universalRead(address,data); memcpy(¯oArgs["myArg"],data,8);
00443 __SET_ARG_OUT__("myArg",macroArgs["myArg"]);
00444
00445
00446 macroAddress = 0x1001; memcpy(address,¯oAddress,8);
00447 universalRead(address,data); memcpy(¯oArgs["outArg1"],data,8);
00448 __SET_ARG_OUT__("outArg1",macroArgs["outArg1"]);
00449
00450
00451 macroAddress = 0x1002; memcpy(address,¯oAddress,8);
00452 macroArgs["myOtherArg"] = __GET_ARG_IN__("myOtherArg", uint64_t);
00453 memcpy(data,¯oArgs["myOtherArg"],8);
00454 universalWrite(address,data);
00455
00456
00457 macroAddress = 0x1001; memcpy(address,¯oAddress,8);
00458 memcpy(data,¯oArgs["myArg"],8);
00459 universalWrite(address,data);
00460
00461
00462 __FE_COUT__ << "Sleeping for... " << 4 << " milliseconds " << __E__;
00463 usleep(4*1000 );
00464
00465
00466 delete[] address;
00467 delete[] data;
00468 }
00469
00470 for(auto &argOut:argsOut)
00471 __FE_COUT__ << argOut.first << ": " << argOut.second << __E__;
00472
00473 }
00474
00475
00476
00477
00478
00479
00480 void FEOtsUDPTemplateInterface::varTest2(__ARGS__)
00481 {
00482 __FE_COUT__ << "# of input args = " << argsIn.size() << __E__;
00483 __FE_COUT__ << "# of output args = " << argsOut.size() << __E__;
00484 for(auto &argIn:argsIn)
00485 __FE_COUT__ << argIn.first << ": " << argIn.second << __E__;
00486
00487 __SET_ARG_OUT__("myArg","hello2");
00488 uint64_t macroData = __GET_ARG_IN__("myOtherArg", uint64_t);
00489 macroData *= 3;
00490 __SET_ARG_OUT__("outArg1",macroData);
00491
00492 for(auto &argOut:argsOut)
00493 __FE_COUT__ << argOut.first << ": " << argOut.second << __E__;
00494
00495 }
00496
00497 DEFINE_OTS_INTERFACE(FEOtsUDPTemplateInterface)