37#include "oseventqueue.h"
61typedef struct __hsm_state hsm_state_t;
67typedef struct __hsm_action hsm_action_t;
73typedef struct __hsm hsm_t;
91typedef struct __hsm_state hsm_state_id [];
116 hsm_state_t * target;
121typedef struct __hsm_action hsm_action_id [];
131 hsm_action_t * action;
135typedef struct __hsm hsm_id [];
152#define _HSM_STATE_INIT( _parent ) \
162#define OS_HSM_STATE( state, parent ) \
163 hsm_state_t state[] = { _HSM_STATE_INIT( parent ) }
166#define static_HSM_STATE( state, parent ) \
167 static hsm_state_t state[] = { _HSM_STATE_INIT( parent ) }
180#define HSM_STATE_INIT( parent ) \
181 _HSM_STATE_INIT( parent )
195#define HSM_STATE_CREATE( parent ) \
196 { HSM_STATE_INIT ( parent ) }
198#define HSM_STATE_NEW \
220#define _HSM_ACTION_INIT( _owner, _event, _target, _handler ) \
221 { _owner, _event, _target, _handler, NULL }
233#define OS_HSM_ACTION( action, owner, event, target, handler ) \
234 hsm_action_t action[] = { _HSM_ACTION_INIT( owner, event, target, handler ) }
237#define static_HSM_ACTION( action, owner, event, target, handler ) \
238 static hsm_action_t action[] = { _HSM_ACTION_INIT( owner, event, target, handler ) }
254#define HSM_ACTION_INIT( owner, event, target, handler ) \
255 _HSM_ACTION_INIT( owner, event, target, handler )
272#define HSM_ACTION_CREATE( owner, event, target, handler ) \
273 { HSM_ACTION_INIT ( owner, event, target, handler ) }
275#define HSM_ACTION_NEW \
295#define _HSM_INIT( _limit, _data ) \
296 { _EVQ_INIT( _limit, _data ), NULL, NULL, NULL }
305#define OS_HSM( hsm, limit ) \
306 static unsigned hsm##__buf[limit]; \
307 hsm_t hsm[] = { _HSM_INIT( limit, hsm##__buf ) }
310#define static_HSM( hsm, limit ) \
311 static unsigned hsm##__buf[limit]; \
312 static hsm_t hsm[] = { _HSM_INIT( limit, hsm##__buf ) }
325#define HSM_INIT( limit ) \
326 _HSM_INIT( limit, _EVQ_DATA( limit ) )
340#define HSM_CREATE( limit ) \
341 { HSM_INIT ( limit ) }
358void hsm_initState( hsm_state_t *state, hsm_state_t *parent );
388void hsm_link( hsm_action_t *action );
398void hsm_start( hsm_t *hsm, tsk_t *tsk, hsm_state_t *initState );
402void hsm_startAsync( hsm_t *hsm, tsk_t *tsk, hsm_state_t *initState );
433int hsm_give( hsm_t *hsm,
unsigned event );
455int hsm_sendFor( hsm_t *hsm,
unsigned event, cnt_t delay );
501void hsm_push( hsm_t *hsm,
unsigned event );
519#if defined(__cplusplus) && (__cplusplus >= 201402L)
530struct State :
public __hsm_state
534 State(): __hsm_state _HSM_STATE_INIT( nullptr) {}
535 template<
class S>
constexpr
537 State( S& _parent ): __hsm_state _HSM_STATE_INIT(&_parent) {}
542 assert(_src.queue ==
nullptr);
556struct Action :
public __hsm_action
558 template<
class S>
constexpr
560 Action( S& _owner,
unsigned _event,
hsm_handler_t *_handler =
nullptr ): __hsm_action _HSM_ACTION_INIT(&_owner, _event, nullptr, _handler) {}
561 template<
class S>
constexpr
563 Action( S& _owner,
unsigned _event, S& _target,
hsm_handler_t *_handler =
nullptr ): __hsm_action _HSM_ACTION_INIT(&_owner, _event, &_target, _handler) {}
564#if __cplusplus >= 201402L
565 template<
class S,
class F>
constexpr
567 Action( S& _owner,
unsigned _event, F&& _handler ): __hsm_action _HSM_ACTION_INIT(&_owner, _event, nullptr, handler_),
handler{_handler} {}
568 template<
class S,
class F>
constexpr
570 Action( S& _owner,
unsigned _event, S& _target, F&& _handler ): __hsm_action _HSM_ACTION_INIT(&_owner, _event, &_target, handler_),
handler{_handler} {}
580#if __cplusplus >= 201402L
582 void handler_( hsm_t *_hsm,
unsigned _event ) {
static_cast<Action*
>(_hsm->action)->
handler(_hsm, _event); }
583 std::function<void( hsm_t *,
unsigned )>
handler;
591 friend struct StateMachineT;
601template<
unsigned limit_ = 1>
607 StateMachineT(
const std::vector<Action>& _tab ): __hsm _HSM_INIT(limit_, data_), tab_{_tab} {}
612 assert(_src.state ==
nullptr && _src.evq.count == 0 && _src.evq.obj.queue ==
nullptr);
621 template<
class S,
typename... A>
622 void add ( S& _owner,
unsigned _event, A&&... _args ) { tab_.emplace_back(_owner, _event, std::forward<A>(_args)...); }
624 void add (
const std::vector<Action>& _tab ) { std::copy(std::begin(_tab), std::end(_tab), std::back_inserter(tab_)); }
626 void start ( tsk_t& _task, hsm_state_t& _init ) {
for (
auto& _action: tab_) _action.link();
630 int give (
unsigned _event ) {
return hsm_give (
this, _event); }
635 int send (
unsigned _event ) {
return hsm_send (
this, _event); }
636 void push (
unsigned _event ) {
hsm_push (
this, _event); }
640 void startAsync( tsk_t& _task, hsm_state_t& _init ) {
for (
auto& _action: tab_) _action.link();
648 unsigned data_[limit_];
649 std::vector<Action> tab_;
#define INFINITE
infinite waiting
Definition osbase.h:250
char data[]
message data
Definition osmessagequeue.h:54
int hsm_give(hsm_t *hsm, unsigned event)
try to transfer event data to the hsm event queue, don't wait if the hsm event queue is full
int hsm_giveAsync(hsm_t *hsm, unsigned event)
Async alias of hsm_give().
void hsm_kill(hsm_t *hsm)
alias of hsm_reset()
Definition osstatemachine.h:415
void hsm_reset(hsm_t *hsm)
reset the hsm object, stop the hsm event dispatcher and wake up all waiting tasks with 'E_STOPPED' ev...
int hsm_sendAsync(hsm_t *hsm, unsigned event)
Async alias of hsm_send().
void hsm_push(hsm_t *hsm, unsigned event)
transfer event data to the hsm event queue, remove the oldest event data if the hsm event queue is fu...
int hsm_send(hsm_t *hsm, unsigned event)
try to transfer event data to the hsm event queue, wait indefinitely while the hsm event queue is ful...
Definition osstatemachine.h:483
void hsm_startAsync(hsm_t *hsm, tsk_t *tsk, hsm_state_t *initState)
Async alias of hsm_start().
void hsm_link(hsm_action_t *action)
link hsm state action to the state action queue
int hsm_sendUntil(hsm_t *hsm, unsigned event, cnt_t time)
try to transfer event value to the hsm event queue, wait until given timepoint while the hsm event qu...
hsm_state_t * hsm_getState(hsm_t *hsm)
get current hsm state
int hsm_sendFor(hsm_t *hsm, unsigned event, cnt_t delay)
try to transfer event value to the hsm event queue, wait for given duration of time while the hsm eve...
void hsm_handler_t(hsm_t *, unsigned)
hierarchical state machine - event handler definition
Definition osstatemachine.h:79
void hsm_start(hsm_t *hsm, tsk_t *tsk, hsm_state_t *initState)
start hsm event dispatcher
void hsm_initAction(hsm_action_t *action, hsm_state_t *owner, unsigned event, hsm_state_t *target, hsm_handler_t *handler)
initialize hsm state action object
void hsm_init(hsm_t *hsm, void *data, size_t bufsize)
initialize hsm object
void hsm_initState(hsm_state_t *state, hsm_state_t *parent)
initialize hsm object
constexpr Action(S &_owner, unsigned _event, S &_target, hsm_handler_t *_handler=nullptr)
constructor, see the constructor parameters of the class
Definition osstatemachine.h:560
constexpr Action(S &_owner, unsigned _event, S &_target, F &&_handler)
constructor, see the constructor parameters of the class
Definition osstatemachine.h:567
constexpr Action(S &_owner, unsigned _event, hsm_handler_t *_handler=nullptr)
constructor, see the constructor parameters of the class
Definition osstatemachine.h:557
std::function< void(hsm_t *, unsigned)> handler
handler of the action (function object)
Definition osstatemachine.h:580
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:109
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:102
create and initialize a hierarchical state machine object
Definition osstatemachine.h:600
void startAsync(tsk_t &_task, hsm_state_t &_init)
link the actions of the table and start the state machine (hsm_startAsync())
Definition osstatemachine.h:637
void push(unsigned _event)
hsm_push()
Definition osstatemachine.h:633
void reset()
hsm_reset()
Definition osstatemachine.h:625
int sendFor(unsigned _event, const T &_delay)
hsm_sendFor()
Definition osstatemachine.h:629
int sendUntil(unsigned _event, const T &_time)
hsm_sendUntil()
Definition osstatemachine.h:631
hsm_state_t * getState()
hsm_getState()
Definition osstatemachine.h:634
int send(unsigned _event)
hsm_send()
Definition osstatemachine.h:632
int sendAsync(unsigned _event)
hsm_sendAsync()
Definition osstatemachine.h:640
void add(S &_owner, unsigned _event, A &&... _args)
add an action (parameters of the Action constructor) to the table of the state machine
Definition osstatemachine.h:619
void kill()
hsm_kill()
Definition osstatemachine.h:626
void start(tsk_t &_task, hsm_state_t &_init)
link the actions of the table and start the state machine (hsm_start())
Definition osstatemachine.h:623
StateMachineT(StateMachineT &&_src)
move constructor
Definition osstatemachine.h:607
int giveAsync(unsigned _event)
hsm_giveAsync()
Definition osstatemachine.h:639
StateMachineT()
constructor, see the constructor parameters of the class
Definition osstatemachine.h:602
int give(unsigned _event)
hsm_give()
Definition osstatemachine.h:627
create and initialize an hsm state object
Definition osstatemachine.h:528
constexpr State()
constructor, see the constructor parameters of the class
Definition osstatemachine.h:531
State(State &&_src)
move constructor
Definition osstatemachine.h:537
constexpr State(S &_parent)
constructor, see the constructor parameters of the class
Definition osstatemachine.h:534