Initial commit
This commit is contained in:
		
							
								
								
									
										92
									
								
								test-bench/test4/src/app/statemachine04a.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								test-bench/test4/src/app/statemachine04a.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,92 @@
 | 
			
		||||
#include <cassert>
 | 
			
		||||
#include "trace/trace.h"
 | 
			
		||||
#include "events/evrestart.h"
 | 
			
		||||
#include "statemachine04a.h"
 | 
			
		||||
 | 
			
		||||
StateMachine04a::StateMachine04a()
 | 
			
		||||
 : pNeighbour_(nullptr)
 | 
			
		||||
{
 | 
			
		||||
	currentState_ = STATE_INITIAL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
StateMachine04a::~StateMachine04a()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Sets the association to the neighbour state machine.
 | 
			
		||||
 * Call this method prior the start the state machine.
 | 
			
		||||
 */
 | 
			
		||||
void StateMachine04a::setNeighbour(XFBehavior * pNeighbour)
 | 
			
		||||
{
 | 
			
		||||
	pNeighbour_ = pNeighbour;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Returns reference to neighbour state machine.
 | 
			
		||||
 */
 | 
			
		||||
XFBehavior * StateMachine04a::getNeighbour() const
 | 
			
		||||
{
 | 
			
		||||
	assert(pNeighbour_);
 | 
			
		||||
	return pNeighbour_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Implements state machine behavior.
 | 
			
		||||
 */
 | 
			
		||||
XFEventStatus StateMachine04a::processEvent()
 | 
			
		||||
{
 | 
			
		||||
	eEventStatus eventStatus = XFEventStatus::Unknown;
 | 
			
		||||
 | 
			
		||||
	switch (currentState_)
 | 
			
		||||
	{
 | 
			
		||||
	case STATE_INITIAL:
 | 
			
		||||
		{
 | 
			
		||||
			if (getCurrentEvent()->getEventType() == XFEvent::Initial)
 | 
			
		||||
			{
 | 
			
		||||
				GEN(XFDefaultTransition());
 | 
			
		||||
 | 
			
		||||
				currentState_ = STATE_WAIT;
 | 
			
		||||
				eventStatus = XFEventStatus::Consumed;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
	case STATE_WAIT:
 | 
			
		||||
		{
 | 
			
		||||
			if (getCurrentEvent()->getEventType() == XFEvent::DefaultTransition)
 | 
			
		||||
			{
 | 
			
		||||
				{
 | 
			
		||||
					Trace::out("SM04a: Wait");
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
                scheduleTimeout(Timeout_WAIT_id, 4500);
 | 
			
		||||
 | 
			
		||||
				currentState_ = STATE_SEND_RESTART;
 | 
			
		||||
				eventStatus = XFEventStatus::Consumed;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
	case STATE_SEND_RESTART:
 | 
			
		||||
		{
 | 
			
		||||
			if (getCurrentEvent()->getEventType() == XFEvent::Timeout &&
 | 
			
		||||
				getCurrentTimeout()->getId() == Timeout_WAIT_id)
 | 
			
		||||
			{
 | 
			
		||||
				{
 | 
			
		||||
					Trace::out("SM04a: Send restart to SM04b");
 | 
			
		||||
					getNeighbour()->GEN(evRestart());
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				GEN(XFDefaultTransition());
 | 
			
		||||
 | 
			
		||||
				currentState_ = STATE_WAIT;
 | 
			
		||||
				eventStatus = XFEventStatus::Consumed;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
	default:
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return eventStatus;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										54
									
								
								test-bench/test4/src/app/statemachine04a.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								test-bench/test4/src/app/statemachine04a.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
#ifndef STATEMACHINE04A_H
 | 
			
		||||
#define STATEMACHINE04A_H
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include "xf/behavior.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \ingroup test04
 | 
			
		||||
 *
 | 
			
		||||
 * Implements a state machine sending its neighbour state machine
 | 
			
		||||
 * an \a evRestart event every four seconds.
 | 
			
		||||
 *
 | 
			
		||||
 * Following you will find the state machine implemented by StateMachine04a:
 | 
			
		||||
 * \image html state-machine04a.png "State Machine implemented by StateMachine04"
 | 
			
		||||
 */
 | 
			
		||||
class StateMachine04a : public XFBehavior
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	StateMachine04a();					///< Constructor
 | 
			
		||||
    ~StateMachine04a() override;
 | 
			
		||||
 | 
			
		||||
    void setNeighbour(XFBehavior * pNeighbour);
 | 
			
		||||
    XFBehavior * getNeighbour() const;
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
    XFEventStatus processEvent() override;
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
	/**
 | 
			
		||||
	 * Timeout identifier(s) for this state machine
 | 
			
		||||
	 */
 | 
			
		||||
	typedef enum
 | 
			
		||||
	{
 | 
			
		||||
		Timeout_WAIT_id = 1			///< Timeout id for WAIT
 | 
			
		||||
	} eTimeoutId;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Enumeration used to have a unique identifier for every
 | 
			
		||||
	 * state in the state machine.
 | 
			
		||||
	 */
 | 
			
		||||
	typedef enum
 | 
			
		||||
	{
 | 
			
		||||
		STATE_UNKOWN = 0,			///< Unknown state
 | 
			
		||||
		STATE_INITIAL = 1,			///< Initial state
 | 
			
		||||
		STATE_WAIT = 2,				///< Wait state
 | 
			
		||||
		STATE_SEND_RESTART = 3		///< State sending an restart event
 | 
			
		||||
	} eMainState;
 | 
			
		||||
 | 
			
		||||
    eMainState currentState_;		///< Attribute indicating currently active state
 | 
			
		||||
 | 
			
		||||
    XFBehavior * pNeighbour_;		///< Association to the neigbour state machine
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // STATEMACHINE04A_H
 | 
			
		||||
							
								
								
									
										85
									
								
								test-bench/test4/src/app/statemachine04b.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								test-bench/test4/src/app/statemachine04b.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,85 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include "trace/trace.h"
 | 
			
		||||
#include "events/evrestart.h"
 | 
			
		||||
#include "statemachine04b.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
StateMachine04b::StateMachine04b()
 | 
			
		||||
{
 | 
			
		||||
	currentState_ = STATE_INITIAL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
StateMachine04b::~StateMachine04b()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Implements state machine behavior.
 | 
			
		||||
 */
 | 
			
		||||
XFEventStatus StateMachine04b::processEvent()
 | 
			
		||||
{
 | 
			
		||||
	eEventStatus eventStatus = XFEventStatus::Unknown;
 | 
			
		||||
 | 
			
		||||
	// Every time receiving an evRestart event, reset state machine
 | 
			
		||||
	if (isRestartEvent())
 | 
			
		||||
	{
 | 
			
		||||
		// Remove any previous WAIT timeouts
 | 
			
		||||
        unscheduleTimeout(Timeout_WAIT_id);
 | 
			
		||||
 | 
			
		||||
		currentState_ = STATE_INITIAL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch (currentState_)
 | 
			
		||||
	{
 | 
			
		||||
	case STATE_INITIAL:
 | 
			
		||||
		{
 | 
			
		||||
			if (isRestartEvent() ||
 | 
			
		||||
				getCurrentEvent()->getEventType() == XFEvent::Initial)
 | 
			
		||||
			{
 | 
			
		||||
				{
 | 
			
		||||
					timeout_ = 0;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				GEN(XFDefaultTransition());
 | 
			
		||||
 | 
			
		||||
				currentState_ = STATE_WAIT;
 | 
			
		||||
				eventStatus = XFEventStatus::Consumed;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
	case STATE_WAIT:
 | 
			
		||||
		{
 | 
			
		||||
			if (getCurrentEvent()->getEventType() == XFEvent::DefaultTransition ||
 | 
			
		||||
				isTimeoutEvent(Timeout_WAIT_id))
 | 
			
		||||
			{
 | 
			
		||||
				if (isTimeoutEvent(Timeout_WAIT_id))
 | 
			
		||||
				{
 | 
			
		||||
					Trace::out("SM04b: Timeout %d", ++timeout_);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
                scheduleTimeout(Timeout_WAIT_id, 1000);
 | 
			
		||||
 | 
			
		||||
				currentState_ = STATE_WAIT;
 | 
			
		||||
				eventStatus = XFEventStatus::Consumed;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
	default:
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return eventStatus;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool StateMachine04b::isTimeoutEvent(int timeoutId)
 | 
			
		||||
{
 | 
			
		||||
	return (getCurrentEvent()->getEventType() == XFEvent::Timeout &&
 | 
			
		||||
			getCurrentTimeout()->getId() == timeoutId);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool StateMachine04b::isRestartEvent() const
 | 
			
		||||
{
 | 
			
		||||
	return (getCurrentEvent()->getEventType() == XFEvent::Event &&
 | 
			
		||||
			getCurrentEvent()->getId() == EventId::evRestart);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								test-bench/test4/src/app/statemachine04b.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								test-bench/test4/src/app/statemachine04b.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
#ifndef STATEMACHINE04B_H
 | 
			
		||||
#define STATEMACHINE04B_H
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include "xf/behavior.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \ingroup test04
 | 
			
		||||
 *
 | 
			
		||||
 * Implements a state machine timeouting every second. It shows how many times
 | 
			
		||||
 * the timeout has expired (using a #timeout_ attribute). The state machine can
 | 
			
		||||
 * be reset by sending an \a evRestart event.
 | 
			
		||||
 *
 | 
			
		||||
 * Following you will find the state machine implemented by StateMachine04b:
 | 
			
		||||
 * \image html state-machine04b.png "State Machine implemented by StateMachine04"
 | 
			
		||||
 */
 | 
			
		||||
class StateMachine04b : public XFBehavior
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	StateMachine04b();
 | 
			
		||||
    ~StateMachine04b() override;
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
    XFEventStatus processEvent() override;
 | 
			
		||||
 | 
			
		||||
	bool isTimeoutEvent(int timeoutId);		///< Returns true in case state machine is currently processing a timeout.
 | 
			
		||||
	bool isRestartEvent() const;			///< Returns true in case state machine is currently processing an \a evRestart event.
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
	/**
 | 
			
		||||
	 * Timeout identifier(s) for this state machine
 | 
			
		||||
	 */
 | 
			
		||||
	typedef enum
 | 
			
		||||
	{
 | 
			
		||||
		Timeout_WAIT_id = 1			///< Timeout id for WAIT
 | 
			
		||||
	} eTimeoutId;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Enumeration used to have a unique identifier for every
 | 
			
		||||
	 * state in the state machine.
 | 
			
		||||
	 */
 | 
			
		||||
	typedef enum
 | 
			
		||||
	{
 | 
			
		||||
		STATE_UNKOWN = 0,			///< Unkown state
 | 
			
		||||
		STATE_INITIAL = 1,			///< Initial state
 | 
			
		||||
		STATE_WAIT = 2				///< Wait for timeout state
 | 
			
		||||
	} eMainState;
 | 
			
		||||
 | 
			
		||||
    eMainState currentState_;		///< Attribute indicating currently active state
 | 
			
		||||
    int timeout_;					///< Timeout attribute counting rised timeouts in state machine
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // STATEMACHINE04B_H
 | 
			
		||||
							
								
								
									
										39
									
								
								test-bench/test4/src/app/testfactory04.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								test-bench/test4/src/app/testfactory04.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
#include "xf/xf.h"
 | 
			
		||||
#include "trace/trace.h"
 | 
			
		||||
#include "testfactory04.h"
 | 
			
		||||
 | 
			
		||||
void Factory_initialize()
 | 
			
		||||
{
 | 
			
		||||
    TestFactory04::initialize();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Factory_build()
 | 
			
		||||
{
 | 
			
		||||
    TestFactory04::build();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
StateMachine04a TestFactory04::task01_;
 | 
			
		||||
StateMachine04b TestFactory04::task02_;
 | 
			
		||||
 | 
			
		||||
TestFactory04::TestFactory04()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
void TestFactory04::initialize()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
void TestFactory04::build()
 | 
			
		||||
{
 | 
			
		||||
	Trace::out("Starting test4...");
 | 
			
		||||
	Trace::out("---------------------");
 | 
			
		||||
 | 
			
		||||
	// Set up association(s)
 | 
			
		||||
	task01_.setNeighbour(&task02_);
 | 
			
		||||
 | 
			
		||||
	// Start state machine(s)
 | 
			
		||||
	task01_.startBehavior();
 | 
			
		||||
	task02_.startBehavior();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								test-bench/test4/src/app/testfactory04.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								test-bench/test4/src/app/testfactory04.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
#ifndef TESTFACTORY04_H
 | 
			
		||||
#define TESTFACTORY04_H
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// What is seen only by the C++ compiler
 | 
			
		||||
//
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
 | 
			
		||||
#include "statemachine04a.h"
 | 
			
		||||
#include "statemachine04b.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \ingroup test04
 | 
			
		||||
 *
 | 
			
		||||
 * Factory creating all objects used in test4.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
class TestFactory04
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    TestFactory04();                    ///< Constructor
 | 
			
		||||
 | 
			
		||||
    static void initialize();           ///< Initializes the factory
 | 
			
		||||
    static void build();                ///< Creates components and initializes relations
 | 
			
		||||
	
 | 
			
		||||
protected:
 | 
			
		||||
	static StateMachine04a task01_;		///< Instance of StateMachine04a
 | 
			
		||||
	static StateMachine04b task02_;		///< Instance of StateMachine04b
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // __cplusplus
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// What is seen by the C and C++ compiler
 | 
			
		||||
//
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif // __cplusplus
 | 
			
		||||
 | 
			
		||||
void Factory_initialize();
 | 
			
		||||
void Factory_build();
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif // __cplusplus
 | 
			
		||||
#endif // TESTFACTORY03_H
 | 
			
		||||
							
								
								
									
										24
									
								
								test-bench/test4/src/events/eventids.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								test-bench/test4/src/events/eventids.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
#ifndef EVENTIDS_H
 | 
			
		||||
#define EVENTIDS_H
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class is responsible to distribute to every event
 | 
			
		||||
 * used in the project a different event id.
 | 
			
		||||
 *
 | 
			
		||||
 * Only values grater than zero should be used
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
class EventId
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	/**
 | 
			
		||||
	 * The event identifiers available
 | 
			
		||||
	 */
 | 
			
		||||
	typedef enum
 | 
			
		||||
	{
 | 
			
		||||
		Unknown = 0,		///< Unknown event. Must not be used.
 | 
			
		||||
		evRestart = 1		///< Restart event
 | 
			
		||||
	} eEventId;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // EVENTIDS_H
 | 
			
		||||
							
								
								
									
										9
									
								
								test-bench/test4/src/events/evrestart.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								test-bench/test4/src/events/evrestart.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
#include "evrestart.h"
 | 
			
		||||
 | 
			
		||||
evRestart::evRestart() : XFCustomEvent(EventId::evRestart)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
evRestart::~evRestart()
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								test-bench/test4/src/events/evrestart.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								test-bench/test4/src/events/evrestart.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#ifndef EVRESTART_H
 | 
			
		||||
#define EVRESTART_H
 | 
			
		||||
 | 
			
		||||
#include "xf/customevent.h"
 | 
			
		||||
#include "eventids.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Restart event used to reset a state machine
 | 
			
		||||
 */
 | 
			
		||||
class evRestart : public XFCustomEvent
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	evRestart();
 | 
			
		||||
    ~evRestart() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // EVRESTART_H
 | 
			
		||||
							
								
								
									
										14
									
								
								test-bench/test4/src/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								test-bench/test4/src/main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
#include "xf/xf.h"
 | 
			
		||||
#include "app/testfactory04.h"
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
    XF::initialize(20, argc, argv);
 | 
			
		||||
 | 
			
		||||
	TestFactory04 factory;
 | 
			
		||||
 | 
			
		||||
    factory.initialize();
 | 
			
		||||
    factory.build();
 | 
			
		||||
 | 
			
		||||
    return XF::exec();
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user