FACT++  1.0
template<class T>
int StateMachineFAD< T >::Execute ( )
inlineprivatevirtual

Implements StateMachineAsio< T >.

Definition at line 1399 of file fadctrl.cc.

References ConnectionFAD::Cmd(), ConnectionFAD::IsConfigured(), Connection::IsConnected(), Connection::IsConnecting(), Connection::IsDisconnected(), kCmdBusyOn, kCmdContTrigger, kCmdResetEventCounter, kCmdSingleTrigger, kCmdSocket, MCP::State::kConfigured, FTM::State::kConfiguring1, FTM::State::kConfiguring2, MCP::State::kConfiguring3, Agilent::State::kConnected, BIAS::State::kConnecting, and Agilent::State::kDisconnected.

1400  {
1401  // ===== Evaluate connection status =====
1402 
1403  uint16_t nclosed1 = 0;
1404  uint16_t nconnecting1 = 0;
1405  uint16_t nconnecting2 = 0;
1406  uint16_t nconnected1 = 0;
1407  uint16_t nconnected2 = 0;
1408  uint16_t nconfigured = 0;
1409 
1410  vector<uint8_t> stat1(40);
1411  vector<uint8_t> stat2(40);
1412 
1413  int cnt = 0; // counter for enabled board
1414 
1415  const bool runs = IsThreadRunning();
1416 
1417  for (int idx=0; idx<40; idx++)
1418  {
1419  // ----- Command socket -----
1420  const BoardList::const_iterator &slot = fBoards.find(idx);
1421  if (slot!=fBoards.end())
1422  {
1423  const ConnectionFAD *c = slot->second;
1424  if (c->IsDisconnected())
1425  {
1426  stat1[idx] = 0;
1427  nclosed1++;
1428 
1429  //DisconnectSlot(idx);
1430  }
1431  if (c->IsConnecting())
1432  {
1433  stat1[idx] = 1;
1434  nconnecting1++;
1435  }
1436  if (c->IsConnected())
1437  {
1438  stat1[idx] = 2;
1439  nconnected1++;
1440 
1441  if (c->IsConfigured())
1442  {
1443  stat1[idx] = 3;
1444  nconfigured++;
1445  }
1446  }
1447 
1448  cnt++;
1449  }
1450 
1451  // ----- Event builder -----
1452 
1453  stat2[idx] = 0; // disconnected
1454  if (!runs)
1455  continue;
1456 
1457  if (IsConnecting(idx))
1458  {
1459  nconnecting2++;
1460  stat2[idx] = 1; // connecting
1461  }
1462 
1463  if (IsConnected(idx))
1464  {
1465  nconnected2++;
1466  stat2[idx] = 8; // connected
1467  }
1468  }
1469 
1470  // ===== Send connection status via dim =====
1471 
1472  if (fStatus1!=stat1 || fStatus2!=stat2 || fStatusT!=runs)
1473  {
1474  fStatus1 = stat1;
1475  fStatus2 = stat2;
1476  fStatusT = runs;
1477  UpdateConnectionStatus(stat1, stat2, runs);
1478  }
1479 
1480  // ===== Return connection status =====
1481 
1482  // Keep the state during reconnection (theoretically, can only be WritingData)
1483 /* if (fReconnectionList.size()>0)
1484  {
1485  bool isnew = true;
1486  for (auto it=fReconnectionList.begin(); it!=fReconnectionList.end(); it++)
1487  if (it->state>0)
1488  {
1489  isnew = false;
1490  break;
1491  }
1492 
1493  if (isnew)
1494  {
1495  for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
1496  it->second->Cmd(FAD::kCmdBusyOn, true); // continously on
1497  }
1498 
1499  // Loop over all scheduled re-connections
1500  for (auto it=fReconnectionList.begin(); it!=fReconnectionList.end(); it++)
1501  {
1502  if (ProcessReconnection(it))
1503  continue;
1504 
1505  const lock_guard<mutex> guard(fMutexReconnect);
1506  fReconnectionList.erase(it);
1507  }
1508 
1509  if (fReconnectionList.size()==0)
1510  {
1511  for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
1512  it->second->Cmd(FAD::kCmdBusyOff, false);
1513  }
1514 
1515  return T::GetCurrentState();
1516  }
1517 */
1518  // fadctrl: Always connecting if not disabled
1519  // event builder:
1520  if (nconnecting1==0 && nconnected1>0 && nconnected2==nconnected1)
1521  {
1522  if (T::GetCurrentState()==FAD::State::kConfiguring1)
1523  {
1524  // Wait until the configuration commands to all boards
1525  // have been sent and achknowledged
1526  for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
1527  if (!it->second->IsTxQueueEmpty())
1529 
1530  // Note that if there are less than 40 boards, this
1531  // can be so fast that the single trigger still
1532  // comes to early, and a short watiting is necessary :(
1533 
1534  // Now we can sent the trigger
1535  for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
1536  it->second->Cmd(FAD::kCmdSingleTrigger);
1537 
1539  }
1540 
1541  // If all boards are configured and we are configuring
1542  // go on and start the FADs
1543  if (T::GetCurrentState()==FAD::State::kConfiguring2)
1544  {
1545  // If not all boards have yet received the proper
1546  // configuration
1547  if (nconfigured!=nconnected1)
1549 
1550  // FIXME: Distinguish between not all boards have received
1551  // the configuration and the configuration is not consistent
1552 
1553  for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
1554  {
1555  ConnectionFAD &fad = *it->second;
1556 
1557  // Make sure that after switching on the trigger line
1558  // there needs to be some waiting before all boards
1559  // can be assumed to be listening
1561  fad.Cmd(FAD::kCmdSocket, false);
1562  //fad.Cmd(FAD::kCmdTriggerLine, true);
1563  if (fTargetConfig->second.fContinousTrigger)
1564  fad.Cmd(FAD::kCmdContTrigger, true);
1565  fad.Cmd(FAD::kCmdBusyOn, false); // continously on
1566 
1567  // FIXME: How do we find out when the FADs
1568  // successfully enabled the trigger lines?
1569  }
1570 
1571 // const lock_guard<mutex> guard(fMutexReconnect);
1572 // fReconnectionList.clear();
1573 
1575  }
1576 
1577  if (T::GetCurrentState()==FAD::State::kConfiguring3)
1578  {
1579  // Wait until the configuration commands to all boards
1580  // have been sent and achknowledged
1581  for (BoardList::iterator it=fBoards.begin(); it!=fBoards.end(); it++)
1582  if (!it->second->IsTxQueueEmpty())
1584 
1585  return FAD::State::kConfigured;
1586  }
1587 
1588  if (T::GetCurrentState()==FAD::State::kConfigured)
1589  {
1590  // Stay in Configured as long as we have a valid
1591  // configuration and the run has not yet been started
1592  // (means the the event builder has received its
1593  // first event)
1594  if (IsRunWaiting() && nconfigured==nconnected1)
1595  return FAD::State::kConfigured;
1596 
1597  if (!IsRunWaiting())
1598  T::Message("Run successfully started... first data received.");
1599  if (nconfigured!=nconnected1)
1600  T::Message("Configuration of some boards changed.");
1601  }
1602 
1603  // FIXME: Rename WritingData to TakingData
1604  return IsRunInProgress() ? FAD::State::kRunInProgress : FAD::State::kConnected;
1605  }
1606 
1607  if (nconnecting1>0 || nconnecting2>0 || nconnected1!=nconnected2)
1608  return FAD::State::kConnecting;
1609 
1610  // nconnected1 == nconnected2 == 0
1611  return runs ? FAD::State::kDisconnected : FAD::State::kOffline;
1612  }
bool IsDisconnected() const
Definition: Connection.h:144
BoardList fBoards
Definition: fadctrl.cc:555
vector< uint8_t > fStatus1
Definition: fadctrl.cc:1395
bool IsConnected() const
Definition: Connection.h:145
bool IsConnecting() const
Definition: Connection.h:146
bool IsRunInProgress() const
bool IsConnected(int i) const
Configs::const_iterator fTargetConfig
Definition: fadctrl.cc:1995
vector< uint8_t > fStatus2
Definition: fadctrl.cc:1396
void UpdateConnectionStatus(const vector< uint8_t > &stat1, const vector< uint8_t > &stat2, bool thread)
Definition: fadctrl.cc:1645
bool IsConfigured() const
Definition: fadctrl.cc:530
void Cmd(FAD::Enable cmd, bool on=true)
Definition: fadctrl.cc:371
bool IsConnecting(int i) const

+ Here is the call graph for this function: