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