NDS3  1.0.0
API reference manual
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
stateMachineImpl.h
1 /*
2  * Nominal Device Support v3 (NDS3)
3  *
4  * Copyright (c) 2015 Cosylab d.d.
5  *
6  * For more information about the license please refer to the license.txt
7  * file included in the distribution.
8  */
9 
10 #ifndef NDSSTATEMACHINEIMPL_H
11 #define NDSSTATEMACHINEIMPL_H
12 
13 #include <thread>
14 #include "nds3/definitions.h"
15 #include "nds3/impl/nodeImpl.h"
16 
17 namespace nds
18 {
19 
20 template <typename T> class PVDelegateInImpl;
21 
28 {
29 public:
30 
68  StateMachineImpl(bool bAsync,
69  stateChange_t switchOnFunction,
70  stateChange_t switchOffFunction,
71  stateChange_t startFunction,
72  stateChange_t stopFunction,
73  stateChange_t recoverFunction,
74  allowChange_t allowStateChangeFunction);
75 
77 
95  void setState(const state_t newState);
96 
102  virtual state_t getLocalState() const;
103 
111  virtual void getGlobalState(timespec* pTimestamp, state_t* pState) const;
112 
120  bool canChange(const state_t newState) const;
121 
130  bool isAllowedTransition(const state_t currentState, const state_t newState) const;
131 
139  bool isIntermediateState(const state_t newState) const;
140 
148  parameters_t commandSetState(const state_t state, const parameters_t& parameters);
149 
155  virtual void initialize(FactoryBaseImpl& controlSystem);
156 
160  virtual void deinitialize();
161 
162 
163 protected:
164 
172  void executeTransition(const state_t initialState, const state_t finalState, stateChange_t transitionFunction);
173 
182  void executeTransitionThread(const state_t initialState, const state_t finalState, stateChange_t transitionFunction);
183 
190  void readLocalState(timespec* pTimestamp, std::int32_t* pValue);
191 
198  void writeLocalState(const timespec& timestamp, const std::int32_t& value);
199 
206  void readGlobalState(timespec* pTimestamp, std::int32_t* pValue);
207 
214  static std::string getStateName(const state_t state);
215 
216  bool m_bAsync;
217 
218  std::thread m_transitionThread;
220 
222  timespec m_stateTimestamp;
223 
230 
231  std::shared_ptr<PVDelegateInImpl<std::int32_t> > m_pGetStatePV;
232 
233 };
234 
235 
236 }
237 
238 #endif // NDSSTATEMACHINEIMPL_H
std::shared_ptr< PVDelegateInImpl< std::int32_t > > m_pGetStatePV
Delegate PV to which the local state change is pushed.
Definition: stateMachineImpl.h:231
void executeTransitionThread(const state_t initialState, const state_t finalState, stateChange_t transitionFunction)
Thread function executed for asynchronous state machines. Calls executeTransition() and catches its e...
timespec m_stateTimestamp
The timestamp of the last local state change.
Definition: stateMachineImpl.h:222
bool m_bAsync
If true then the state transitions happen in another thread (held by m_transitionThread) ...
Definition: stateMachineImpl.h:216
virtual void getGlobalState(timespec *pTimestamp, state_t *pState) const
Return the global state. The global state takes into consideration the states of the children of the ...
stateChange_t m_start
Delegate function for the start transition.
Definition: stateMachineImpl.h:226
void readGlobalState(timespec *pTimestamp, std::int32_t *pValue)
Delegate function called to read the current global state.
bool canChange(const state_t newState) const
Check if the transition to the new state is legal and is not denied by the delegate function...
stateChange_t m_switchOn
Delegate function for the switch-on transition.
Definition: stateMachineImpl.h:224
This is the base class for objects that interact with specific control systems and has to be allocate...
Definition: factoryBaseImpl.h:51
void executeTransition(const state_t initialState, const state_t finalState, stateChange_t transitionFunction)
Execute the state transition. May be called from a separate thread.
virtual void initialize(FactoryBaseImpl &controlSystem)
Register the PVs and set the local state to state_t::off.
StateMachineImpl(bool bAsync, stateChange_t switchOnFunction, stateChange_t switchOffFunction, stateChange_t startFunction, stateChange_t stopFunction, stateChange_t recoverFunction, allowChange_t allowStateChangeFunction)
Construct the StateMachineImpl class.
void writeLocalState(const timespec &timestamp, const std::int32_t &value)
Delegate function called to set the local state.
stateChange_t m_recover
Delegate function for the recover transition.
Definition: stateMachineImpl.h:228
Represents a node (channel or channelGroup in the old NDS) which can contain other nodes...
Definition: nodeImpl.h:29
static std::string getStateName(const state_t state)
Returns the human readable name for the requested state.
bool isAllowedTransition(const state_t currentState, const state_t newState) const
Returns true if the transition between two states is legal (does not check the delegate function allo...
std::function< bool(const state_t, const state_t, const state_t)> allowChange_t
Definition for a function called to allow or deny a state transition.
Definition: definitions.h:271
virtual state_t getLocalState() const
Return the local state. The local state does not reflect the state of the children.
std::mutex m_lockTransitionThread
Lock the access to m_transitionThread.
Definition: stateMachineImpl.h:219
allowChange_t m_allowChange
Delegate function for the Allow-Change function.
Definition: stateMachineImpl.h:229
stateChange_t m_stop
Delegate function for the stop transition.
Definition: stateMachineImpl.h:227
std::function< void()> stateChange_t
Definition for the function executed during the state transition.
Definition: definitions.h:254
state_t m_localState
The local state.
Definition: stateMachineImpl.h:221
Defines all the enumeration and common types used across the NDS library.
virtual void deinitialize()
Wait for pending transitions to terminate and deregister the PVs.
void setState(const state_t newState)
Instructs the state machine to start the transition to another state.
Implements the state machine.
Definition: stateMachineImpl.h:27
stateChange_t m_switchOff
Delegate function for the switch-off transition.
Definition: stateMachineImpl.h:225
bool isIntermediateState(const state_t newState) const
Returns true if the specified state is an intermediate state (not settable directly with setState())...
std::vector< std::string > parameters_t
List of strings passed as parameters to nodes' commands.
Definition: definitions.h:204
void readLocalState(timespec *pTimestamp, std::int32_t *pValue)
Delegate function called to read the local state.
state_t
Available states, ordered by priority (lowest to higher).
Definition: definitions.h:36
parameters_t commandSetState(const state_t state, const parameters_t &parameters)
Delegate function for the commands that set the node's state.
std::thread m_transitionThread
Thread that is used to execute the state transition when bAsync is true in the constructor.
Definition: stateMachineImpl.h:218