/** * @file * @brief QF/C platform-independent public interface. * @ingroup qf * @cond ****************************************************************************** * Last updated for version 6.3.7 * Last updated on 2018-11-07 * * Q u a n t u m L e a P s * ------------------------ * Modern Embedded Software * * Copyright (C) 2002-2018 Quantum Leaps, LLC. All rights reserved. * * This program is open source software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Alternatively, this program may be distributed and modified under the * terms of Quantum Leaps commercial licenses, which expressly supersede * the GNU General Public License and are specifically designed for * licensees interested in retaining the proprietary status of their code. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * https://www.state-machine.com * mailto:info@state-machine.com ****************************************************************************** * @endcond */ #ifndef qf_h #define qf_h #ifndef qpset_h #include "qpset.h" #endif /****************************************************************************/ #ifndef QF_EVENT_SIZ_SIZE /*! Default value of the macro configurable value in qf_port.h */ #define QF_EVENT_SIZ_SIZE 2 #endif #if (QF_EVENT_SIZ_SIZE == 1) typedef uint8_t QEvtSize; #elif (QF_EVENT_SIZ_SIZE == 2) /*! The data type to store the block-size defined based on * the macro #QF_EVENT_SIZ_SIZE. */ /** * The dynamic range of this data type determines the maximum block * size that can be managed by the pool. */ typedef uint16_t QEvtSize; #elif (QF_EVENT_SIZ_SIZE == 4) typedef uint32_t QEvtSize; #else #error "QF_EVENT_SIZ_SIZE defined incorrectly, expected 1, 2, or 4" #endif #ifndef QF_MAX_EPOOL /*! Default value of the macro configurable value in qf_port.h */ #define QF_MAX_EPOOL 3 #endif #ifndef QF_MAX_TICK_RATE /*! Default value of the macro configurable value in qf_port.h. * Valid values: [0..15]; default 1 */ #define QF_MAX_TICK_RATE 1 #elif (QF_MAX_TICK_RATE > 15) #error "QF_MAX_TICK_RATE exceeds the maximum of 15" #endif #ifndef QF_TIMEEVT_CTR_SIZE /*! macro to override the default ::QTimeEvtCtr size. * Valid values: 1, 2, or 4; default 2 */ #define QF_TIMEEVT_CTR_SIZE 2 #endif /****************************************************************************/ struct QEQueue; /* forward declaration */ /****************************************************************************/ /*! Active Object (based on ::QHsm implementation) */ /** * @description * Active objects in QP are encapsulated state machines (each embedding an * event queue and a thread) that communicate with one another asynchronously * by sending and receiving events. Within an active object, events are * processed sequentially in a run-to-completion (RTC) fashion, while QF * encapsulates all the details of thread-safe event exchange and queuing. * @n@n * ::QActive represents an active object that uses the QHsm-style * implementation strategy for state machines. This strategy is tailored * to manual coding, but it is also supported by the QM modeling tool. * The resulting code is slower than in the ::QMsm style implementation * strategy. * * @usage * The following example illustrates how to derive an active object from * ::QActive. Please note that the ::QActive member @c super is defined as the * __first__ member of the derived struct (see @ref oop). * @include qf_qactive.c */ typedef struct QActive { QHsm super; /*!< inherits ::QHsm */ #ifdef QF_EQUEUE_TYPE /*! OS-dependent event-queue type. */ /** * @description * The type of the queue depends on the underlying operating system or * a kernel. Many kernels support "message queues" that can be adapted * to deliver QF events to the active object. Alternatively, QF provides * a native event queue implementation that can be used as well. * * @note * The native QF event queue is configured by defining the macro * #QF_EQUEUE_TYPE as ::QEQueue. */ QF_EQUEUE_TYPE eQueue; #endif #ifdef QF_OS_OBJECT_TYPE /*! OS-dependent per-thread object. */ /** * @description * This data might be used in various ways, depending on the QF port. * In some ports osObject is used to block the calling thread when * the native QF queue is empty. In other QF ports the OS-dependent * object might be used differently. */ QF_OS_OBJECT_TYPE osObject; #endif #ifdef QF_THREAD_TYPE /*! OS-dependent representation of the thread of the active object. */ /** * @description * This data might be used in various ways, depending on the QF port. * In some ports thread is used to store the thread handle. In other * ports thread can be the pointer to the Thread-Local-Storage (TLS). */ QF_THREAD_TYPE thread; #endif /*! QF priority (1..#QF_MAX_ACTIVE) of this active object. */ uint8_t prio; #ifdef qxk_h /* QXK kernel used? */ /*! QF start priority (1..#QF_MAX_ACTIVE) of this active object. */ uint8_t startPrio; #endif } QActive; /*! protected "constructor" of an ::QActive active object */ void QActive_ctor(QActive * const me, QStateHandler initial); /*! Virtual table for the ::QActive class */ typedef struct { struct QHsmVtbl super; /*!< inherits ::QHsmVtbl */ /*! virtual function to start the active object (thread) */ /** @sa QACTIVE_START() */ void (*start)(QActive * const me, uint_fast8_t prio, QEvt const *qSto[], uint_fast16_t qLen, void *stkSto, uint_fast16_t stkSize, QEvt const *ie); #ifdef Q_SPY /*! virtual function to asynchronously post (FIFO) an event to an AO */ /** @sa QACTIVE_POST() and QACTIVE_POST_X() */ bool (*post)(QActive * const me, QEvt const * const e, uint_fast16_t const margin, void const * const sender); #else bool (*post)(QActive * const me, QEvt const * const e, uint_fast16_t const margin); #endif /*! virtual function to asynchronously post (LIFO) an event to an AO */ /** @sa QACTIVE_POST_LIFO() */ void (*postLIFO)(QActive * const me, QEvt const * const e); } QActiveVtbl; /*! Polymorphically start an active object. */ /** * @description * Starts execution of the AO and registers the AO with the framework. * * @param[in,out] me_ pointer (see @ref oop) * @param[in] prio_ priority at which to start the active object * @param[in] qSto_ pointer to the storage for the ring buffer of the * event queue (used only with the built-in ::QEQueue) * @param[in] qLen_ length of the event queue (in events) * @param[in] stkSto_ pointer to the stack storage (used only when * per-AO stack is needed) * @param[in] stkSize_ stack size (in bytes) * @param[in] param_ pointer to the additional port-specific parameter(s) * (might be NULL). * @usage * @include qf_start.c */ #define QACTIVE_START(me_, prio_, qSto_, qLen_, stkSto_, stkLen_, param_) \ ((*((QActiveVtbl const *)((me_)->super.vptr))->start)( \ (me_), (prio_), (qSto_), (qLen_), (stkSto_), (stkLen_), (param_))) #ifdef Q_SPY /*! Polymorphically posts an event to an active object (FIFO) * with delivery guarantee. */ /** * @description * This macro asserts if the queue overflows and cannot accept the event. * * @param[in,out] me_ pointer (see @ref oop) * @param[in] e_ pointer to the event to post * @param[in] sender_ pointer to the sender object. * * @note * The @p sendedr_ parameter is actually only used when QS tracing * is enabled (macro #Q_SPY is defined). When QS software tracing is * disenabled, the QACTIVE_POST() macro does not pass the @p sender_ * argument, so the overhead of passing this extra argument is entirely * avoided. * * @note * The pointer to the sender object is not necessarily a pointer * to an active object. In fact, if QACTIVE_POST() is called from an * interrupt or other context, you can create a unique object just to * unambiguously identify the sender of the event. * * @sa #QACTIVE_POST_X, QActive_post_(). */ #define QACTIVE_POST(me_, e_, sender_) \ ((void)(*((QActiveVtbl const *)((me_)->super.vptr))->post)((me_), \ (e_), QF_NO_MARGIN, (sender_))) /*! Polymorphically posts an event to an active object (FIFO) * without delivery guarantee. */ /** * @description * This macro does not assert if the queue overflows and cannot accept * the event with the specified margin of free slots remaining. * * @param[in,out] me_ pointer (see @ref oop) * @param[in] e_ pointer to the event to post * @param[in] margin_ the minimum free slots in the queue, which * must still be available after posting the event. * The special value #QF_NO_MARGIN causes asserting failure * in case event allocation fails. * @param[in] sender_ pointer to the sender object. * * @returns 'true' if the posting succeeded, and 'false' if the posting * failed due to insufficient margin of free slots available in the queue. * * @note * The @p sender_ parameter is actually only used when QS tracing * is enabled (macro #Q_SPY is defined). When QS software tracing is * disabled, the QACTIVE_POST() macro does not pass the @p sender_ * argument, so the overhead of passing this extra argument is entirely * avoided. * * @note * The pointer to the sender object is not necessarily a pointer * to an active object. In fact, if QACTIVE_POST_X() is called from an * interrupt or other context, you can create a unique object just to * unambiguously identify the sender of the event. * * @usage * @include qf_postx.c */ #define QACTIVE_POST_X(me_, e_, margin_, sender_) \ ((*((QActiveVtbl const *)((me_)->super.vptr))->post)((me_), \ (e_), (margin_), (sender_))) #else #define QACTIVE_POST(me_, e_, sender_) \ ((void)(*((QActiveVtbl const *)((me_)->super.vptr))->post)((me_), \ (e_), QF_NO_MARGIN)) #define QACTIVE_POST_X(me_, e_, margin_, sender_) \ ((*((QActiveVtbl const *)((me_)->super.vptr))->post)((me_), \ (e_), (margin_))) #endif /*! Polymorphically posts an event to an active object using the * Last-In-First-Out (LIFO) policy. */ /** * @param[in,out] me_ pointer (see @ref oop) * @param[in] e_ pointer to the event to post */ #define QACTIVE_POST_LIFO(me_, e_) \ ((*((QActiveVtbl const *)((me_)->super.vptr))->postLIFO)((me_), (e_))) /* protected functions for ::QActive ...*/ /*! Stops execution of an active object and removes it from the * framework's supervision. */ void QActive_stop(QActive * const me); /*! Subscribes for delivery of signal @p sig to the active object @p me. */ void QActive_subscribe(QActive const * const me, enum_t const sig); /*! Un-subscribes from the delivery of signal @p sig to the AO @p me. */ void QActive_unsubscribe(QActive const * const me, enum_t const sig); /*! Un-subscribes from the delivery of all signals to the AO @p me. */ void QActive_unsubscribeAll(QActive const * const me); /*! Defer an event @p e to a given event queue @p eq. */ bool QActive_defer(QActive const * const me, QEQueue * const eq, QEvt const * const e); /*! Recall a deferred event from a given event queue @p eq. */ bool QActive_recall(QActive * const me, QEQueue * const eq); /*! Flush the specified deferred queue @p eq. */ uint_fast16_t QActive_flushDeferred(QActive const * const me, QEQueue * const eq); /*! Generic setting of additional attributes (useful in QP ports) */ void QActive_setAttr(QActive *const me, uint32_t attr1, void const *attr2); /****************************************************************************/ /*! QMActive active object (based on ::QMsm implementation) */ /** * @description * QMActive represents an active object that uses the ::QMsm style state * machine implementation strategy. This strategy requires the use of the * QM modeling tool to generate state machine code automatically, but the * code is faster than in the ::QHsm style implementation strategy and needs * less run-time support (smaller event-processor). * * @note * ::QMActive is not intended to be instantiated directly, but rather serves * as the base class for derivation of active objects in the application. * * @note * ::QMActive inherits ::QActive exactly, without adding any new attributes * (or operations) and therefore, ::QMActive is typedef'ed as ::QActive. * ::QMActive is not intended to be instantiated directly, but rather serves * as the base class for derivation of active objects in the application. * * @sa ::QActive * * @usage * The following example illustrates how to derive an active object from * ::QMActive. Please note that the ::QActive member @c super is defined as * the __first__ member of the derived struct (see @ref oop). * @include qf_qmactive.c */ typedef QActive QMActive; /*! Virtual Table for the ::QMActive class (inherited from ::QActiveVtbl */ /** * @note * ::QMActive inherits ::QActive exactly, without adding any new virtual * functions and therefore, ::QMActiveVtbl is typedef'ed as ::QActiveVtbl. */ typedef QActiveVtbl QMActiveVtbl; /*! protected "constructor" of an ::QMActive active object. */ void QMActive_ctor(QMActive * const me, QStateHandler initial); /****************************************************************************/ #if (QF_TIMEEVT_CTR_SIZE == 1) typedef uint8_t QTimeEvtCtr; #elif (QF_TIMEEVT_CTR_SIZE == 2) /*! type of the Time Event counter, which determines the dynamic * range of the time delays measured in clock ticks. */ /** * @description * This typedef is configurable via the preprocessor switch * #QF_TIMEEVT_CTR_SIZE. The other possible values of this type are * as follows: @n * uint8_t when (QF_TIMEEVT_CTR_SIZE == 1), and @n * uint32_t when (QF_TIMEEVT_CTR_SIZE == 4). */ typedef uint16_t QTimeEvtCtr; #elif (QF_TIMEEVT_CTR_SIZE == 4) typedef uint32_t QTimeEvtCtr; #else #error "QF_TIMEEVT_CTR_SIZE defined incorrectly, expected 1, 2, or 4" #endif /*! Time Event structure */ /** * @description * Time events are special QF events equipped with the notion of time passage. * The basic usage model of the time events is as follows. An active object * allocates one or more ::QTimeEvt objects (provides the storage for them). * When the active object needs to arrange for a timeout, it arms one of its * time events to fire either just once (one-shot) or periodically. Each time * event times out independently from the others, so a QF application can make * multiple parallel timeout requests (from the same or different active * objects). When QF detects that the appropriate moment has arrived, it * inserts the time event directly into the recipient's event queue. The * recipient then processes the time event just like any other event. * * Time events, as any other QF events derive from the ::QEvt base structure. * Typically, you will use a time event as-is, but you can also further * derive more specialized time events from it by adding some more data * members and/or specialized functions that operate on the specialized * time events. * * Internally, the armed time events are organized into linked lists--one list * for every supported ticking rate. These linked lists are scanned in every * invocation of the QF_tickX_() function. Only armed (timing out) time events * are in the list, so only armed time events consume CPU cycles. * * @sa ::QTimeEvt for the description of the data members @n @ref oop * * @note * QF manages the time events in the function QF_tickX_(), which must be called * periodically, from the clock tick ISR or from the special ::QTicker * active object. * * @note * Even though ::QTimeEvt is a subclass of ::QEvt, ::QTimeEvt instances can NOT * be allocated dynamically from event pools. In other words, it is illegal to * allocate ::QTimeEvt instances with the Q_NEW() or Q_NEW_X() macros. */ typedef struct QTimeEvt { QEvt super; /*