source: trunk/FACT++/src/fadctrl.cc@ 11154

Last change on this file since 11154 was 11139, checked in by tbretz, 14 years ago
Added option to set maximum event buffer memory.
File size: 57.1 KB
Line 
1#include <boost/bind.hpp>
2#include <boost/array.hpp>
3#if BOOST_VERSION < 104400
4#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
5#undef BOOST_HAS_RVALUE_REFS
6#endif
7#endif
8#include <boost/thread.hpp>
9//#include <boost/foreach.hpp>
10#include <boost/asio/error.hpp>
11#include <boost/asio/deadline_timer.hpp>
12#include <boost/date_time/posix_time/posix_time_types.hpp>
13
14#include "Dim.h"
15#include "Event.h"
16#include "Shell.h"
17#include "StateMachineDim.h"
18#include "Connection.h"
19#include "Configuration.h"
20#include "Console.h"
21#include "Converter.h"
22#include "LocalControl.h"
23#include "HeadersFAD.h"
24
25#include "tools.h"
26
27namespace ba = boost::asio;
28namespace bs = boost::system;
29
30using ba::ip::tcp;
31
32using namespace std;
33
34// ------------------------------------------------------------------------
35
36class ConnectionFAD : public Connection
37{
38 vector<uint16_t> fBuffer;
39
40protected:
41 FAD::EventHeader fEventHeader;
42 FAD::ChannelHeader fChannelHeader[FAD::kNumChannels];
43
44private:
45 bool fIsVerbose;
46 bool fIsHexOutput;
47 bool fIsDataOutput;
48 bool fBlockTransmission;
49
50 uint64_t fCounter;
51
52protected:
53 void PrintEventHeader()
54 {
55 Out() << endl << kBold << "Header received (N=" << dec << fCounter << "):" << endl;
56 Out() << fEventHeader;
57 if (fIsHexOutput)
58 Out() << Converter::GetHex<uint16_t>(fEventHeader, 16) << endl;
59 }
60
61 void PrintChannelHeaders()
62 {
63 Out() << dec << endl;
64
65 for (unsigned int c=0; c<FAD::kNumChips; c++)
66 {
67 Out() << "ROI|" << fEventHeader.Crate() << ":" << fEventHeader.Board() << ":" << c << ":";
68 for (unsigned int ch=0; ch<FAD::kNumChannelsPerChip; ch++)
69 Out() << " " << setw(4) << fChannelHeader[c+ch*FAD::kNumChips].fRegionOfInterest;
70 Out() << endl;
71 }
72
73 Out() << "CEL|" << fEventHeader.Crate() << ":" <<fEventHeader.Board() << ": ";
74 for (unsigned int c=0; c<FAD::kNumChips; c++)
75 {
76 if (0)//fIsFullChannelHeader)
77 {
78 for (unsigned int ch=0; ch<FAD::kNumChannelsPerChip; ch++)
79 Out() << " " << setw(4) << fChannelHeader[c+ch*FAD::kNumChips].fStartCell;
80 Out() << endl;
81 }
82 else
83 {
84 Out() << " ";
85 const uint16_t cel = fChannelHeader[c*FAD::kNumChannelsPerChip].fStartCell;
86 for (unsigned int ch=1; ch<FAD::kNumChannelsPerChip; ch++)
87 if (cel!=fChannelHeader[c+ch*FAD::kNumChips].fStartCell)
88 {
89 Out() << "!";
90 break;
91 }
92 Out() << cel;
93 }
94 }
95 Out() << endl;
96
97 if (fIsHexOutput)
98 Out() << Converter::GetHex<uint16_t>(fChannelHeader, 16) << endl;
99
100 }
101
102 virtual void UpdateFirstHeader()
103 {
104 }
105
106 virtual void UpdateEventHeader()
107 {
108 // emit service with trigger counter from header
109 if (fIsVerbose)
110 PrintEventHeader();
111 }
112
113 virtual void UpdateChannelHeaders()
114 {
115 // emit service with trigger counter from header
116 if (fIsVerbose)
117 PrintChannelHeaders();
118
119 }
120
121 virtual void UpdateData(const uint16_t *data, size_t sz)
122 {
123 // emit service with trigger counter from header
124 if (fIsVerbose && fIsDataOutput)
125 Out() << Converter::GetHex<uint16_t>(data, sz, 16, true) << endl;
126 }
127
128private:
129 enum
130 {
131 kReadHeader = 1,
132 kReadData = 2,
133 };
134
135 void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int type)
136 {
137 // Do not schedule a new read if the connection failed.
138 if (bytes_received==0 || err)
139 {
140 if (err==ba::error::eof)
141 Warn("Connection closed by remote host (FAD).");
142
143 // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
144 // 125: Operation canceled
145 if (err && err!=ba::error::eof && // Connection closed by remote host
146 err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
147 err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
148 {
149 ostringstream str;
150 str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
151 Error(str);
152 }
153 PostClose(err!=ba::error::basic_errors::operation_aborted);
154 return;
155 }
156
157 if (type==kReadHeader)
158 {
159 if (bytes_received!=sizeof(FAD::EventHeader))
160 {
161 ostringstream str;
162 str << "Bytes received (" << bytes_received << " don't match header size " << sizeof(FAD::EventHeader);
163 Error(str);
164 PostClose(false);
165 return;
166 }
167
168 fEventHeader = fBuffer;
169
170 if (fEventHeader.fStartDelimiter!=FAD::kDelimiterStart)
171 {
172 ostringstream str;
173 str << "Invalid header received: start delimiter wrong, received ";
174 str << hex << fEventHeader.fStartDelimiter << ", expected " << FAD::kDelimiterStart << ".";
175 Error(str);
176 PostClose(false);
177 return;
178 }
179
180 if (fCounter==0)
181 UpdateFirstHeader();
182
183 UpdateEventHeader();
184
185 fBuffer.resize(fEventHeader.fPackageLength-sizeof(FAD::EventHeader)/2);
186 AsyncRead(ba::buffer(fBuffer), kReadData);
187 AsyncWait(fInTimeout, 2000, &Connection::HandleReadTimeout);
188
189 return;
190 }
191
192 fInTimeout.cancel();
193
194 if (ntohs(fBuffer.back())!=FAD::kDelimiterEnd)
195 {
196 ostringstream str;
197 str << "Invalid data received: end delimiter wrong, received ";
198 str << hex << ntohs(fBuffer.back()) << ", expected " << FAD::kDelimiterEnd << ".";
199 Error(str);
200 PostClose(false);
201 return;
202 }
203
204 uint8_t *ptr = reinterpret_cast<uint8_t*>(fBuffer.data());
205 uint8_t *end = ptr + fBuffer.size()*2;
206 for (unsigned int i=0; i<FAD::kNumChannels; i++)
207 {
208 if (ptr+sizeof(FAD::ChannelHeader) > end)
209 {
210 Error("Channel header exceeds buffer size.");
211 PostClose(false);
212 return;
213 }
214
215 fChannelHeader[i] = vector<uint16_t>((uint16_t*)ptr, (uint16_t*)ptr+sizeof(FAD::ChannelHeader)/2);
216 ptr += sizeof(FAD::ChannelHeader);
217
218 //UpdateChannelHeader(i);
219
220 if (ptr+fChannelHeader[i].fRegionOfInterest*2 > end)
221 {
222 Error("Data block exceeds buffer size.");
223 PostClose(false);
224 return;
225 }
226
227 uint16_t *data = reinterpret_cast<uint16_t*>(ptr);
228
229 /*
230 for (uint16_t *d=data; d<data+fChannelHeader[i].fRegionOfInterest; d++)
231 {
232 const bool sign = *d & 0x2000;
233 const bool overflow = *d & 0x1000;
234
235 if (sign)
236 *d |= 0xf000; // no overflow, nagative
237 else
238 *d &= 0x07ff; // no overlow, positive
239
240 // max = [-2047;2048]
241
242 if (overflow)
243 {
244 if (sign)
245 *d = 0xF800; // overflow, negative
246 else
247 *d = 0x0800; // overflow, positive
248 }
249 }
250 */
251
252 UpdateData(data, fChannelHeader[i].fRegionOfInterest*2);
253 ptr += fChannelHeader[i].fRegionOfInterest*2;
254 }
255
256 if (fIsVerbose)
257 UpdateChannelHeaders();
258
259 fCounter++;
260
261 fBuffer.resize(sizeof(FAD::EventHeader)/2);
262 AsyncRead(ba::buffer(fBuffer), kReadHeader);
263 }
264
265 void HandleReadTimeout(const bs::error_code &error)
266 {
267 if (error==ba::error::basic_errors::operation_aborted)
268 return;
269
270 if (error)
271 {
272 ostringstream str;
273 str << "Read timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
274 Error(str);
275
276 PostClose();
277 return;
278
279 }
280
281 if (!is_open())
282 {
283 // For example: Here we could schedule a new accept if we
284 // would not want to allow two connections at the same time.
285 return;
286 }
287
288 // Check whether the deadline has passed. We compare the deadline
289 // against the current time since a new asynchronous operation
290 // may have moved the deadline before this actor had a chance
291 // to run.
292 if (fInTimeout.expires_at() > ba::deadline_timer::traits_type::now())
293 return;
294
295 Error("Timeout reading data from "+URL());
296 PostClose();
297 }
298
299 // This is called when a connection was established
300 void ConnectionEstablished()
301 {
302 fEventHeader.clear();
303 for (unsigned int i=0; i<FAD::kNumChannels; i++)
304 fChannelHeader[i].clear();
305
306 fCounter = 0;
307
308 fBuffer.resize(sizeof(FAD::EventHeader)/2);
309 AsyncRead(ba::buffer(fBuffer), kReadHeader);
310
311// for (int i=0; i<36; i++)
312// CmdSetRoi(i, 100);
313
314// Cmd(FAD::kCmdTriggerLine, true);
315// Cmd(FAD::kCmdSingleTrigger);
316 }
317
318public:
319 void PostCmd(std::vector<uint16_t> cmd)
320 {
321 if (fBlockTransmission || !IsConnected())
322 return;
323
324#ifdef DEBUG_TX
325 ostringstream msg;
326 msg << "Sending command:" << hex;
327 msg << " 0x" << setw(4) << setfill('0') << cmd[0];
328 msg << " (+ " << cmd.size()-1 << " bytes data)";
329 Message(msg);
330#endif
331 transform(cmd.begin(), cmd.end(), cmd.begin(), htons);
332
333 PostMessage(cmd);
334 }
335
336 void PostCmd(uint16_t cmd)
337 {
338 if (fBlockTransmission || !IsConnected())
339 return;
340
341#ifdef DEBUG_TX
342 ostringstream msg;
343 msg << "Sending command:" << hex;
344 msg << " 0x" << setw(4) << setfill('0') << cmd;
345 Message(msg);
346#endif
347 cmd = htons(cmd);
348 PostMessage(&cmd, sizeof(uint16_t));
349 }
350
351 void PostCmd(uint16_t cmd, uint16_t data)
352 {
353 if (fBlockTransmission || !IsConnected())
354 return;
355
356#ifdef DEBUG_TX
357 ostringstream msg;
358 msg << "Sending command:" << hex;
359 msg << " 0x" << setw(4) << setfill('0') << cmd;
360 msg << " 0x" << setw(4) << setfill('0') << data;
361 Message(msg);
362#endif
363 const uint16_t d[2] = { htons(cmd), htons(data) };
364 PostMessage(d, sizeof(d));
365 }
366
367public:
368 ConnectionFAD(ba::io_service& ioservice, MessageImp &imp) :
369 Connection(ioservice, imp()),
370 fIsVerbose(false), fIsHexOutput(false), fIsDataOutput(false),
371 fBlockTransmission(false), fCounter(0)
372 {
373 // Maximum possible needed space:
374 // The full header, all channels with all DRS bins
375 // Two trailing shorts
376 fBuffer.reserve(sizeof(FAD::EventHeader) + FAD::kNumChannels*(sizeof(FAD::ChannelHeader) + FAD::kMaxBins*sizeof(uint16_t)) + 2*sizeof(uint16_t));
377
378 SetLogStream(&imp);
379 }
380
381 void Cmd(FAD::Enable cmd, bool on=true)
382 {
383 PostCmd(cmd + (on ? 0 : 0x100));
384 }
385
386 // ------------------------------
387
388 // IMPLEMENT: Abs/Rel
389 void CmdPhaseShift(int16_t val)
390 {
391 vector<uint16_t> cmd(abs(val)+2, FAD::kCmdPhaseApply);
392 cmd[0] = FAD::kCmdPhaseReset;
393 cmd[1] = val<0 ? FAD::kCmdPhaseDecrease : FAD::kCmdPhaseIncrease;
394 PostCmd(cmd);
395 }
396
397 bool CmdSetTriggerRate(int32_t val)
398 {
399 if (val<0 || val>0xffff)
400 return false;
401
402 PostCmd(FAD::kCmdWriteRate, val);//uint8_t(1000./val/12.5));
403 //PostCmd(FAD::kCmdWriteExecute);
404
405 return true;
406 }
407
408 void CmdSetRunNumber(uint32_t num)
409 {
410 PostCmd(FAD::kCmdWriteRunNumberLSW, num&0xffff);
411 PostCmd(FAD::kCmdWriteRunNumberMSW, num>>16);
412 PostCmd(FAD::kCmdWriteExecute);
413 }
414
415 void CmdSetRegister(uint8_t addr, uint16_t val)
416 {
417 // Allowed addr: [0, MAX_ADDR]
418 // Allowed value: [0, MAX_VAL]
419 PostCmd(FAD::kCmdWrite + addr, val);
420 PostCmd(FAD::kCmdWriteExecute);
421 }
422
423 bool CmdSetDacValue(uint8_t addr, uint16_t val)
424 {
425 if (addr>FAD::kMaxDacAddr) // NDAC
426 return false;
427
428 PostCmd(FAD::kCmdWriteDac + addr, val);
429 PostCmd(FAD::kCmdWriteExecute);
430 return true;
431 }
432
433 bool CmdSetRoi(int8_t addr, uint16_t val)
434 {
435 if (val>FAD::kMaxRoiValue)
436 return false;
437
438 if (addr<0)
439 {
440 for (unsigned int i=0; i<=FAD::kMaxRoiAddr; i++)
441 PostCmd(FAD::kCmdWriteRoi + i, val);
442 PostCmd(FAD::kCmdWriteExecute);
443 return true;
444 }
445
446 if (uint8_t(addr)>FAD::kMaxRoiAddr)
447 return false;
448
449 PostCmd(FAD::kCmdWriteRoi + addr, val);
450 PostCmd(FAD::kCmdWriteExecute);
451 return true;
452 }
453
454 bool CmdSetRoi(uint16_t val) { return CmdSetRoi(-1, val); }
455
456 void AmplitudeCalibration()
457 {
458 // ------------- case baseline -----------------
459
460 CmdSetRoi(-1, FAD::kMaxBins);
461
462 CmdSetDacValue(1, 0);
463 CmdSetDacValue(2, 0);
464 CmdSetDacValue(3, 0);
465
466 // Take N events
467
468 /*
469 // ====== Part B: Baseline calibration =====
470
471 // Loop over all channels(ch) and time-slices (t)
472 T0 = TriggerCell[chip]
473 Sum[ch][(t+T0) % kMaxBins] += Data[ch][t];
474 // FIXME: Determine median instead of average
475
476 Baseline[ch][slice] = MEDIAN( sum[ch][slice] )
477 */
478
479 // --------------- case gain -------------------
480
481 // Set new DAC values and start accumulation
482 CmdSetDacValue(1, 50000);
483 CmdSetDacValue(2, 50000);
484 CmdSetDacValue(3, 50000);
485
486 // Take N events
487
488 /*
489 // ====== Part C: Gain calibration =====
490
491 T0 = TriggerCell[chip]
492 Sum[ch][(t+T0) % kMaxBins] += Data[ch][t];
493 // FIXME: Determine median instead of average
494
495 Gain[ch][slice] = MEDIAN( sum[ch][slice] ) - Baseline[ch][slice]
496 */
497
498 // --------------- secondary ------------------
499
500 // FIXME: Can most probably be done together with the baseline calibration
501 // FIXME: Why does the secondary baseline not influence the baseline?
502
503 CmdSetDacValue(1, 0);
504 CmdSetDacValue(2, 0);
505 CmdSetDacValue(3, 0);
506
507 // Take N events
508
509 /*
510 // ====== Part D: Secondary calibration =====
511
512 T0 = TriggerCell[chip]
513 Sum[ch][t] = Data[ch][t] - Baseline[ch][(i-T0) % kMaxBins];
514
515 // Determine secondary baseline if integration finished
516 SecondaryBaseline[ch][t] = MEDIAN( Sum[ch][t] )
517 */
518 }
519
520 void SetVerbose(bool b)
521 {
522 fIsVerbose = b;
523 }
524
525 void SetHexOutput(bool b)
526 {
527 fIsHexOutput = b;
528 }
529
530 void SetDataOutput(bool b)
531 {
532 fIsDataOutput = b;
533 }
534
535 void SetBlockTransmission(bool b)
536 {
537 fBlockTransmission = b;
538 }
539
540 bool IsTransmissionBlocked() const
541 {
542 return fBlockTransmission;
543 }
544
545 void PrintEvent()
546 {
547 if (fCounter>0)
548 {
549 PrintEventHeader();
550 PrintChannelHeaders();
551 }
552 else
553 Out() << "No event received yet." << endl;
554 }
555
556};
557
558// ------------------------------------------------------------------------
559#include "DimDescriptionService.h"
560#include "EventBuilderWrapper.h"
561
562// ------------------------------------------------------------------------
563
564template <class T>
565class StateMachineFAD : public T, public EventBuilderWrapper, public ba::io_service, public ba::io_service::work
566{
567private:
568 typedef pair<tcp::endpoint, ConnectionFAD*> Connection;
569 typedef map<uint8_t, Connection> BoardList;
570
571 BoardList fBoards;
572
573 bool fIsVerbose;
574 bool fIsHexOutput;
575 bool fIsDataOutput;
576 bool fDebugTx;
577
578 bool CheckEventSize(size_t has, const char *name, size_t size)
579 {
580 if (has==size)
581 return true;
582
583 ostringstream msg;
584 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
585 T::Fatal(msg);
586 return false;
587 }
588
589 int Cmd(FAD::Enable command)
590 {
591 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
592 i->second.second->Cmd(command);
593
594 return T::GetCurrentState();
595 }
596
597 int SendCmd(const EventImp &evt)
598 {
599 if (!CheckEventSize(evt.GetSize(), "SendCmd", 4))
600 return T::kSM_FatalError;
601
602 if (evt.GetUInt()>0xffff)
603 {
604 T::Warn("Command value out of range (0-65535).");
605 return T::GetCurrentState();
606 }
607
608 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
609 i->second.second->PostCmd(evt.GetUInt());
610
611 return T::GetCurrentState();
612 }
613
614 int SendCmdData(const EventImp &evt)
615 {
616 if (!CheckEventSize(evt.GetSize(), "SendCmdData", 8))
617 return T::kSM_FatalError;
618
619 const uint32_t *ptr = evt.Ptr<uint32_t>();
620
621 if (ptr[0]>0xffff)
622 {
623 T::Warn("Command value out of range (0-65535).");
624 return T::GetCurrentState();
625 }
626
627 if (ptr[1]>0xffff)
628 {
629 T::Warn("Data value out of range (0-65535).");
630 return T::GetCurrentState();
631 }
632
633 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
634 i->second.second->PostCmd(ptr[0], ptr[1]);
635
636 return T::GetCurrentState();
637 }
638
639 int CmdEnable(const EventImp &evt, FAD::Enable command)
640 {
641 if (!CheckEventSize(evt.GetSize(), "CmdEnable", 1))
642 return T::kSM_FatalError;
643
644 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
645 i->second.second->Cmd(command, evt.GetBool());
646
647 return T::GetCurrentState();
648 }
649
650 bool Check(const uint32_t *dat, uint32_t maxaddr, uint32_t maxval)
651 {
652 if (dat[0]>maxaddr)
653 {
654 ostringstream msg;
655 msg << hex << "Address " << dat[0] << " out of range, max=" << maxaddr << ".";
656 T::Error(msg);
657 return false;
658 }
659
660 if (dat[1]>maxval)
661 {
662 ostringstream msg;
663 msg << hex << "Value " << dat[1] << " out of range, max=" << maxval << ".";
664 T::Error(msg);
665 return false;
666 }
667
668 return true;
669 }
670
671 int SetRegister(const EventImp &evt)
672 {
673 if (!CheckEventSize(evt.GetSize(), "SetRegister", 8))
674 return T::kSM_FatalError;
675
676 const uint32_t *dat = evt.Ptr<uint32_t>();
677
678 if (!Check(dat, FAD::kMaxRegAddr, FAD::kMaxRegValue))
679 return T::GetCurrentState();
680
681 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
682 i->second.second->CmdSetRegister(dat[0], dat[1]);
683
684 return T::GetCurrentState();
685 }
686
687 int SetRoi(const EventImp &evt)
688 {
689 if (!CheckEventSize(evt.GetSize(), "SetRoi", 8))
690 return T::kSM_FatalError;
691
692 const int32_t *dat = evt.Ptr<int32_t>();
693
694 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
695 if (!i->second.second->CmdSetRoi(dat[0], dat[1]))
696 {
697 ostringstream msg;
698 msg << hex << "Channel " << dat[0] << " or Value " << dat[1] << " out of range.";
699 T::Error(msg);
700 return false;
701 }
702
703
704 return T::GetCurrentState();
705 }
706
707 int SetDac(const EventImp &evt)
708 {
709 if (!CheckEventSize(evt.GetSize(), "SetDac", 8))
710 return T::kSM_FatalError;
711
712 const uint32_t *dat = evt.Ptr<uint32_t>();
713
714 if (!Check(dat, FAD::kMaxDacAddr, FAD::kMaxDacValue))
715 return T::GetCurrentState();
716
717 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
718 i->second.second->CmdSetDacValue(dat[0], dat[1]);
719
720 return T::GetCurrentState();
721 }
722
723 int Trigger(int n)
724 {
725 for (int nn=0; nn<n; nn++)
726 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
727 i->second.second->Cmd(FAD::kCmdSingleTrigger);
728
729 return T::GetCurrentState();
730 }
731
732 int SendTriggers(const EventImp &evt)
733 {
734 if (!CheckEventSize(evt.GetSize(), "SendTriggers", 4))
735 return T::kSM_FatalError;
736
737 Trigger(evt.GetUInt());
738
739 return T::GetCurrentState();
740 }
741
742 int StartRun(const EventImp &evt, bool start)
743 {
744 if (!CheckEventSize(evt.GetSize(), "StartRun", 0))
745 return T::kSM_FatalError;
746
747 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
748 i->second.second->Cmd(FAD::kCmdRun, start);
749
750 return T::GetCurrentState();
751 }
752
753 int PhaseShift(const EventImp &evt)
754 {
755 if (!CheckEventSize(evt.GetSize(), "PhaseShift", 2))
756 return T::kSM_FatalError;
757
758 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
759 i->second.second->CmdPhaseShift(evt.GetShort());
760
761 return T::GetCurrentState();
762 }
763
764 int SetTriggerRate(const EventImp &evt)
765 {
766 if (!CheckEventSize(evt.GetSize(), "SetTriggerRate", 4))
767 return T::kSM_FatalError;
768
769 if (evt.GetUShort()>0xff)
770 {
771 ostringstream msg;
772 msg << hex << "Value " << evt.GetUShort() << " out of range, max=" << 0xff << "(?)";
773 T::Error(msg);
774 return false;
775 }
776
777 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
778 i->second.second->CmdSetTriggerRate(evt.GetUInt());
779
780 return T::GetCurrentState();
781 }
782
783 int SetRunNumber(const EventImp &evt)
784 {
785 if (!CheckEventSize(evt.GetSize(), "SetRunNumber", 8))
786 return T::kSM_FatalError;
787
788 const uint64_t num = evt.GetUXtra();
789
790 if (num>FAD::kMaxRunNumber)
791 {
792 ostringstream msg;
793 msg << hex << "Value " << num << " out of range, max=" << FAD::kMaxRunNumber;
794 T::Error(msg);
795 return false;
796 }
797
798 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
799 i->second.second->CmdSetRunNumber(num);
800
801 return T::GetCurrentState();
802 }
803
804 int SetMaxMemoryBuffer(const EventImp &evt)
805 {
806 if (!CheckEventSize(evt.GetSize(), "SetMaxMemoryBuffer", 2))
807 return T::kSM_FatalError;
808
809 const int16_t mem = evt.GetShort();
810
811 if (mem<=0)
812 {
813 ostringstream msg;
814 msg << hex << "Value " << mem << " out of range.";
815 T::Error(msg);
816 return false;
817 }
818
819 SetMaxMemory(mem);
820
821 return T::GetCurrentState();
822 }
823
824 int Test(const EventImp &evt)
825 {
826 if (!CheckEventSize(evt.GetSize(), "Test", 2))
827 return T::kSM_FatalError;
828
829
830 SetMode(evt.GetShort());
831
832 return T::GetCurrentState();
833 }
834
835
836 int SetVerbosity(const EventImp &evt)
837 {
838 if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
839 return T::kSM_FatalError;
840
841 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
842 i->second.second->SetVerbose(evt.GetBool());
843
844 return T::GetCurrentState();
845 }
846
847 int SetHexOutput(const EventImp &evt)
848 {
849 if (!CheckEventSize(evt.GetSize(), "SetHexOutput", 1))
850 return T::kSM_FatalError;
851
852 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
853 i->second.second->SetHexOutput(evt.GetBool());
854
855 return T::GetCurrentState();
856 }
857
858 int SetDataOutput(const EventImp &evt)
859 {
860 if (!CheckEventSize(evt.GetSize(), "SetDataOutput", 1))
861 return T::kSM_FatalError;
862
863 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
864 i->second.second->SetDataOutput(evt.GetBool());
865
866 return T::GetCurrentState();
867 }
868
869 int SetDebugTx(const EventImp &evt)
870 {
871 if (!CheckEventSize(evt.GetSize(), "SetDebugTx", 1))
872 return T::kSM_FatalError;
873
874 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
875 i->second.second->SetDebugTx(evt.GetBool());
876
877 return T::GetCurrentState();
878 }
879
880 const BoardList::iterator GetSlot(uint16_t slot)
881 {
882 const BoardList::iterator it=fBoards.find(slot);
883 if (it==fBoards.end())
884 {
885 ostringstream str;
886 str << "Slot " << slot << " not found.";
887 T::Warn(str);
888 }
889
890 return it;
891 }
892
893 int PrintEvent(const EventImp &evt)
894 {
895 if (!CheckEventSize(evt.GetSize(), "PrintEvent", 2))
896 return T::kSM_FatalError;
897
898 const int16_t slot = evt.Get<int16_t>();
899
900 if (slot<0)
901 {
902 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
903 i->second.second->PrintEvent();
904 }
905 else
906 {
907 const BoardList::iterator it=GetSlot(slot);
908 if (it!=fBoards.end())
909 it->second.second->PrintEvent();
910 }
911
912 return T::GetCurrentState();
913 }
914
915 int SetBlockTransmission(const EventImp &evt)
916 {
917 if (!CheckEventSize(evt.GetSize(), "SetBlockTransmission", 3))
918 return T::kSM_FatalError;
919
920 const int16_t slot = evt.Get<int32_t>();
921
922 const BoardList::iterator it=GetSlot(slot);
923 if (it!=fBoards.end())
924 it->second.second->SetBlockTransmission(evt.Get<uint8_t>(2));
925
926 return T::GetCurrentState();
927 }
928
929 int SetBlockTransmissionRange(const EventImp &evt)
930 {
931 if (!CheckEventSize(evt.GetSize(), "SetBlockTransmissionRange", 5))
932 return T::kSM_FatalError;
933
934 const int16_t *slot = evt.Ptr<int16_t>();
935 const bool block = evt.Get<uint8_t>(4);
936
937 for (int i=slot[0]; i<=slot[1]; i++)
938 {
939 const BoardList::iterator it=GetSlot(i);
940 if (it!=fBoards.end())
941 it->second.second->SetBlockTransmission(block);
942 }
943
944 return T::GetCurrentState();
945 }
946
947 int SetIgnoreSlot(const EventImp &evt)
948 {
949 if (!CheckEventSize(evt.GetSize(), "SetIgnoreSlot", 3))
950 return T::kSM_FatalError;
951
952 const uint16_t slot = evt.Get<uint16_t>();
953
954 if (slot>39)
955 {
956 T::Warn("Slot out of range (0-39).");
957 return T::GetCurrentState();
958 }
959
960 SetIgnore(slot, evt.Get<uint8_t>(2));
961
962 return T::GetCurrentState();
963 }
964
965 int SetIgnoreSlots(const EventImp &evt)
966 {
967 if (!CheckEventSize(evt.GetSize(), "SetIgnoreSlots", 5))
968 return T::kSM_FatalError;
969
970 const int16_t *slot = evt.Ptr<int16_t>();
971 const bool block = evt.Get<uint8_t>(4);
972
973 if (slot[0]<0 || slot[1]>39 || slot[0]>slot[1])
974 {
975 T::Warn("Slot out of range.");
976 return T::GetCurrentState();
977 }
978
979 for (int i=slot[0]; i<=slot[1]; i++)
980 SetIgnore(i, block);
981
982 return T::GetCurrentState();
983 }
984
985 int SetDumpStream(const EventImp &evt)
986 {
987 if (!CheckEventSize(evt.GetSize(), "SetDumpStream", 1))
988 return T::kSM_FatalError;
989
990 SetDebugStream(evt.Get<uint8_t>());
991
992 return T::GetCurrentState();
993 }
994
995 int SetDumpRecv(const EventImp &evt)
996 {
997 if (!CheckEventSize(evt.GetSize(), "SetDumpRecv", 1))
998 return T::kSM_FatalError;
999
1000 SetDebugRead(evt.Get<uint8_t>());
1001
1002 return T::GetCurrentState();
1003 }
1004
1005 int AddAddress(const EventImp &evt)
1006 {
1007 const string addr = Tools::Trim(evt.GetText());
1008
1009 const tcp::endpoint endpoint = GetEndpoint(addr);
1010 if (endpoint==tcp::endpoint())
1011 return T::GetCurrentState();
1012
1013 for (BoardList::const_iterator i=fBoards.begin(); i!=fBoards.end(); i++)
1014 {
1015 if (i->second.first==endpoint)
1016 {
1017 T::Warn("Address "+addr+" already known.... ignored.");
1018 return T::GetCurrentState();
1019 }
1020 }
1021
1022 AddEndpoint(endpoint);
1023
1024 return T::GetCurrentState();
1025 }
1026
1027 int RemoveSlot(const EventImp &evt)
1028 {
1029 if (!CheckEventSize(evt.GetSize(), "RemoveSlot", 2))
1030 return T::kSM_FatalError;
1031
1032 const int16_t slot = evt.GetShort();
1033
1034 const BoardList::iterator it = GetSlot(slot);
1035
1036 if (it==fBoards.end())
1037 return T::GetCurrentState();
1038
1039 ConnectSlot(slot, tcp::endpoint());
1040
1041 delete it->second.second;
1042 fBoards.erase(it);
1043
1044 return T::GetCurrentState();
1045 }
1046
1047 int ListSlots()
1048 {
1049 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
1050 {
1051 const int &idx = i->first;
1052 const Connection &slot = i->second;
1053
1054 ostringstream str;
1055 str << "Slot " << setw(2) << idx << ": " << slot.first;
1056
1057 const ConnectionFAD *c = slot.second;
1058
1059 if (c->IsConnecting())
1060 str << " (0:connecting, ";
1061 else
1062 {
1063 if (c->IsClosed())
1064 str << " (0:disconnected, ";
1065 if (c->IsConnected())
1066 str << " (0:connected, ";
1067 }
1068
1069 switch (fStatus2[idx])
1070 {
1071 case 0: str << "1-7:not connected)"; break;
1072 case 7: str << "1-7:connected)"; break;
1073 default: str << "1-7:connecting [" << (int)fStatus2[idx] << "])"; break;
1074 }
1075
1076 if (c->IsTransmissionBlocked())
1077 str << " [cmd_blocked]";
1078
1079 if (fStatus2[idx]==7 && IsIgnored(idx))
1080 str << " [data_ignored]";
1081
1082 T::Out() << str.str() << endl;
1083 }
1084
1085 T::Out() << "Event builder thread:";
1086 if (!IsThreadRunning())
1087 T::Out() << " not";
1088 T::Out() << " running" << endl;
1089
1090 // FIXME: Output state
1091
1092 return T::GetCurrentState();
1093 }
1094
1095 void EnableConnection(Connection &c, bool enable=true)
1096 {
1097 ConnectionFAD *ptr = c.second;
1098 if (!ptr)
1099 return;
1100
1101 if (!enable)
1102 {
1103 ptr->PostClose(false);
1104 return;
1105 }
1106
1107 ostringstream str;
1108 str << c.first;
1109
1110 if (!ptr->IsDisconnected())
1111 {
1112 T::Warn("Connection to "+str.str()+" already in progress.");
1113 return;
1114 }
1115
1116 ptr->SetEndpoint(str.str());
1117 ptr->StartConnect();
1118 }
1119
1120 void EnableAll(bool enable=true)
1121 {
1122 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
1123 EnableConnection(i->second, enable);
1124 }
1125
1126 int CloseOpenFiles()
1127 {
1128 EventBuilderWrapper::CloseOpenFiles();
1129 return T::GetCurrentState();
1130 }
1131
1132 int EnableSlot(const EventImp &evt, bool enable)
1133 {
1134 if (!CheckEventSize(evt.GetSize(), "EnableSlot", 2))
1135 return T::kSM_FatalError;
1136
1137 const int16_t slot = evt.GetShort();
1138
1139 const BoardList::iterator it = GetSlot(slot);
1140 if (it==fBoards.end())
1141 return T::GetCurrentState();
1142
1143 EnableConnection(it->second, enable);
1144 ConnectSlot(it->first, enable ? it->second.first : tcp::endpoint());
1145
1146 return T::GetCurrentState();
1147 }
1148
1149 int ToggleSlot(const EventImp &evt)
1150 {
1151 if (!CheckEventSize(evt.GetSize(), "ToggleSlot", 2))
1152 return T::kSM_FatalError;
1153
1154 const int16_t slot = evt.GetShort();
1155
1156 const BoardList::iterator it = GetSlot(slot);
1157 if (it==fBoards.end())
1158 return T::GetCurrentState();
1159
1160 const bool enable = it->second.second->IsDisconnected() && fStatus2[slot]==0;
1161
1162 EnableConnection(it->second, enable);
1163 ConnectSlot(it->first, enable ? it->second.first : tcp::endpoint());
1164
1165 return T::GetCurrentState();
1166 }
1167
1168 int StartConnection()
1169 {
1170 vector<tcp::endpoint> addr(40);
1171
1172 for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
1173 addr[i->first] = i->second.first;
1174
1175 StartThread(addr);
1176 EnableAll(true);
1177
1178 return T::GetCurrentState();
1179 }
1180
1181 int StopConnection()
1182 {
1183 Exit();
1184 EnableAll(false);
1185 return T::GetCurrentState();
1186 }
1187
1188 int AbortConnection()
1189 {
1190 Abort();
1191 EnableAll(false);
1192 return T::GetCurrentState();
1193 }
1194
1195 vector<uint8_t> fStatus1;
1196 vector<uint8_t> fStatus2;
1197 bool fStatusT;
1198
1199 int Execute()
1200 {
1201 // Dispatch (execute) at most one handler from the queue. In contrary
1202 // to run_one(), it doesn't wait until a handler is available
1203 // which can be dispatched, so poll_one() might return with 0
1204 // handlers dispatched. The handlers are always dispatched/executed
1205 // synchronously, i.e. within the call to poll_one()
1206 poll_one();
1207
1208 // ===== Evaluate connection status =====
1209
1210 uint16_t nclosed1 = 0;
1211 uint16_t nconnecting1 = 0;
1212 uint16_t nconnecting2 = 0;
1213 uint16_t nconnected1 = 0;
1214 uint16_t nconnected2 = 0;
1215
1216 vector<uint8_t> stat1(40);
1217 vector<uint8_t> stat2(40);
1218
1219 int cnt = 0; // counter for enabled board
1220
1221 for (int idx=0; idx<40; idx++)
1222 {
1223 // ----- Command socket -----
1224 const BoardList::const_iterator &slot = fBoards.find(idx);
1225 if (slot!=fBoards.end())
1226 {
1227 const ConnectionFAD *c = slot->second.second;
1228 if (c->IsDisconnected())
1229 {
1230 stat1[idx] = 0;
1231 nclosed1++;
1232 }
1233 if (c->IsConnecting())
1234 {
1235 stat1[idx] = 1;
1236 nconnecting1++;
1237 }
1238 if (c->IsConnected())
1239 {
1240 stat1[idx] = 2;
1241 nconnected1++;
1242 }
1243
1244 cnt++;
1245 }
1246
1247 // ----- Event builder -----
1248 stat2[idx] = GetNumConnected(idx);
1249
1250 if (!IsConnected(idx) && !IsDisconnected(idx))
1251 nconnecting2++;
1252
1253 if (IsConnected(idx))
1254 nconnected2++;
1255 }
1256
1257 // ===== Send connection status via dim =====
1258
1259 if (fStatus1!=stat1 || fStatus2!=stat2 || fStatusT!=IsThreadRunning())
1260 {
1261 fStatus1 = stat1;
1262 fStatus2 = stat2;
1263 fStatusT = IsThreadRunning();
1264 UpdateConnectionStatus(stat1, stat2, IsThreadRunning());
1265 }
1266
1267 // ===== Return connection status =====
1268
1269 // fadctrl: Always connecting if not disabled
1270 // event builder:
1271 if (nconnecting1==0 && nconnected1>0 &&
1272 nconnected2==nconnected1)
1273 return FAD::kConnected;
1274
1275 if (nconnecting1>0 || nconnecting2>0 || nconnected1!=nconnected2)
1276 return FAD::kConnecting;
1277
1278 // nconnected1 == nconnected2 == 0
1279 return IsThreadRunning() ? FAD::kDisconnected : FAD::kOffline;
1280 }
1281
1282 void AddEndpoint(const tcp::endpoint &addr)
1283 {
1284 int i=0;
1285 while (i<40)
1286 {
1287 if (fBoards.find(i)==fBoards.end())
1288 break;
1289 i++;
1290 }
1291
1292 if (i==40)
1293 {
1294 T::Warn("Not more than 40 slots allowed.");
1295 return;
1296 }
1297
1298 fBoards[i] = make_pair(addr, new ConnectionFAD(*this, *this));
1299 fBoards[i].second->SetVerbose(fIsVerbose);
1300 fBoards[i].second->SetHexOutput(fIsHexOutput);
1301 fBoards[i].second->SetDataOutput(fIsDataOutput);
1302 fBoards[i].second->SetDebugTx(fDebugTx);
1303 }
1304
1305
1306 DimDescribedService fDimConnection;
1307 /*
1308 template<class T>
1309 void Update(DimDescribedService &svc, const T &data) const
1310 {
1311 //cout << "Update: " << svc.getName() << " (" << sizeof(T) << ")" << endl;
1312 svc.setData(const_cast<T*>(&data), sizeof(T));
1313 svc.updateService();
1314 }
1315 */
1316 void UpdateConnectionStatus(const vector<uint8_t> &stat1, const vector<uint8_t> &stat2, bool thread)
1317 {
1318 vector<uint8_t> stat(41);
1319
1320 for (int i=0; i<40; i++)
1321 stat[i] = stat1[i]+stat2[i];
1322
1323 stat[40] = thread;
1324
1325 fDimConnection.setData(stat.data(), 41);
1326 fDimConnection.updateService();
1327 }
1328
1329public:
1330 StateMachineFAD(ostream &out=cout) :
1331 T(out, "FAD_CONTROL"), EventBuilderWrapper(*static_cast<MessageImp*>(this)), ba::io_service::work(static_cast<ba::io_service&>(*this)),
1332 fStatus1(40), fStatus2(40), fStatusT(false),
1333 fDimConnection("FAD_CONTROL/CONNECTIONS", "C:40;C:1", "")
1334 {
1335 // ba::io_service::work is a kind of keep_alive for the loop.
1336 // It prevents the io_service to go to stopped state, which
1337 // would prevent any consecutive calls to run()
1338 // or poll() to do nothing. reset() could also revoke to the
1339 // previous state but this might introduce some overhead of
1340 // deletion and creation of threads and more.
1341
1342 // State names
1343 T::AddStateName(FAD::kOffline, "Disengaged",
1344 "All enabled FAD boards are disconnected and the event-builer thread is not running.");
1345
1346 T::AddStateName(FAD::kDisconnected, "Disconnected",
1347 "All enabled FAD boards are disconnected, but the event-builder thread is running.");
1348
1349 T::AddStateName(FAD::kConnecting, "Connecting",
1350 "Only some enabled FAD boards are connected.");
1351
1352 T::AddStateName(FAD::kConnected, "Connected",
1353 "All enabled FAD boards are connected..");
1354
1355 // FAD Commands
1356 T::AddEvent("SEND_CMD", "I:1")
1357 (boost::bind(&StateMachineFAD::SendCmd, this, _1))
1358 ("Send a command to the FADs. Values between 0 and 0xffff are allowed."
1359 "|command[uint16]:Command to be transmittted.");
1360 T::AddEvent("SEND_DATA", "I:2")
1361 (boost::bind(&StateMachineFAD::SendCmdData, this, _1))
1362 ("Send a command with data to the FADs. Values between 0 and 0xffff are allowed."
1363 "|command[uint16]:Command to be transmittted."
1364 "|data[uint16]:Data to be sent with the command.");
1365
1366 T::AddEvent("ENABLE_SRCLK", "B:1")
1367 (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdSrclk))
1368 ("Set SRCLK");
1369 T::AddEvent("ENABLE_SCLK", "B:1")
1370 (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdSclk))
1371 ("Set SCLK");
1372 T::AddEvent("ENABLE_DRS", "B:1")
1373 (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdDrsEnable))
1374 ("Switch Domino wave");
1375 T::AddEvent("ENABLE_DWRITE", "B:1")
1376 (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdDwrite))
1377 ("Set Dwrite (possibly high / always low)");
1378 T::AddEvent("SET_DEBUG_MODE", "B:1")
1379 (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdSocket))
1380 ("Set debug mode (yes: dump events through command socket, no=dump events through other sockets)");
1381 T::AddEvent("ENABLE_TRIGGER_LINE", "B:1")
1382 (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdTriggerLine))
1383 ("Incoming triggers can be accepted/will not be accepted");
1384 T::AddEvent("SET_TRIGGER_RATE", "I:1")
1385 (boost::bind(&StateMachineFAD::SetTriggerRate, this, _1))
1386 ("Enable continous trigger");
1387 T::AddEvent("SEND_SINGLE_TRIGGER")
1388 (boost::bind(&StateMachineFAD::Trigger, this, 1))
1389 ("Issue software triggers");
1390 T::AddEvent("SEND_N_TRIGGERS", "I")
1391 (boost::bind(&StateMachineFAD::SendTriggers, this, _1))
1392 ("Issue software triggers");
1393 T::AddEvent("START_RUN", "")
1394 (boost::bind(&StateMachineFAD::StartRun, this, _1, true))
1395 ("Set FAD DAQ mode. when started, no configurations must be send.");
1396 T::AddEvent("STOP_RUN")
1397 (boost::bind(&StateMachineFAD::StartRun, this, _1, false))
1398 ("");
1399 T::AddEvent("PHASE_SHIFT", "S:1")
1400 (boost::bind(&StateMachineFAD::PhaseShift, this, _1))
1401 ("Adjust ADC phase (in 'steps')");
1402
1403 T::AddEvent("CONTINOUS_TRIGGER_ON")
1404 (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdContTriggerOn))
1405 ("");
1406 T::AddEvent("CONTINOUS_TRIGGER_OFF")
1407 (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdContTriggerOff))
1408 ("");
1409
1410 T::AddEvent("RESET_TRIGGER_ID")
1411 (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdResetTriggerId))
1412 ("");
1413
1414 T::AddEvent("SET_RUN_NUMBER", "X:1")
1415 (boost::bind(&StateMachineFAD::SetRunNumber, this, _1))
1416 ("");
1417
1418 T::AddEvent("SET_MAX_MEMORY", "S:1")
1419 (boost::bind(&StateMachineFAD::SetMaxMemoryBuffer, this, _1))
1420 ("Set maximum memory buffer size allowed to be consumed by the EventBuilder to buffer events."
1421 "|memory[short]:Buffer size in Mega-bytes.");
1422
1423 T::AddEvent("SET_REGISTER", "I:2")
1424 (boost::bind(&StateMachineFAD::SetRegister, this, _1))
1425 ("set register to value"
1426 "|addr[short]:Address of register"
1427 "|val[short]:Value to be set");
1428
1429 // FIXME: Maybe add a mask which channels should be set?
1430 T::AddEvent("SET_REGION_OF_INTEREST", "I:2")
1431 (boost::bind(&StateMachineFAD::SetRoi, this, _1))
1432 ("Set region-of-interest to value"
1433 "|addr[short]:Address of register"
1434 "|val[short]:Value to be set");
1435
1436 // FIXME: Maybe add a mask which channels should be set?
1437 T::AddEvent("SET_DAC_VALUE", "I:2")
1438 (boost::bind(&StateMachineFAD::SetDac, this, _1))
1439 ("Set DAC numbers in range to value"
1440 "|addr[short]:Address of register"
1441 "|val[short]:Value to be set");
1442
1443 // Verbosity commands
1444 T::AddEvent("SET_VERBOSE", "B:1")
1445 (boost::bind(&StateMachineFAD::SetVerbosity, this, _1))
1446 ("Set verbosity state"
1447 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
1448
1449 T::AddEvent("SET_HEX_OUTPUT", "B:1")
1450 (boost::bind(&StateMachineFAD::SetHexOutput, this, _1))
1451 ("Enable or disable hex output for received data"
1452 "|hexout[bool]:disable or enable hex output for received data (yes/no)");
1453
1454 T::AddEvent("SET_DATA_OUTPUT", "B:1")
1455 (boost::bind(&StateMachineFAD::SetDataOutput, this, _1))
1456 ("");
1457
1458 T::AddEvent("SET_DEBUG_TX", "B:1")
1459 (boost::bind(&StateMachineFAD::SetDebugTx, this, _1))
1460 ("Enable or disable the output of messages in case of successfull data transmission to the boards."
1461 "|debug[bool]:disable or enable debug output for transmitted data (yes/no)");
1462
1463 T::AddEvent("PRINT_EVENT", "S:1")
1464 (boost::bind(&StateMachineFAD::PrintEvent, this, _1))
1465 ("Print (last) event"
1466 "|board[short]:slot from which the event should be printed (-1 for all)");
1467
1468 T::AddEvent("DUMP_STREAM", "B:1")
1469 (boost::bind(&StateMachineFAD::SetDumpStream, this, _1))
1470 ("For debugging purpose: the binary data stream read from the sockets 0-7 can be dumped to files."
1471 "|switch[bool]:Enable (yes) or disable (no)");
1472
1473 T::AddEvent("DUMP_RECV", "B:1")
1474 (boost::bind(&StateMachineFAD::SetDumpRecv, this, _1))
1475 ("For debugging purpose: the times when data has been receives are dumped to a file."
1476 "|switch[bool]:Enable (yes) or disable (no)");
1477
1478 T::AddEvent("BLOCK_TRANSMISSION", "S:1;B:1")
1479 (boost::bind(&StateMachineFAD::SetBlockTransmission, this, _1))
1480 ("Blocks the transmission of commands to the given slot. Use with care! For debugging pupose only!"
1481 "|slot[short]:Slot to which the command transmission should be blocked (0-39)"
1482 "|enable[bool]:Whether the command transmission should be blockes (yes) or allowed (no)");
1483
1484 T::AddEvent("BLOCK_TRANSMISSION_RANGE", "S:2;B:1")
1485 (boost::bind(&StateMachineFAD::SetBlockTransmissionRange, this, _1))
1486 ("Blocks the transmission of commands to the given range of slots. Use with care! For debugging pupose only!"
1487 "|first[short]:First slot to which the command transmission should be blocked (0-39)"
1488 "|last[short]:Last slot to which the command transmission should be blocked (0-39)"
1489 "|enable[bool]:Whether the command transmission should be blockes (yes) or allowed (no)");
1490
1491 T::AddEvent("IGNORE_EVENTS", "S:1;B:1")
1492 (boost::bind(&StateMachineFAD::SetIgnoreSlot, this, _1))
1493 ("Instructs the event-builder to ignore events from the given slot but still read the data from the socket."
1494 "|slot[short]:Slot from which the data should be ignored when building events"
1495 "|enable[bool]:Whether the event builder should ignore data from this slot (yes) or allowed (no)");
1496
1497 T::AddEvent("IGNORE_EVENTS_RANGE", "S:2;B:1")
1498 (boost::bind(&StateMachineFAD::SetIgnoreSlots, this, _1))
1499 ("Instructs the event-builder to ignore events from the given slot but still read the data from the socket."
1500 "|first[short]:First slot from which the data should be ignored when building events"
1501 "|last[short]:Last slot from which the data should be ignored when building events"
1502 "|enable[bool]:Whether the event builder should ignore data from this slot (yes) or allowed (no)");
1503
1504 // Conenction commands
1505 T::AddEvent("START", FAD::kOffline)
1506 (boost::bind(&StateMachineFAD::StartConnection, this))
1507 ("Start EventBuilder thread and connect all valid slots.");
1508
1509 T::AddEvent("STOP", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
1510 (boost::bind(&StateMachineFAD::StopConnection, this))
1511 ("Stop EventBuilder thread (still write buffered events) and disconnect all slots.");
1512
1513 T::AddEvent("ABORT", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
1514 (boost::bind(&StateMachineFAD::AbortConnection, this))
1515 ("Immediately abort EventBuilder thread and disconnect all slots.");
1516
1517 T::AddEvent("CONNECT", "S:1", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
1518 (boost::bind(&StateMachineFAD::EnableSlot, this, _1, true))
1519 ("Connect a disconnected slot.");
1520
1521 T::AddEvent("DISCONNECT", "S:1", FAD::kConnecting, FAD::kConnected)
1522 (boost::bind(&StateMachineFAD::EnableSlot, this, _1, false))
1523 ("Disconnect a connected slot.");
1524
1525 T::AddEvent("TOGGLE", "S:1", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
1526 (boost::bind(&StateMachineFAD::ToggleSlot, this, _1))
1527 ("");
1528
1529 T::AddEvent("CLOSE_OPEN_FILES", FAD::kConnecting, FAD::kConnected)
1530 (boost::bind(&StateMachineFAD::CloseOpenFiles, this))
1531 ("Close all run files opened by the EventBuilder.");
1532
1533 T::AddEvent("TEST", "S:1")
1534 (boost::bind(&StateMachineFAD::Test, this, _1))
1535 ("");
1536
1537 T::AddEvent("ADD_ADDRESS", "C", FAD::kOffline)
1538 (boost::bind(&StateMachineFAD::AddAddress, this, _1))
1539 ("Add the address of a DRS4 board to the first free slot"
1540 "|IP[string]:address in the format <address:port>");
1541 T::AddEvent("REMOVE_SLOT", "S:1", FAD::kOffline)
1542 (boost::bind(&StateMachineFAD::RemoveSlot, this, _1))
1543 ("Remove the Iaddress in slot n. For a list see LIST"
1544 "|slot[short]:Remove the address in slot n from the list");
1545 T::AddEvent("LIST_SLOTS")
1546 (boost::bind(&StateMachineFAD::ListSlots, this))
1547 ("Print a list of all available board addressesa and whether they are enabled");
1548 }
1549
1550 ~StateMachineFAD()
1551 {
1552 for (BoardList::const_iterator i=fBoards.begin(); i!=fBoards.end(); i++)
1553 delete i->second.second;
1554 fBoards.clear();
1555 }
1556
1557 tcp::endpoint GetEndpoint(const string &base)
1558 {
1559 const size_t p0 = base.find_first_of(':');
1560 const size_t p1 = base.find_last_of(':');
1561
1562 if (p0==string::npos || p0!=p1)
1563 {
1564 T::Out() << kRed << "GetEndpoint - Wrong format ('host:port' expected)" << endl;
1565 return tcp::endpoint();
1566 }
1567
1568 tcp::resolver resolver(get_io_service());
1569
1570 boost::system::error_code ec;
1571
1572 const tcp::resolver::query query(base.substr(0, p0), base.substr(p0+1));
1573 const tcp::resolver::iterator iterator = resolver.resolve(query, ec);
1574
1575 if (ec)
1576 {
1577 T::Out() << kRed << "GetEndpoint - Couldn't resolve endpoint '" << base << "': " << ec.message();
1578 return tcp::endpoint();
1579 }
1580
1581 return *iterator;
1582 }
1583
1584 bool SetConfiguration(const Configuration &conf)
1585 {
1586 fIsVerbose = !conf.Get<bool>("quiet");
1587 fIsHexOutput = conf.Get<bool>("hex-out");
1588 fIsDataOutput = conf.Get<bool>("data-out");
1589 fDebugTx = conf.Get<bool>("debug-tx");
1590
1591 SetMaxMemory(conf.Get<unsigned int>("max-mem"));
1592
1593 // vvvvv for debugging vvvvv
1594 if (conf.Has("debug-addr"))
1595 {
1596 const string addr = conf.Get<string>("debug-addr");
1597 const int num = conf.Get<unsigned int>("debug-num");
1598
1599 const tcp::endpoint endpoint = GetEndpoint(addr);
1600 if (endpoint==tcp::endpoint())
1601 return false;
1602
1603 for (int i=0; i<num; i++)
1604 AddEndpoint(tcp::endpoint(endpoint.address(), endpoint.port()+8*i));
1605
1606 StartConnection();
1607 return true;
1608 }
1609 // ^^^^^ for debugging ^^^^^
1610
1611 if (!(conf.Has("base-addr") ^ conf.Has("addr")))
1612 {
1613 T::Out() << kRed << "SetConfiguration - Only --base-addr or --addr allowed." << endl;
1614 return false;
1615 }
1616
1617 if (conf.Has("base-addr"))
1618 {
1619 string base = conf.Get<string>("base-addr");
1620
1621 if (base=="def" || base =="default")
1622 base = "10.0.128.128:5000";
1623
1624 const tcp::endpoint endpoint = GetEndpoint(base);
1625 if (endpoint==tcp::endpoint())
1626 return false;
1627
1628 const ba::ip::address_v4::bytes_type ip = endpoint.address().to_v4().to_bytes();
1629
1630 if (ip[2]>250 || ip[3]>244)
1631 {
1632 T::Out() << kRed << "SetConfiguration - IP address given by --base-addr out-of-range." << endl;
1633 return false;
1634 }
1635
1636 for (int crate=0; crate<4; crate++)
1637 for (int board=0; board<10; board++)
1638 {
1639 ba::ip::address_v4::bytes_type target = endpoint.address().to_v4().to_bytes();
1640 target[2] += crate;
1641 target[3] += board;
1642
1643 AddEndpoint(tcp::endpoint(ba::ip::address_v4(target), endpoint.port()));
1644 }
1645 }
1646
1647 if (conf.Has("addr"))
1648 {
1649 const vector<string> addrs = conf.Get<vector<string>>("addr");
1650 for (vector<string>::const_iterator i=addrs.begin(); i<addrs.end(); i++)
1651 {
1652 const tcp::endpoint endpoint = GetEndpoint(*i);
1653 if (endpoint==tcp::endpoint())
1654 return false;
1655
1656 AddEndpoint(endpoint);
1657 }
1658 }
1659
1660 StartConnection();
1661
1662 return true;
1663 }
1664
1665};
1666
1667// ------------------------------------------------------------------------
1668
1669
1670void RunThread(StateMachineImp *io_service)
1671{
1672 // This is necessary so that the StateMachien Thread can signal the
1673 // Readline to exit
1674 io_service->Run();
1675 Readline::Stop();
1676}
1677
1678/*
1679template<class S>
1680int RunDim(Configuration &conf)
1681{
1682 WindowLog wout;
1683
1684 ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
1685
1686 if (conf.Has("log"))
1687 if (!wout.OpenLogFile(conf.Get<string>("log")))
1688 wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
1689
1690 // Start io_service.Run to use the StateMachineImp::Run() loop
1691 // Start io_service.run to only use the commandHandler command detaching
1692 StateMachineFAD<S> io_service(wout);
1693 if (!io_service.SetConfiguration(conf))
1694 return -1;
1695
1696 io_service.Run();
1697
1698 return 0;
1699}
1700*/
1701
1702template<class T, class S>
1703int RunShell(Configuration &conf)
1704{
1705 static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
1706
1707 WindowLog &win = shell.GetStreamIn();
1708 WindowLog &wout = shell.GetStreamOut();
1709
1710 if (conf.Has("log"))
1711 if (!wout.OpenLogFile(conf.Get<string>("log")))
1712 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
1713
1714 StateMachineFAD<S> io_service(wout);
1715 if (!io_service.SetConfiguration(conf))
1716 return -1;
1717
1718 shell.SetReceiver(io_service);
1719
1720 boost::thread t(boost::bind(RunThread, &io_service));
1721 //boost::thread t(boost::bind(&StateMachineFAD<S>::Run, &io_service));
1722
1723 if (conf.Has("exec"))
1724 {
1725 const vector<string> v = conf.Get<vector<string>>("exec");
1726 for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
1727 shell.Execute(*it);
1728 }
1729
1730 shell.Run(); // Run the shell
1731 io_service.Stop(); // Signal Loop-thread to stop
1732
1733 // Wait until the StateMachine has finished its thread
1734 // before returning and destroying the dim objects which might
1735 // still be in use.
1736 t.join();
1737
1738 return 0;
1739}
1740
1741void SetupConfiguration(Configuration &conf)
1742{
1743 const string n = conf.GetName()+".log";
1744
1745 po::options_description config("Program options");
1746 config.add_options()
1747 ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
1748 ("log,l", var<string>(n), "Write log-file")
1749 ("no-dim,d", po_switch(), "Disable dim services")
1750 ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
1751 ("exec,e", vars<string>(), "Execute one or more scrips at startup")
1752 ;
1753
1754 po::options_description control("FAD control options");
1755 control.add_options()
1756 ("quiet,q", po_bool(), "Disable printing contents of all received messages in clear text.")
1757 ("hex-out", po_bool(), "Enable printing contents of all printed messages also as hex data.")
1758 ("data-out", po_bool(), "Enable printing received event data.")
1759 ("debug-tx", po_bool(), "Enable debugging of ethernet transmission.")
1760 ;
1761
1762 po::options_description builder("Event builder options");
1763 builder.add_options()
1764 ("max-mem,m", var<unsigned int>(100), "Maximum memory the event builder thread is allowed to consume for its event buffer")
1765 ;
1766
1767 po::options_description connect("FAD connection options");
1768 connect.add_options()
1769 ("addr", vars<string>(), "Network address of FAD")
1770 ("base-addr", var<string>(), "Base address of all FAD")
1771 ("debug-num,n", var<unsigned int>(40), "Sets the number of fake boards to be connected locally")
1772 ("debug-addr", var<string>(), "")
1773 ;
1774
1775 conf.AddEnv("dns", "DIM_DNS_NODE");
1776
1777 conf.AddOptions(config);
1778 conf.AddOptions(control);
1779 conf.AddOptions(builder);
1780 conf.AddOptions(connect);
1781}
1782
1783void PrintUsage()
1784{
1785 cout <<
1786 "The fadctrl controls the FAD boards.\n"
1787 "\n"
1788 "The default is that the program is started without user intercation. "
1789 "All actions are supposed to arrive as DimCommands. Using the -c "
1790 "option, a local shell can be initialized. With h or help a short "
1791 "help message about the usuage can be brought to the screen.\n"
1792 "\n"
1793 "Usage: fadctrl [-c type] [OPTIONS]\n"
1794 " or: fadctrl [OPTIONS]\n";
1795 cout << endl;
1796}
1797
1798void PrintHelp()
1799{
1800 /* Additional help text which is printed after the configuration
1801 options goes here */
1802}
1803
1804int main(int argc, const char* argv[])
1805{
1806 Configuration conf(argv[0]);
1807 conf.SetPrintUsage(PrintUsage);
1808 SetupConfiguration(conf);
1809
1810 po::variables_map vm;
1811 try
1812 {
1813 vm = conf.Parse(argc, argv);
1814 }
1815#if BOOST_VERSION > 104000
1816 catch (po::multiple_occurrences &e)
1817 {
1818 cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
1819 return -1;
1820 }
1821#endif
1822 catch (exception& e)
1823 {
1824 cerr << "Program options invalid due to: " << e.what() << endl;
1825 return -1;
1826 }
1827
1828 if (conf.HasVersion() || conf.HasPrint())
1829 return -1;
1830
1831 if (conf.HasHelp())
1832 {
1833 PrintHelp();
1834 return -1;
1835 }
1836
1837 Dim::Setup(conf.Get<string>("dns"));
1838
1839// try
1840 {
1841 // No console access at all
1842 if (!conf.Has("console"))
1843 {
1844 if (conf.Get<bool>("no-dim"))
1845 return RunShell<LocalStream, StateMachine>(conf);
1846 else
1847 return RunShell<LocalStream, StateMachineDim>(conf);
1848 }
1849 // Cosole access w/ and w/o Dim
1850 if (conf.Get<bool>("no-dim"))
1851 {
1852 if (conf.Get<int>("console")==0)
1853 return RunShell<LocalShell, StateMachine>(conf);
1854 else
1855 return RunShell<LocalConsole, StateMachine>(conf);
1856 }
1857 else
1858 {
1859 if (conf.Get<int>("console")==0)
1860 return RunShell<LocalShell, StateMachineDim>(conf);
1861 else
1862 return RunShell<LocalConsole, StateMachineDim>(conf);
1863 }
1864 }
1865/* catch (std::exception& e)
1866 {
1867 cerr << "Exception: " << e.what() << endl;
1868 return -1;
1869 }*/
1870
1871 return 0;
1872}
Note: See TracBrowser for help on using the repository browser.