QP/C
Data Structures | Defines | Typedefs | Enumerations | Functions
qep.h File Reference

Public QEP/C interface. More...

#include "qevent.h"

Go to the source code of this file.

Data Structures

struct  QFsm
 Finite State Machine. More...

Defines

#define Q_STATE_CAST(handler_)   ((QStateHandler)(handler_))
 Perform cast to QStateHandler.
#define QFsm_ctor(me_, initial_)   ((me_)->state = (initial_))
 Protected "constructor" of a FSM.
#define QHsm_ctor(me_, initial_)   ((me_)->state = (initial_))
 protected "constructor" of a HSM. Performs the first step of HSM initialization by assigning the initial pseudostate to the currently active state of the state machine.
#define Q_RET_IGNORED   ((QState)1)
 Value returned by a non-hierarchical state-handler function when it ignores (does not handle) the event.
#define Q_IGNORED()   (Q_RET_IGNORED)
 The macro returned from a non-hierarchical state-handler function when it ignores (does not handle) the event.
#define Q_RET_HANDLED   ((QState)0)
 Value returned by a state-handler function when it handles the event.
#define Q_HANDLED()   (Q_RET_HANDLED)
 Value returned by a state-handler function when it handles the event.
#define Q_RET_TRAN   ((QState)2)
 Value returned by a state-handler function when it takes a regular state transition.
#define Q_TRAN(target_)   (((QFsm *)me)->state = Q_STATE_CAST(target_), Q_RET_TRAN)
 Designates a target for an initial or regular transition. Q_TRAN() can be used both in the FSMs and HSMs.
#define Q_RET_SUPER   ((QState)3)
 Value returned by a state-handler function when it cannot handle the event.
#define Q_SUPER(super_)   (((QHsm *)me)->state = Q_STATE_CAST(super_), Q_RET_SUPER)
 Designates the superstate of a given state in an HSM.
#define Q_EVENT_CAST(class_)   ((class_ const *)e)
 Perform downcast of an event onto a subclass of QEvent class_.

Typedefs

typedef uint8_t QState
 Type returned from a state-handler function.
typedef QState(* QStateHandler )(void *me, QEvent const *e)
 pointer to state-handler function
typedef struct QFsmTag QHsm
 Hierarchical State Machine.

Enumerations

enum  QReservedSignals { Q_ENTRY_SIG = 1, Q_EXIT_SIG, Q_INIT_SIG, Q_USER_SIG }
 QEP reserved signals. More...

Functions

char_t const Q_ROM *Q_ROM_VAR QEP_getVersion (void)
 obtain the current QEP version number string
void QFsm_init (QFsm *me, QEvent const *e)
 Performs the second step of FSM initialization by triggering the top-most initial transition.
void QFsm_dispatch (QFsm *me, QEvent const *e)
 Dispatches an event to a FSM.
void QHsm_init (QHsm *me, QEvent const *e)
 Performs the second step of HSM initialization by triggering the top-most initial transition.
void QHsm_dispatch (QHsm *me, QEvent const *e)
 Dispatches an event to a HSM.
uint8_t QHsm_isIn (QHsm *me, QStateHandler state)
 Tests if a given state is part of the current active state configuratioin.
QState QHsm_top (void *me, QEvent const *e)
 the top-state.

Detailed Description

Public QEP/C interface.

This header file must be included, perhaps indirectly, in all modules (*.c files) that use QEP/C

Definition in file qep.h.


Define Documentation

#define Q_EVENT_CAST (   class_)    ((class_ const *)e)

Perform downcast of an event onto a subclass of QEvent class_.

This macro encapsulates the downcast of QEvent pointers, which violates MISRA-C 2004 rule 11.4(advisory). This macro helps to localize this deviation.

Definition at line 275 of file qep.h.

#define Q_HANDLED ( )    (Q_RET_HANDLED)

Value returned by a state-handler function when it handles the event.

You call that macro after the return statement (return Q_HANDLED();) Q_HANDLED() can be used both in the FSMs and HSMs.

Definition at line 241 of file qep.h.

#define Q_IGNORED ( )    (Q_RET_IGNORED)

The macro returned from a non-hierarchical state-handler function when it ignores (does not handle) the event.

You call that macro after the return statement (return Q_IGNORED();)

Definition at line 226 of file qep.h.

Referenced by QHsm_top().

#define Q_STATE_CAST (   handler_)    ((QStateHandler)(handler_))

Perform cast to QStateHandler.

This macro encapsulates the cast of a specific state handler function pointer to QStateHandler, which violates MISRA-C 2004 rule 11.4(advisory). This macro helps to localize this deviation.

Definition at line 65 of file qep.h.

Referenced by QFsm_init().

#define Q_SUPER (   super_)    (((QHsm *)me)->state = Q_STATE_CAST(super_), Q_RET_SUPER)

Designates the superstate of a given state in an HSM.

Definition at line 265 of file qep.h.

#define Q_TRAN (   target_)    (((QFsm *)me)->state = Q_STATE_CAST(target_), Q_RET_TRAN)

Designates a target for an initial or regular transition. Q_TRAN() can be used both in the FSMs and HSMs.

Definition at line 253 of file qep.h.

#define QFsm_ctor (   me_,
  initial_ 
)    ((me_)->state = (initial_))

Protected "constructor" of a FSM.

Performs the first step of FSM initialization by assigning the initial pseudostate to the currently active state of the state machine.

Note:
Must be called only by the "constructors" of the derived state machines.
Must be called only ONCE before QFsm_init().

The following example illustrates how to invoke QFsm_ctor() in the "constructor" of a derived state machine:

void QBomb_ctor(QBomb *me) {
    QFsm_ctor(&me->super, (QStateHandler)&QBomb_initial);/* superclass ctor */
}

Definition at line 100 of file qep.h.

#define QHsm_ctor (   me_,
  initial_ 
)    ((me_)->state = (initial_))

protected "constructor" of a HSM. Performs the first step of HSM initialization by assigning the initial pseudostate to the currently active state of the state machine.

Note:
Must be called only by the "constructors" of the derived state machines.
Must be called before QHsm_init().

The following example illustrates how to invoke QHsm_ctor() in the "constructor" of a derived state machine:

void QCalc_ctor(QCalc *me) {
    QHsm_ctor(&me->super, (QStateHandler)&QCalc_initial);
}
See also:
QFsm_ctor

Definition at line 166 of file qep.h.


Typedef Documentation

typedef struct QFsmTag QHsm

Hierarchical State Machine.

QHsm represents a Hierarchical Finite State Machine (HSM) with full support for hierarchical nesting of states, entry/exit actions, and initial transitions in any composite state.

Note:
QHsm is not intended to be instantiated directly, but rather serves as the base structure for derivation of state machines in the application code.

The following example illustrates how to derive a state machine structure from QHsm. Please note that the QHsm member super is defined as the FIRST member of the derived struct.

typedef struct QCalcTag {
    QHsm super;                                        /* derives from QHsm */
    double operand1;
    double operand2;
    char display[DISP_WIDTH + 1];
    uint8_t len;
    uint8_t opKey;
} QCalc;
See also:
Encapsulation and Single Inheritance in C

Definition at line 148 of file qep.h.


Enumeration Type Documentation

QEP reserved signals.

Enumerator:
Q_ENTRY_SIG 

signal for coding entry actions

Q_EXIT_SIG 

signal for coding exit actions

Q_INIT_SIG 

signal for coding initial transitions

Q_USER_SIG 

first signal that can be used in user applications

Definition at line 279 of file qep.h.


Function Documentation

char_t const Q_ROM* Q_ROM_VAR QEP_getVersion ( void  )

obtain the current QEP version number string

Returns:
version of the QEP as a constant 6-character string of the form x.y.zz, where x is a 1-digit major version number, y is a 1-digit minor version number, and zz is a 2-digit release number.

Definition at line 45 of file qep.c.

References QP_VERSION.

void QFsm_dispatch ( QFsm me,
QEvent const *  e 
)

Dispatches an event to a FSM.

Processes one event at a time in Run-to-Completion fashion. The argument me is the pointer the state machine structure derived from QFsm. The argument e is a constant pointer the QEvent or a structure derived from QEvent.

Note:
Must be called after the "constructor" QFsm_ctor() and QFsm_init().
See also:
example for QFsm_init()
Encapsulation and Single Inheritance in C

Definition at line 37 of file qfsm_dis.c.

References Q_ENTRY_SIG, Q_EXIT_SIG, Q_RET_HANDLED, Q_RET_TRAN, QEP_TRIG_, QS_BEGIN_, QS_CRIT_STAT_, QS_END_, QS_FUN_, QS_OBJ_, QS_QEP_IGNORED, QS_QEP_INTERN_TRAN, QS_QEP_TRAN, QS_smObj_, QS_TIME_, QEvent::sig, and QFsm::state.

void QFsm_init ( QFsm me,
QEvent const *  e 
)

Performs the second step of FSM initialization by triggering the top-most initial transition.

Parameters:
mepointer the state machine structure derived from QFsm
econstant pointer the QEvent or a structure derived from QEvent
Note:
Must be called only ONCE after the "constructor" QFsm_ctor().

The following example illustrates how to initialize a FSM, and dispatch events to it:

#include "qep.h"                                  /* QEP/C public interface */
#include "qbomb.h"                           /* QBomb FSM derived from QFsm */

static QBomb l_qbomb;                           /* an instance of QBomb FSM */

int main() {
    QBombInitEvt ie;                  /* initialization event for QBomb FSM */

    QBomb_ctor(&l_qbomb);   /* QBomb FSM "constructor" invokes QFsm_ctor_() */

    /* set the initialization event ie */

    QFsm_init((QFsm *)&l_qbomb, &ie);         /* trigger initial transition */

    for (;;) {                                                /* event loop */
        QEvent e;
        . . .
        /* wait for the next event and assign it to the event object e */
        . . .
        QFsm_dispatch((QFsm *)&l_qbomb, &e);       /* dispatch e to l_qbomb */
    }
    return 0;
}

Definition at line 40 of file qfsm_ini.c.

References Q_ALLEGE, Q_ENTRY_SIG, Q_RET_TRAN, Q_STATE_CAST, QEP_TRIG_, QS_BEGIN_, QS_CRIT_STAT_, QS_END_, QS_FUN_, QS_OBJ_, QS_QEP_INIT_TRAN, QS_QEP_STATE_INIT, QS_smObj_, and QS_TIME_.

void QHsm_dispatch ( QHsm me,
QEvent const *  e 
)

Dispatches an event to a HSM.

Processes one event at a time in Run-to-Completion fashion.

Parameters:
meis the pointer the state machine structure derived from QHsm.
eis a constant pointer the QEvent or a structure derived from QEvent.
Note:
Must be called after the "constructor" QHsm_ctor() and QHsm_init().
See also:
example for QHsm_init()
Encapsulation and Single Inheritance in C

Definition at line 40 of file qhsm_dis.c.

References Q_ASSERT, Q_EXIT_SIG, Q_INIT_SIG, Q_RET_HANDLED, Q_RET_IGNORED, Q_RET_SUPER, Q_RET_TRAN, QEP_EMPTY_SIG_, QEP_ENTER_, QEP_EXIT_, QEP_MAX_NEST_DEPTH_, QEP_TRIG_, QS_BEGIN_, QS_CRIT_STAT_, QS_END_, QS_FUN_, QS_OBJ_, QS_QEP_DISPATCH, QS_QEP_IGNORED, QS_QEP_INTERN_TRAN, QS_QEP_STATE_EXIT, QS_QEP_STATE_INIT, QS_QEP_TRAN, QS_smObj_, and QS_TIME_.

void QHsm_init ( QHsm me,
QEvent const *  e 
)

Performs the second step of HSM initialization by triggering the top-most initial transition.

Parameters:
mepointer the state machine structure derived from QHsm
econstant pointer the QEvent or a structure derived from QEvent
Note:
Must be called only ONCE after the "constructor" QHsm_ctor().

The following example illustrates how to initialize a HSM, and dispatch events to it:

#include "qep.h"                                  /* QEP/C public interface */
#include "qcalc.h"                           /* QCalc HSM derived from QHsm */

static QCalc l_qcalc;                           /* an instance of QCalc HSM */

int main() {
    QCalc_ctor(&l_qcalc);   /* QCalc HSM "constructor" invokes QHsm_ctor_() */

    QHsm_init((QHsm *)&l_qcalc, (QEvent *)0); /* trigger initial transition */

    for (;;) {                                                /* event loop */
        QEvent e;
        . . .
        /* wait for the next event and assign it to the event object e */
        . . .
        QHsm_dispatch((QHsm *)&l_qcalc, (QEvent *)&e);        /* dispatch e */
    }
    return 0;
}

Definition at line 40 of file qhsm_ini.c.

References Q_ALLEGE, Q_ASSERT, Q_INIT_SIG, Q_RET_TRAN, QEP_EMPTY_SIG_, QEP_ENTER_, QEP_MAX_NEST_DEPTH_, QEP_TRIG_, QHsm_top(), QS_BEGIN_, QS_CRIT_STAT_, QS_END_, QS_FUN_, QS_OBJ_, QS_QEP_INIT_TRAN, QS_QEP_STATE_INIT, QS_smObj_, and QS_TIME_.

uint8_t QHsm_isIn ( QHsm me,
QStateHandler  state 
)

Tests if a given state is part of the current active state configuratioin.

Parameters:
meis the pointer the state machine structure derived from QHsm.
stateis a pointer to the state handler function, e.g., &QCalc_on.

Definition at line 37 of file qhsm_in.c.

References Q_RET_IGNORED, QEP_EMPTY_SIG_, and QEP_TRIG_.

QState QHsm_top ( void *  me,
QEvent const *  e 
)

the top-state.

QHsm_top() is the ultimate root of state hierarchy in all HSMs derived from QHsm. This state handler always returns (QState)0, which means that it "handles" all events.

See also:
Example of the QCalc_on() state handler for Q_INIT().

Definition at line 37 of file qhsm_top.c.

References Q_IGNORED.

Referenced by QHsm_init().