| 1 | #include <functional>
|
|---|
| 2 |
|
|---|
| 3 | #include "Dim.h"
|
|---|
| 4 | #include "Event.h"
|
|---|
| 5 | #include "Shell.h"
|
|---|
| 6 | #include "StateMachineDim.h"
|
|---|
| 7 | #include "ConnectionUSB.h"
|
|---|
| 8 | #include "Configuration.h"
|
|---|
| 9 | #include "Console.h"
|
|---|
| 10 | #include "Converter.h"
|
|---|
| 11 |
|
|---|
| 12 | #include "tools.h"
|
|---|
| 13 |
|
|---|
| 14 | #include "LocalControl.h"
|
|---|
| 15 |
|
|---|
| 16 | namespace ba = boost::asio;
|
|---|
| 17 | namespace bs = boost::system;
|
|---|
| 18 | namespace dummy = ba::placeholders;
|
|---|
| 19 |
|
|---|
| 20 | using namespace std::placeholders;
|
|---|
| 21 | using namespace std;
|
|---|
| 22 |
|
|---|
| 23 | // ------------------------------------------------------------------------
|
|---|
| 24 |
|
|---|
| 25 | class ConnectionBias : public ConnectionUSB
|
|---|
| 26 | {
|
|---|
| 27 | vector<char> fBuffer;
|
|---|
| 28 |
|
|---|
| 29 | bool fIsVerbose;
|
|---|
| 30 |
|
|---|
| 31 | enum
|
|---|
| 32 | {
|
|---|
| 33 | kNumBoards = 13,
|
|---|
| 34 | kNumChannelsPerBoard = 32,
|
|---|
| 35 | kNumChannels = kNumBoards*kNumChannelsPerBoard
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | enum Command_t
|
|---|
| 39 | {
|
|---|
| 40 | kCmdReset = 0,
|
|---|
| 41 | kCmdRead = 1,
|
|---|
| 42 | kCmdWrite = 3
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | // Resistance in Ohm for voltage correction
|
|---|
| 47 | #define RESISTOR float(1000)
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | vector<uint16_t> fVolt; // Voltage in DAC units
|
|---|
| 51 | vector<uint16_t> fRefVolt;
|
|---|
| 52 |
|
|---|
| 53 | vector<uint16_t> fCurrent; // Current in ADC units
|
|---|
| 54 | vector<uint16_t> fRefCurrent;
|
|---|
| 55 |
|
|---|
| 56 | vector<bool> fOC;
|
|---|
| 57 | vector<bool> fPresent;
|
|---|
| 58 |
|
|---|
| 59 | bool fResetHit;
|
|---|
| 60 |
|
|---|
| 61 | private:
|
|---|
| 62 | void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int /*type*/)
|
|---|
| 63 | {
|
|---|
| 64 | // Do not schedule a new read if the connection failed.
|
|---|
| 65 | if (bytes_received==0 || err)
|
|---|
| 66 | {
|
|---|
| 67 | if (err==ba::error::eof)
|
|---|
| 68 | Warn("Connection closed by remote host (BIAS).");
|
|---|
| 69 |
|
|---|
| 70 | // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
|
|---|
| 71 | // 125: Operation canceled
|
|---|
| 72 | if (err && err!=ba::error::eof && // Connection closed by remote host
|
|---|
| 73 | err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
|
|---|
| 74 | err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
|
|---|
| 75 | {
|
|---|
| 76 | ostringstream str;
|
|---|
| 77 | str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
|
|---|
| 78 | Error(str);
|
|---|
| 79 | }
|
|---|
| 80 | PostClose(err!=ba::error::basic_errors::operation_aborted);
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (bytes_received%3)
|
|---|
| 85 | {
|
|---|
| 86 | Error("Number of received bytes not a multiple of 3, can't read data.");
|
|---|
| 87 | PostClose(true);
|
|---|
| 88 | return;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if (fIsVerbose)
|
|---|
| 92 | {
|
|---|
| 93 | Out() << endl << kBold << "Data received:" << endl;
|
|---|
| 94 | Out() << Converter::GetHex<uint8_t>(fBuffer, 32) << endl;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | int wrapcnt = -1;
|
|---|
| 98 |
|
|---|
| 99 | // === Check/update all wrap counter ===
|
|---|
| 100 | for (unsigned int i=0; i<fBuffer.size(); i += 3)
|
|---|
| 101 | {
|
|---|
| 102 | const int old = wrapcnt;
|
|---|
| 103 | wrapcnt = (fBuffer[i]>>4)&7;
|
|---|
| 104 |
|
|---|
| 105 | if (wrapcnt==-1 || (old+1)%8 == wrapcnt)
|
|---|
| 106 | continue;
|
|---|
| 107 |
|
|---|
| 108 | Error("WrapCnt wrong");
|
|---|
| 109 | // Error receiving proper answer!
|
|---|
| 110 | return;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | // Success with received answer
|
|---|
| 114 |
|
|---|
| 115 | if (fBuffer.size()!=kNumChannels*3)
|
|---|
| 116 | return;
|
|---|
| 117 |
|
|---|
| 118 | /*
|
|---|
| 119 | data[0] = (cmd<<5) | (board<<1) | (((channel&16)>>4) & 1);
|
|---|
| 120 | data[1] = (channel<<4) | (cmdval>>8);
|
|---|
| 121 | data[2] = val&0xff;
|
|---|
| 122 | */
|
|---|
| 123 |
|
|---|
| 124 | // ################## Read all channels status ##################
|
|---|
| 125 |
|
|---|
| 126 | // Evaluate data returned from crate
|
|---|
| 127 | for (int i=0; i<kNumChannels; i++)
|
|---|
| 128 | {
|
|---|
| 129 | fOC[i] = fBuffer[i*3]&0x80;
|
|---|
| 130 | fCurrent[i] = fBuffer[i*3+1] | ((fBuffer[i*3]&0xf)<<8);
|
|---|
| 131 | fPresent[i] = fBuffer[i*3+2]&0x70 ? false : true;
|
|---|
| 132 |
|
|---|
| 133 | // FIXME FIXME FIXME
|
|---|
| 134 | fResetHit = fBuffer[i*3+2] & 0x80;
|
|---|
| 135 |
|
|---|
| 136 | if (i==2*kNumChannelsPerBoard+19)
|
|---|
| 137 | fOC[i] = false;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void HandleTransmittedData(size_t n)
|
|---|
| 142 | {
|
|---|
| 143 | fBuffer.resize(n);
|
|---|
| 144 | AsyncRead(ba::buffer(fBuffer));
|
|---|
| 145 | AsyncWait(fInTimeout, 50, &ConnectionUSB::HandleReadTimeout);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | // This is called when a connection was established
|
|---|
| 149 | void ConnectionEstablished()
|
|---|
| 150 | {
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | void HandleReadTimeout(const bs::error_code &error)
|
|---|
| 154 | {
|
|---|
| 155 | if (error==ba::error::basic_errors::operation_aborted)
|
|---|
| 156 | return;
|
|---|
| 157 |
|
|---|
| 158 | if (error)
|
|---|
| 159 | {
|
|---|
| 160 | ostringstream str;
|
|---|
| 161 | str << "Read timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
|
|---|
| 162 | Error(str);
|
|---|
| 163 |
|
|---|
| 164 | PostClose();
|
|---|
| 165 | return;
|
|---|
| 166 |
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | if (!is_open())
|
|---|
| 170 | {
|
|---|
| 171 | // For example: Here we could schedule a new accept if we
|
|---|
| 172 | // would not want to allow two connections at the same time.
|
|---|
| 173 | return;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | // Check whether the deadline has passed. We compare the deadline
|
|---|
| 177 | // against the current time since a new asynchronous operation
|
|---|
| 178 | // may have moved the deadline before this actor had a chance
|
|---|
| 179 | // to run.
|
|---|
| 180 | if (fInTimeout.expires_at() > ba::deadline_timer::traits_type::now())
|
|---|
| 181 | return;
|
|---|
| 182 |
|
|---|
| 183 | Error("Timeout reading data from "+URL());
|
|---|
| 184 |
|
|---|
| 185 | PostClose();
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | void SystemReset()
|
|---|
| 190 | {
|
|---|
| 191 | PostMessage(GetCmd(0, kCmdReset));
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | vector<char> GetCmd(uint16_t id, Command_t cmd, uint16_t dac=0)
|
|---|
| 195 | {
|
|---|
| 196 | const unsigned int board = id/kNumChannelsPerBoard;
|
|---|
| 197 | const unsigned int channel = id%kNumChannelsPerBoard;
|
|---|
| 198 |
|
|---|
| 199 | return GetCmd(board, channel, cmd, dac);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | vector<char> GetCmd(uint16_t board, uint16_t channel, Command_t cmd, uint16_t dac=0)
|
|---|
| 203 | {
|
|---|
| 204 | vector<char> data(3);
|
|---|
| 205 |
|
|---|
| 206 | /*
|
|---|
| 207 | if (board>kNumBoards)
|
|---|
| 208 | return;
|
|---|
| 209 | if (channel>kNumChannelsPerBoard)
|
|---|
| 210 | return;
|
|---|
| 211 | if (dac>0xfff)
|
|---|
| 212 | return;
|
|---|
| 213 | */
|
|---|
| 214 |
|
|---|
| 215 | data[0] = (cmd<<5) | (board<<1) | (((channel&16)>>4) & 1);
|
|---|
| 216 | data[1] = (channel<<4) | (dac>>8);
|
|---|
| 217 | data[2] = dac&0xff;
|
|---|
| 218 |
|
|---|
| 219 | return data;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | void ReadAllChannelsStatus()
|
|---|
| 223 | {
|
|---|
| 224 | vector<char> data;
|
|---|
| 225 | data.reserve(kNumChannels*3);
|
|---|
| 226 |
|
|---|
| 227 | // Prepare command to read all channels
|
|---|
| 228 | for (int i=0; i<kNumChannels; i++)
|
|---|
| 229 | {
|
|---|
| 230 | const vector<char> cmd = GetCmd(i, kCmdRead);
|
|---|
| 231 | data.insert(data.end(), cmd.begin(), cmd.end());
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | PostMessage(data);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | void GlobalSetDac(uint16_t dac)
|
|---|
| 238 | {
|
|---|
| 239 | PostMessage(GetCmd(0, kCmdWrite, dac));
|
|---|
| 240 | /*
|
|---|
| 241 | // On success
|
|---|
| 242 | if (fBuffer.size() == 3)
|
|---|
| 243 | {
|
|---|
| 244 | for (int i=0; i<MAX_NUM_BOARDS; i++)
|
|---|
| 245 | for (int j=0; j<NUM_CHANNELS; j++)
|
|---|
| 246 | {
|
|---|
| 247 | DAC[i][j] = SetPoint;
|
|---|
| 248 | Volt[i][j] = Voltage;
|
|---|
| 249 | RefVolt[i][j] = Voltage;
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 | */
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | void SetChannels(const map<uint16_t, uint16_t> &vals)
|
|---|
| 257 | {
|
|---|
| 258 | if (vals.empty())
|
|---|
| 259 | return;
|
|---|
| 260 |
|
|---|
| 261 | vector<char> data;
|
|---|
| 262 |
|
|---|
| 263 | // Build and execute commands
|
|---|
| 264 | for (map<uint16_t, uint16_t>::const_iterator it=vals.begin();
|
|---|
| 265 | it!=vals.end(); it++)
|
|---|
| 266 | {
|
|---|
| 267 | //const uint16_t dac = it->second/90.0*0xfff;
|
|---|
| 268 |
|
|---|
| 269 | // If DAC value unchanged, do not send command
|
|---|
| 270 | if (fVolt[it->first] == it->second)
|
|---|
| 271 | continue;
|
|---|
| 272 |
|
|---|
| 273 | const vector<char> cmd = GetCmd(it->first, kCmdWrite, it->second);
|
|---|
| 274 | data.insert(data.end(), cmd.begin(), cmd.end());
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | PostMessage(data);
|
|---|
| 278 |
|
|---|
| 279 | /*
|
|---|
| 280 | // On success
|
|---|
| 281 | if (Data.size() == Buf.size())
|
|---|
| 282 | {
|
|---|
| 283 | for (map<unsigned int, double>::const_iterator it = V.begin(); it != V.end(); ++it) {
|
|---|
| 284 | DAC[it->first/NUM_CHANNELS][it->first%NUM_CHANNELS] = (unsigned int) (it->second/90.0*0x0fff);
|
|---|
| 285 | Volt[it->first/NUM_CHANNELS][it->first%NUM_CHANNELS] = it->second;
|
|---|
| 286 | RefVolt[it->first/NUM_CHANNELS][it->first%NUM_CHANNELS] = it->second;
|
|---|
| 287 | }
|
|---|
| 288 | */
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /*
|
|---|
| 292 | // ***** Synchronize board *****
|
|---|
| 293 | bool Crate::Synch()
|
|---|
| 294 | {
|
|---|
| 295 | //############################################################
|
|---|
| 296 | int Trial = 0;
|
|---|
| 297 | vector<unsigned char> Data;
|
|---|
| 298 |
|
|---|
| 299 | while(++Trial <= 3) {
|
|---|
| 300 | Data = Communicate(string(1, 0));
|
|---|
| 301 | if (Data.size() == 3) return true;
|
|---|
| 302 | }
|
|---|
| 303 | return false;
|
|---|
| 304 | //############################################################
|
|---|
| 305 | }
|
|---|
| 306 | */
|
|---|
| 307 |
|
|---|
| 308 | void SetReferenceCurrent()
|
|---|
| 309 | {
|
|---|
| 310 | fRefCurrent = fCurrent;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | void GlobalSet(double voltage)
|
|---|
| 314 | {
|
|---|
| 315 | if (voltage>90)
|
|---|
| 316 | return;
|
|---|
| 317 |
|
|---|
| 318 | GlobalSetDac(voltage/90.0*0xfff);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | // ***** Correct voltages according to current *****
|
|---|
| 322 | void AdaptVoltages()
|
|---|
| 323 | {
|
|---|
| 324 | map<uint16_t, uint16_t> values;
|
|---|
| 325 |
|
|---|
| 326 | for (int i=0; i<kNumChannels; i++)
|
|---|
| 327 | {
|
|---|
| 328 | if (fRefVolt[i]==0)
|
|---|
| 329 | continue;
|
|---|
| 330 |
|
|---|
| 331 | // Calculate difference and convert ADC units to Amps
|
|---|
| 332 | const double diffcur = (fRefCurrent[i]-fCurrent[i])*1.22;
|
|---|
| 333 |
|
|---|
| 334 | // Calculate voltage difference
|
|---|
| 335 | const double diffvolt = diffcur*RESISTOR/1e6;
|
|---|
| 336 |
|
|---|
| 337 | // Calculate new vlaue by onverting voltage difference to DAC units
|
|---|
| 338 | const int32_t dac = fRefVolt[i] + diffvolt/90.0*0xfff;
|
|---|
| 339 |
|
|---|
| 340 | if (dac<0 || dac>0xfff)
|
|---|
| 341 | continue;
|
|---|
| 342 |
|
|---|
| 343 | values[i] = fRefVolt[i] + dac;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | SetChannels(values);
|
|---|
| 347 |
|
|---|
| 348 | /*
|
|---|
| 349 | static int LastUpdate = 0;
|
|---|
| 350 | if (time(NULL)-LastUpdate > 5)
|
|---|
| 351 | {
|
|---|
| 352 | LastUpdate = time(NULL);
|
|---|
| 353 | UpdateDIM();
|
|---|
| 354 | }*/
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | public:
|
|---|
| 358 | ConnectionBias(ba::io_service& ioservice, MessageImp &imp) : ConnectionUSB(ioservice, imp()),
|
|---|
| 359 | fIsVerbose(true),
|
|---|
| 360 | fVolt(kNumChannels),
|
|---|
| 361 | fRefVolt(kNumChannels),
|
|---|
| 362 | fCurrent(kNumChannels),
|
|---|
| 363 | fRefCurrent(kNumChannels),
|
|---|
| 364 | fOC(kNumChannels),
|
|---|
| 365 | fPresent(kNumChannels)
|
|---|
| 366 | {
|
|---|
| 367 | SetLogStream(&imp);
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | void SetVerbose(bool b)
|
|---|
| 371 | {
|
|---|
| 372 | fIsVerbose = b;
|
|---|
| 373 | }
|
|---|
| 374 | };
|
|---|
| 375 |
|
|---|
| 376 | // ------------------------------------------------------------------------
|
|---|
| 377 |
|
|---|
| 378 | #include "DimDescriptionService.h"
|
|---|
| 379 |
|
|---|
| 380 | class ConnectionDimBias : public ConnectionBias
|
|---|
| 381 | {
|
|---|
| 382 | private:
|
|---|
| 383 |
|
|---|
| 384 | DimDescribedService fDimCurrent;
|
|---|
| 385 |
|
|---|
| 386 | void Update(DimDescribedService &svc, vector<float> data, float time) const
|
|---|
| 387 | {
|
|---|
| 388 | data.insert(data.begin(), time);
|
|---|
| 389 | svc.setData(data.data(), data.size()*sizeof(float));
|
|---|
| 390 | svc.updateService();
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | void UpdateCur(float time, const vector<float> &curr)
|
|---|
| 394 | {
|
|---|
| 395 | Update(fDimCurrent, curr, time);
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | public:
|
|---|
| 399 | ConnectionDimBias(ba::io_service& ioservice, MessageImp &imp) :
|
|---|
| 400 | ConnectionBias(ioservice, imp),
|
|---|
| 401 | fDimCurrent("BIAS_CONTROL/CURRENT", "F:1;F:4", "")
|
|---|
| 402 | {
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | // A B [C] [D] E [F] G H [I] J K [L] M N O P Q R [S] T U V W [X] Y Z
|
|---|
| 406 | };
|
|---|
| 407 |
|
|---|
| 408 | // ------------------------------------------------------------------------
|
|---|
| 409 |
|
|---|
| 410 | template <class T, class S>
|
|---|
| 411 | class StateMachineBias : public T, public ba::io_service, public ba::io_service::work
|
|---|
| 412 | {
|
|---|
| 413 | int Wrap(boost::function<void()> f)
|
|---|
| 414 | {
|
|---|
| 415 | f();
|
|---|
| 416 | return T::GetCurrentState();
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | function<int(const EventImp &)> Wrapper(function<void()> func)
|
|---|
| 420 | {
|
|---|
| 421 | return bind(&StateMachineBias::Wrap, this, func);
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | private:
|
|---|
| 425 | S fBias;
|
|---|
| 426 |
|
|---|
| 427 | enum states_t
|
|---|
| 428 | {
|
|---|
| 429 | kStateDisconnected = 1,
|
|---|
| 430 | kStateConnected = 2,
|
|---|
| 431 | };
|
|---|
| 432 |
|
|---|
| 433 | int Disconnect()
|
|---|
| 434 | {
|
|---|
| 435 | // Close all connections
|
|---|
| 436 | fBias.PostClose(false);
|
|---|
| 437 |
|
|---|
| 438 | /*
|
|---|
| 439 | // Now wait until all connection have been closed and
|
|---|
| 440 | // all pending handlers have been processed
|
|---|
| 441 | poll();
|
|---|
| 442 | */
|
|---|
| 443 |
|
|---|
| 444 | return T::GetCurrentState();
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | int Reconnect(const EventImp &evt)
|
|---|
| 448 | {
|
|---|
| 449 | // Close all connections to supress the warning in SetEndpoint
|
|---|
| 450 | fBias.PostClose(false);
|
|---|
| 451 |
|
|---|
| 452 | // Now wait until all connection have been closed and
|
|---|
| 453 | // all pending handlers have been processed
|
|---|
| 454 | poll();
|
|---|
| 455 |
|
|---|
| 456 | if (evt.GetBool())
|
|---|
| 457 | fBias.SetEndpoint(evt.GetString());
|
|---|
| 458 |
|
|---|
| 459 | // Now we can reopen the connection
|
|---|
| 460 | fBias.PostClose(true);
|
|---|
| 461 |
|
|---|
| 462 | return T::GetCurrentState();
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | int Execute()
|
|---|
| 466 | {
|
|---|
| 467 | // Dispatch (execute) at most one handler from the queue. In contrary
|
|---|
| 468 | // to run_one(), it doesn't wait until a handler is available
|
|---|
| 469 | // which can be dispatched, so poll_one() might return with 0
|
|---|
| 470 | // handlers dispatched. The handlers are always dispatched/executed
|
|---|
| 471 | // synchronously, i.e. within the call to poll_one()
|
|---|
| 472 | poll_one();
|
|---|
| 473 |
|
|---|
| 474 | return fBias.IsConnected() ? kStateConnected : kStateDisconnected;
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | bool CheckEventSize(size_t has, const char *name, size_t size)
|
|---|
| 478 | {
|
|---|
| 479 | if (has==size)
|
|---|
| 480 | return true;
|
|---|
| 481 |
|
|---|
| 482 | ostringstream msg;
|
|---|
| 483 | msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
|
|---|
| 484 | T::Fatal(msg);
|
|---|
| 485 | return false;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | int SetVerbosity(const EventImp &evt)
|
|---|
| 489 | {
|
|---|
| 490 | if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
|
|---|
| 491 | return T::kSM_FatalError;
|
|---|
| 492 |
|
|---|
| 493 | fBias.SetVerbose(evt.GetBool());
|
|---|
| 494 |
|
|---|
| 495 | return T::GetCurrentState();
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | public:
|
|---|
| 499 | StateMachineBias(ostream &out=cout) :
|
|---|
| 500 | T(out, "BIAS_CONTROL"), ba::io_service::work(static_cast<ba::io_service&>(*this)),
|
|---|
| 501 | fBias(*this, *this)
|
|---|
| 502 | {
|
|---|
| 503 | // ba::io_service::work is a kind of keep_alive for the loop.
|
|---|
| 504 | // It prevents the io_service to go to stopped state, which
|
|---|
| 505 | // would prevent any consecutive calls to run()
|
|---|
| 506 | // or poll() to do nothing. reset() could also revoke to the
|
|---|
| 507 | // previous state but this might introduce some overhead of
|
|---|
| 508 | // deletion and creation of threads and more.
|
|---|
| 509 |
|
|---|
| 510 | // State names
|
|---|
| 511 | AddStateName(kStateDisconnected, "Disconnected",
|
|---|
| 512 | "Bias-power supply not connected via USB.");
|
|---|
| 513 |
|
|---|
| 514 | AddStateName(kStateConnected, "Connected",
|
|---|
| 515 | "USB connection to bias-power supply established.");
|
|---|
| 516 |
|
|---|
| 517 | // Verbosity commands
|
|---|
| 518 | T::AddEvent("SET_VERBOSE", "B")
|
|---|
| 519 | (bind(&StateMachineBias::SetVerbosity, this, _1))
|
|---|
| 520 | ("set verbosity state"
|
|---|
| 521 | "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
|---|
| 522 |
|
|---|
| 523 | // Conenction commands
|
|---|
| 524 | AddEvent("DISCONNECT", kStateConnected)
|
|---|
| 525 | (bind(&StateMachineBias::Disconnect, this))
|
|---|
| 526 | ("disconnect from ethernet");
|
|---|
| 527 |
|
|---|
| 528 | AddEvent("RECONNECT", "O", kStateDisconnected, kStateConnected)
|
|---|
| 529 | (bind(&StateMachineBias::Reconnect, this, _1))
|
|---|
| 530 | ("(Re)connect ethernet connection to FTM, a new address can be given"
|
|---|
| 531 | "|[host][string]:new ethernet address in the form <host:port>");
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | void SetEndpoint(const string &url)
|
|---|
| 535 | {
|
|---|
| 536 | fBias.SetEndpoint(url);
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | int EvalOptions(Configuration &conf)
|
|---|
| 540 | {
|
|---|
| 541 | SetEndpoint(conf.Get<string>("addr"));
|
|---|
| 542 |
|
|---|
| 543 | fBias.SetVerbose(!conf.Get<bool>("quiet"));
|
|---|
| 544 |
|
|---|
| 545 | fBias.Connect();
|
|---|
| 546 |
|
|---|
| 547 | return -1;
|
|---|
| 548 | }
|
|---|
| 549 | };
|
|---|
| 550 |
|
|---|
| 551 | // ------------------------------------------------------------------------
|
|---|
| 552 |
|
|---|
| 553 | void RunThread(StateMachineImp *io_service)
|
|---|
| 554 | {
|
|---|
| 555 | // This is necessary so that the StateMachien Thread can signal the
|
|---|
| 556 | // Readline to exit
|
|---|
| 557 | io_service->Run();
|
|---|
| 558 | Readline::Stop();
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | /*
|
|---|
| 562 | template<class S, class T>
|
|---|
| 563 | int RunDim(Configuration &conf)
|
|---|
| 564 | {
|
|---|
| 565 | WindowLog wout;
|
|---|
| 566 |
|
|---|
| 567 | ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
|
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 | if (conf.Has("log"))
|
|---|
| 571 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
|---|
| 572 | wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 573 |
|
|---|
| 574 | // Start io_service.Run to use the StateMachineImp::Run() loop
|
|---|
| 575 | // Start io_service.run to only use the commandHandler command detaching
|
|---|
| 576 | StateMachineBias<S, T> io_service(wout);
|
|---|
| 577 | if (!io_service.EvalConfiguration(conf))
|
|---|
| 578 | return -1;
|
|---|
| 579 |
|
|---|
| 580 | io_service.Run();
|
|---|
| 581 |
|
|---|
| 582 | return 0;
|
|---|
| 583 | }
|
|---|
| 584 | */
|
|---|
| 585 |
|
|---|
| 586 | #include "Main.h"
|
|---|
| 587 |
|
|---|
| 588 | template<class T, class S, class R>
|
|---|
| 589 | int RunShell(Configuration &conf)
|
|---|
| 590 | {
|
|---|
| 591 | return Main<T, StateMachineBias<S, R>>(conf);
|
|---|
| 592 | /*
|
|---|
| 593 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
|---|
| 594 |
|
|---|
| 595 | WindowLog &win = shell.GetStreamIn();
|
|---|
| 596 | WindowLog &wout = shell.GetStreamOut();
|
|---|
| 597 |
|
|---|
| 598 | if (conf.Has("log"))
|
|---|
| 599 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
|---|
| 600 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 601 |
|
|---|
| 602 | StateMachineBias<S, R> io_service(wout);
|
|---|
| 603 | if (!io_service.EvalConfiguration(conf))
|
|---|
| 604 | return -1;
|
|---|
| 605 |
|
|---|
| 606 | shell.SetReceiver(io_service);
|
|---|
| 607 |
|
|---|
| 608 | boost::thread t(bind(RunThread, &io_service));
|
|---|
| 609 | // boost::thread t(bind(&StateMachineBias<S>::Run, &io_service));
|
|---|
| 610 |
|
|---|
| 611 | if (conf.Has("cmd"))
|
|---|
| 612 | {
|
|---|
| 613 | const vector<string> v = conf.Get<vector<string>>("cmd");
|
|---|
| 614 | for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
|
|---|
| 615 | shell.ProcessLine(*it);
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | if (conf.Has("exec"))
|
|---|
| 619 | {
|
|---|
| 620 | const vector<string> v = conf.Get<vector<string>>("exec");
|
|---|
| 621 | for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
|
|---|
| 622 | shell.Execute(*it);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | if (conf.Get<bool>("quit"))
|
|---|
| 626 | shell.Stop();
|
|---|
| 627 |
|
|---|
| 628 | shell.Run(); // Run the shell
|
|---|
| 629 | io_service.Stop(); // Signal Loop-thread to stop
|
|---|
| 630 | // io_service.Close(); // Obsolete, done by the destructor
|
|---|
| 631 |
|
|---|
| 632 | // Wait until the StateMachine has finished its thread
|
|---|
| 633 | // before returning and destroying the dim objects which might
|
|---|
| 634 | // still be in use.
|
|---|
| 635 | t.join();
|
|---|
| 636 |
|
|---|
| 637 | return 0;*/
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | void SetupConfiguration(Configuration &conf)
|
|---|
| 641 | {
|
|---|
| 642 | const string n = conf.GetName()+".log";
|
|---|
| 643 |
|
|---|
| 644 | po::options_description config("Program options");
|
|---|
| 645 | config.add_options()
|
|---|
| 646 | ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
|---|
| 647 | ("log,l", var<string>(n), "Write log-file")
|
|---|
| 648 | ("no-dim,d", po_bool(), "Disable dim services")
|
|---|
| 649 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
|---|
| 650 | ("cmd", vars<string>(), "Execute one or more commands at startup")
|
|---|
| 651 | ("exec,e", vars<string>(), "Execute one or more scrips at startup")
|
|---|
| 652 | ("quit", po_switch(), "Quit after startup");
|
|---|
| 653 | ;
|
|---|
| 654 |
|
|---|
| 655 | po::options_description control("FTM control options");
|
|---|
| 656 | control.add_options()
|
|---|
| 657 | ("addr,a", var<string>("ttysS0"), "Device address of USB port to bias-power supply")
|
|---|
| 658 | ("quiet,q", po_bool(), "Disable printing contents of all received messages (except dynamic data) in clear text.")
|
|---|
| 659 | ;
|
|---|
| 660 |
|
|---|
| 661 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
|---|
| 662 |
|
|---|
| 663 | conf.AddOptions(config);
|
|---|
| 664 | conf.AddOptions(control);
|
|---|
| 665 | }
|
|---|
| 666 |
|
|---|
| 667 | /*
|
|---|
| 668 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 669 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 670 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 671 | (GNU coreutils) which contains both strings:
|
|---|
| 672 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 673 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 674 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 675 | */
|
|---|
| 676 | void PrintUsage()
|
|---|
| 677 | {
|
|---|
| 678 | cout <<
|
|---|
| 679 | "The biasctrl controls the bias-power supply boards.\n"
|
|---|
| 680 | "\n"
|
|---|
| 681 | "The default is that the program is started without user intercation. "
|
|---|
| 682 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
|---|
| 683 | "option, a local shell can be initialized. With h or help a short "
|
|---|
| 684 | "help message about the usuage can be brought to the screen.\n"
|
|---|
| 685 | "\n"
|
|---|
| 686 | "Usage: biasctrl [-c type] [OPTIONS]\n"
|
|---|
| 687 | " or: biasctrl [OPTIONS]\n";
|
|---|
| 688 | cout << endl;
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | void PrintHelp()
|
|---|
| 692 | {
|
|---|
| 693 | /* Additional help text which is printed after the configuration
|
|---|
| 694 | options goes here */
|
|---|
| 695 |
|
|---|
| 696 | /*
|
|---|
| 697 | cout << "bla bla bla" << endl << endl;
|
|---|
| 698 | cout << endl;
|
|---|
| 699 | cout << "Environment:" << endl;
|
|---|
| 700 | cout << "environment" << endl;
|
|---|
| 701 | cout << endl;
|
|---|
| 702 | cout << "Examples:" << endl;
|
|---|
| 703 | cout << "test exam" << endl;
|
|---|
| 704 | cout << endl;
|
|---|
| 705 | cout << "Files:" << endl;
|
|---|
| 706 | cout << "files" << endl;
|
|---|
| 707 | cout << endl;
|
|---|
| 708 | */
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | int main(int argc, const char* argv[])
|
|---|
| 712 | {
|
|---|
| 713 | Configuration conf(argv[0]);
|
|---|
| 714 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 715 | SetupConfiguration(conf);
|
|---|
| 716 |
|
|---|
| 717 | po::variables_map vm;
|
|---|
| 718 | try
|
|---|
| 719 | {
|
|---|
| 720 | vm = conf.Parse(argc, argv);
|
|---|
| 721 | }
|
|---|
| 722 | #if BOOST_VERSION > 104000
|
|---|
| 723 | catch (po::multiple_occurrences &e)
|
|---|
| 724 | {
|
|---|
| 725 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
|
|---|
| 726 | return -1;
|
|---|
| 727 | }
|
|---|
| 728 | #endif
|
|---|
| 729 | catch (exception& e)
|
|---|
| 730 | {
|
|---|
| 731 | cerr << "Program options invalid due to: " << e.what() << endl;
|
|---|
| 732 | return -1;
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | if (conf.HasVersion() || conf.HasPrint())
|
|---|
| 736 | return -1;
|
|---|
| 737 |
|
|---|
| 738 | if (conf.HasHelp())
|
|---|
| 739 | {
|
|---|
| 740 | PrintHelp();
|
|---|
| 741 | return -1;
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | Dim::Setup(conf.Get<string>("dns"));
|
|---|
| 745 |
|
|---|
| 746 | //try
|
|---|
| 747 | {
|
|---|
| 748 | // No console access at all
|
|---|
| 749 | if (!conf.Has("console"))
|
|---|
| 750 | {
|
|---|
| 751 | if (conf.Get<bool>("no-dim"))
|
|---|
| 752 | return RunShell<LocalStream, StateMachine, ConnectionBias>(conf);
|
|---|
| 753 | else
|
|---|
| 754 | return RunShell<LocalStream, StateMachineDim, ConnectionDimBias>(conf);
|
|---|
| 755 | }
|
|---|
| 756 | // Cosole access w/ and w/o Dim
|
|---|
| 757 | if (conf.Get<bool>("no-dim"))
|
|---|
| 758 | {
|
|---|
| 759 | if (conf.Get<int>("console")==0)
|
|---|
| 760 | return RunShell<LocalShell, StateMachine, ConnectionBias>(conf);
|
|---|
| 761 | else
|
|---|
| 762 | return RunShell<LocalConsole, StateMachine, ConnectionBias>(conf);
|
|---|
| 763 | }
|
|---|
| 764 | else
|
|---|
| 765 | {
|
|---|
| 766 | if (conf.Get<int>("console")==0)
|
|---|
| 767 | return RunShell<LocalShell, StateMachineDim, ConnectionDimBias>(conf);
|
|---|
| 768 | else
|
|---|
| 769 | return RunShell<LocalConsole, StateMachineDim, ConnectionDimBias>(conf);
|
|---|
| 770 | }
|
|---|
| 771 | }
|
|---|
| 772 | /*catch (std::exception& e)
|
|---|
| 773 | {
|
|---|
| 774 | cerr << "Exception: " << e.what() << endl;
|
|---|
| 775 | return -1;
|
|---|
| 776 | }*/
|
|---|
| 777 |
|
|---|
| 778 | return 0;
|
|---|
| 779 | }
|
|---|