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

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