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