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