3 #include <boost/asio.hpp> 4 #include <boost/bind.hpp> 5 #include <boost/lexical_cast.hpp> 6 #include <boost/asio/deadline_timer.hpp> 7 #include <boost/enable_shared_from_this.hpp> 9 using boost::lexical_cast;
20 namespace ba = boost::asio;
21 namespace bs = boost::system;
22 namespace dummy = ba::placeholders;
24 using boost::lexical_cast;
34 class tcp_connection :
public ba::ip::tcp::socket,
public boost::enable_shared_from_this<tcp_connection>
42 ba::async_read(*
this, buffers,
44 dummy::error, dummy::bytes_transferred));
49 ba::async_write(*
this, buffers,
51 dummy::error, dummy::bytes_transferred));
53 void AsyncWait(ba::deadline_timer &timer,
int seconds,
56 timer.expires_from_now(boost::posix_time::milliseconds(seconds));
57 timer.async_wait(boost::bind(handler, shared_from_this(), dummy::error));
64 fTriggerDynData(ioservice), fTriggerSendData(ioservice)
70 fHeader.fBoardId=0xaffe;
71 fHeader.fFirmwareId=0x42;
77 fStaticData.fMultiplicityPhysics = 1;
78 fStaticData.fMultiplicityCalib = 40;
79 fStaticData.fWindowCalib = 1;
80 fStaticData.fWindowPhysics = 0;
81 fStaticData.fDelayTrigger = 21;
82 fStaticData.fDelayTimeMarker = 42;
83 fStaticData.fDeadTime = 84;
85 fStaticData.fClockConditioner[0] = 100;
86 fStaticData.fClockConditioner[1] = 1;
87 fStaticData.fClockConditioner[2] = 8;
88 fStaticData.fClockConditioner[3] = 9;
89 fStaticData.fClockConditioner[4] = 11;
90 fStaticData.fClockConditioner[5] = 13;
91 fStaticData.fClockConditioner[6] = 14;
92 fStaticData.fClockConditioner[7] = 15;
94 fStaticData.fTriggerSequence = 1 | (2<<5) | (3<<10);
96 fStaticData.fGeneralSettings =
101 fStaticData.fActiveFTU[0] = 0x3ff;
102 fStaticData.fActiveFTU[3] = 0x3ff;
104 for (
int i=0;
i<40;
i++)
106 for (
int p=0; p<4; p++)
109 for (
int p=0; p<5; p++)
110 fStaticData[
i].
fDAC[p] = (p+1)*10;
112 fStaticData[
i].fPrescaling = 42;
115 for (
unsigned long long i=0;
i<40;
i++)
117 fFtuList[
i].fDNA = (
i<<48)|(
i<<32)|(
i<<16)|
i;
118 fFtuList[
i].fPingAddr = (1<<8) |
i;
121 fFtuList[1].fPingAddr = (1<<9) | 1;
122 fFtuList[0].fPingAddr = 0;
124 fFtuList.fNumBoards = 19;
125 fFtuList.fNumBoardsCrate[0] = 9;
126 fFtuList.fNumBoardsCrate[1] = 0;
127 fFtuList.fNumBoardsCrate[2] = 0;
128 fFtuList.fNumBoardsCrate[3] = 10;
132 void HandleSentData(
const boost::system::error_code& error,
size_t bytes_transferred)
134 cout <<
"Data sent: (transmitted=" << bytes_transferred <<
") rc=" << error.message() <<
" (" << error <<
")" << endl;
137 vector<uint16_t> fBufCommand;
159 ba::deadline_timer fTriggerSendData;
163 if (fReportsDisabled)
169 fDynamicData.
fTempSensor[0] = (23. + (6.*rand()/RAND_MAX-3))*10;
170 fDynamicData.
fTempSensor[1] = (55. + (6.*rand()/RAND_MAX-3))*10;
171 fDynamicData.
fTempSensor[2] = (39. + (6.*rand()/RAND_MAX-3))*10;
172 fDynamicData.
fTempSensor[3] = (42. + (6.*rand()/RAND_MAX-3))*10;
174 for (
int i=0;
i<40;
i++)
175 for (
int p=0; p<4; p++)
176 fDynamicData[
i].
fRatePatch[p] = (1000 + (
float(rand())/RAND_MAX-0.5)*25*p);
183 fBufHeader = fHeader.
HtoN();
184 fBufDynamicData = fDynamicData.
HtoN();
186 AsyncWrite(
ba::buffer(ba::const_buffer(&fBufHeader[0], fBufHeader.size()*2)));
188 AsyncWrite(
ba::buffer(ba::const_buffer(&fDelimiter, 2)));
198 for (
int i=0;
i<4;
i++)
201 fBufHeader = fHeader.
HtoN();
202 fBufStaticData = fStaticData.
HtoN();
204 AsyncWrite(
ba::buffer(ba::const_buffer(&fBufHeader[0], fBufHeader.size()*2)));
205 AsyncWrite(
ba::buffer(ba::const_buffer(&fBufStaticData[0], fBufStaticData.size()*2)));
206 AsyncWrite(
ba::buffer(ba::const_buffer(&fDelimiter, 2)));
212 if (bytes_received==0)
220 if (fCommand.size()==0)
222 transform(fBufCommand.begin(), fBufCommand.begin()+bytes_received/2,
223 fBufCommand.begin(), ntohs);
225 if (fBufCommand[0]!=
'@')
227 cout <<
"Inavlid command: 0x" << hex << fBufCommand[0] << dec << endl;
228 cout <<
"Received b=" << bytes_received <<
": " << error.message() <<
" (" << error <<
")" << endl;
229 cout <<
"Hex:" << Converter::GetHex<uint16_t>(&fBufCommand[0], bytes_received) << endl;
233 switch (fBufCommand[1])
236 cout <<
"-> TOGGLE_LED" << endl;
238 fBufCommand.resize(5);
243 cout <<
"-> PING" << endl;
250 fFtuList[1].fPingAddr = ((rand()&1)<<9) | 1;
251 fFtuList[0].fPingAddr = ((rand()&1)<<8);
253 fBufHeader = fHeader.
HtoN();
254 fBufFtuList = fFtuList.
HtoN();
256 AsyncWrite(
ba::buffer(ba::const_buffer(&fBufHeader[0], fBufHeader.size()*2)));
257 AsyncWrite(
ba::buffer(ba::const_buffer(&fBufFtuList[0], fBufFtuList.size()*2)));
258 AsyncWrite(
ba::buffer(ba::const_buffer(&fDelimiter, 2)));
260 fBufCommand.resize(5);
265 cout <<
"-> READ" << endl;
266 switch (fBufCommand[2])
269 cout <<
"-> STATIC" << endl;
273 fBufCommand.resize(5);
279 cout <<
"-> DYNAMIC" << endl;
283 fBufCommand.resize(5);
289 fCommand = fBufCommand;
290 cout <<
"-> REGISTER" << endl;
292 fBufCommand.resize(1);
300 switch (fBufCommand[2])
303 fCommand = fBufCommand;
304 cout <<
"-> REGISTER" << endl;
306 fBufCommand.resize(2);
311 fCommand = fBufCommand;
312 cout <<
"-> STATIC DATA" << endl;
321 cout <<
"-> DISABLE REPORTS " << !fBufCommand[2] << endl;
322 fReportsDisabled = !fBufCommand[2];
324 fBufCommand.resize(5);
329 cout <<
"-> Configure FTU " << (fBufCommand[2]&0xff) <<
" " << (fBufCommand[2]>>8) << endl;
331 fBufCommand.resize(5);
344 fBufCommand.resize(5);
353 fTriggerSendData.cancel();
358 fBufCommand.resize(5);
363 cout <<
"Received b=" << bytes_received <<
": " << error.message() <<
" (" << error <<
")" << endl;
364 cout <<
"Hex:" << Converter::GetHex<uint16_t>(&fBufCommand[0], bytes_received) << endl;
375 const uint16_t addr = ntohs(fBufCommand[0]);
376 const uint16_t val =
reinterpret_cast<uint16_t*
>(&fStaticData)[addr];
378 cout <<
"-> GET REGISTER[" << addr <<
"]=" << val << endl;
384 fBufHeader = fHeader.
HtoN();
385 fBufStaticData[addr] = htons(val);
387 AsyncWrite(
ba::buffer(ba::const_buffer(&fBufHeader[0], fBufHeader.size()*2)));
388 AsyncWrite(
ba::buffer(ba::const_buffer(&fBufStaticData[addr], 2)));
389 AsyncWrite(
ba::buffer(ba::const_buffer(&fDelimiter, 2)));
398 const uint16_t addr = ntohs(fBufCommand[0]);
399 const uint16_t val = ntohs(fBufCommand[1]);
401 cout <<
"-> SET REGISTER[" << addr <<
"]=" << val << endl;
403 reinterpret_cast<uint16_t*
>(&fStaticData)[addr] = val;
409 cout <<
"-> SET STATIC DATA" << endl;
410 fStaticData = fBufCommand;
419 fBufCommand.resize(5);
432 if (ec==ba::error::basic_errors::operation_aborted)
440 if (fTriggerDynData.expires_at() > ba::deadline_timer::traits_type::now())
458 if (ec==ba::error::basic_errors::operation_aborted)
465 if (fTriggerSendData.expires_at() > ba::deadline_timer::traits_type::now())
472 const uint16_t
time = 100*float(rand())/RAND_MAX+50;
480 static shared_ptr
create(ba::io_service& io_service)
490 fBufCommand.resize(5);
506 tcp::acceptor(ioservice, tcp::endpoint(tcp::v4(), port))
520 cout <<
"Start accept..." << flush;
524 async_accept(*new_connection,
528 ba::placeholders::error));
530 cout <<
"start-done." << endl;
538 cout <<
"Handle accept..." << flush;
541 new_connection->start();
548 cout <<
"handle-done." << endl;
556 const string n = conf.
GetName()+
".log";
558 po::options_description config(
"Program options");
560 (
"dns", var<string>(
"localhost"),
"Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
561 (
"port,p", var<uint16_t>(5000),
"")
564 po::positional_options_description p;
568 conf.
AddEnv(
"dns",
"DIM_DNS_NODE");
574 int main(
int argc,
const char **argv)
580 po::variables_map vm;
583 vm = conf.
Parse(argc, argv);
585 #if BOOST_VERSION > 104000 586 catch (po::multiple_occurrences &e)
588 cerr <<
"Program options invalid due to: " << e.what() <<
" of '" << e.get_option_name() <<
"'." << endl;
594 cerr <<
"Program options invalid due to: " << e.what() << endl;
605 ba::io_service io_service;
607 Port = conf.
Get<uint16_t>(
"port");
Enable the trigger output.
void TriggerSendData(const boost::system::error_code &ec)
std::vector< uint16_t > HtoN() const
void AsyncWait(ba::deadline_timer &timer, int seconds, void(tcp_connection::*handler)(const bs::error_code &))
Adds some functionality to boost::posix_time::ptime for our needs.
T Get(const std::string &var)
void AsyncRead(ba::mutable_buffers_1 buffers)
void HandleReceivedData(const boost::system::error_code &error, size_t bytes_received)
tcp_connection(ba::io_service &ioservice)
void AddEnv(const std::string &conf, const std::string &env)
Static (configuration) data.
void SetArgumentPositions(const po::positional_options_description &desc)
void Setup(const std::string &dns="", const std::string &host="")
FTM::StaticData fStaticData
void handle_accept(tcp_connection::shared_ptr new_connection, const boost::system::error_code &error)
std::vector< uint16_t > HtoN() const
Specifies that dynamic data is read/written.
void HandleSentData(const boost::system::error_code &error, size_t bytes_transferred)
boost::shared_ptr< tcp_connection > shared_ptr
Specifies that a register is read/written.
void AsyncWrite(const ba::const_buffers_1 &buffers)
bool SendCommand(const std::string &command)
Specifies that static (configuration) data is read/written.
vector< uint16_t > fBufHeader
void AddOptions(const po::options_description &opt, bool visible=true)
vector< uint16_t > fBufDynamicData
FTM::DynamicData fDynamicData
int main(int argc, const char **argv)
std::vector< uint16_t > HtoN() const
ba::deadline_timer fTriggerDynData
tcp_server(ba::io_service &ioservice, int port)
Disable the trigger output.
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Enable trigger decision after light pulse (CalibrationTrigger, LP1)
static shared_ptr create(ba::io_service &io_service)
void SetupConfiguration(::Configuration &conf)
Commandline parsing, resource file parsing and database access.
Disable transmission of rate-reports (dynamic data)
Pedestal trigger (artifical)
FTU list (answer of ping)
bool IsEnabled(GeneralSettings type) const
A requested register value.
static void handler(int conn_id, char *packet, int size, int status)
vector< uint16_t > fBufFtuList
Trigger output disabled, configuration possible.
void SendDynData(const boost::system::error_code &ec)
Configure single FTU board.
const po::variables_map & Parse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
uint16_t fActiveFTU[4]
Num of board responded in crate 0-3.
Ping all FTUs (get FTU list)
void HandleSentData(const boost::system::error_code &, size_t)
Physics trigger decision (PhysicTrigger)
Trigger output enabled, configuration ignored.
vector< uint16_t > fBufStaticData
static shared_ptr create(ba::io_service &io_service, int boardid)
const std::string & GetName() const