This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
SummerSchool2-Controller/306-controller_interface.X/middleware/alive_checker.c
2023-08-25 18:35:01 +02:00

177 lines
4.3 KiB
C

/**
* @author Rémi Heredero
* @version 1.0.0
* @date August 2023
* @file alive_checker.c
*/
#include "alive_checker.h"
void ALIVE_CHECKER_init(ALIVE_CHECKER* me){
me->state = STAC_INIT;
me->isAlive = false;
me->aliveTime = 10;
me->setup.f = NULL;
me->born.f = NULL;
me->wait.f = NULL;
me->dead.f = NULL;
}
void ALIVE_CHECKER_startBehaviour(ALIVE_CHECKER* me){
POST(me, &ALIVE_CHECKER_processEvent, evACinit, 0, 0);
}
bool ALIVE_CHECKER_processEvent(Event* ev) {
bool processed = false;
ALIVE_CHECKER* me = (ALIVE_CHECKER*)Event_getTarget(ev);
ALIVE_CHECKER_STATES oldState = me->state;
evIDT evid = Event_getId(ev);
uint64_t data = Event_getData(ev);
switch (me->state) { // onState
case STAC_INIT:
if (ev->id == evACinit) {
me->state = STAC_SETUP;
}
break;
case STAC_SETUP:
if (ev->id == evACborn) {
me->state = STAC_BORN;
}
break;
case STAC_BORN:
if (ev->id == evACready) {
me->state = STAC_WAIT;
ALIVE_CHECKER_emitPoll(me, me->aliveTime*10, 0);
}
break;
case STAC_WAIT:
if (ev->id == evACpoll) {
if (me->isAlive) {
me->state = STAC_WAIT;
ALIVE_CHECKER_emitPoll(me, me->aliveTime*10, 0);
} else {
me->state = STAC_DEAD;
}
me->isAlive = false;
}
break;
case STAC_DEAD:
if(ev->id == evACborn) {
me->state = STAC_BORN;
}
break;
}
if(oldState != me->state){
switch (oldState) { // onExit
case STAC_INIT:
break;
case STAC_SETUP:
break;
case STAC_BORN:
break;
case STAC_WAIT:
break;
case STAC_DEAD:
break;
}
switch (me->state) { // onEntry
case STAC_INIT:
break;
case STAC_SETUP:
if (me->setup.f != NULL) {
me->setup.f(me->setup.p);
}
break;
case STAC_BORN:
if (me->born.f != NULL) {
me->born.f(me->born.p);
}
break;
case STAC_WAIT:
if (me->wait.f != NULL) {
me->wait.f(me->wait.p);
}
break;
case STAC_DEAD:
if (me->dead.f != NULL) {
me->dead.f(me->dead.p);
}
break;
}
processed = true;
}
return processed;
}
/*************
* Callbacks *
*************/
void ALIVE_CHECKER_onSetup(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p) {
me->setup.f = f;
me->setup.p = p;
}
void ALIVE_CHECKER_onBorn(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p) {
me->born.f = f;
me->born.p = p;
}
void ALIVE_CHECKER_onWait(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p) {
me->wait.f = f;
me->wait.p = p;
}
void ALIVE_CHECKER_onDead(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p) {
me->dead.f = f;
me->dead.p = p;
}
/************
* EMITTERS *
************/
void ALIVE_CHECKER_emitBorn(ALIVE_CHECKER* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_CHECKER_processEvent, evACborn, t, data);
}
void ALIVE_CHECKER_emitReady(ALIVE_CHECKER* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_CHECKER_processEvent, evACready, t, data);
}
void ALIVE_CHECKER_emitPoll(ALIVE_CHECKER* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_CHECKER_processEvent, evACpoll, t, data);
}
/***********
* SETTERS *
***********/
void ALIVE_CHECKER_setAliveTime(ALIVE_CHECKER* me, uint8_t v) {
me->aliveTime = v;
}
void ALIVE_CHECKER_setIsAlive(ALIVE_CHECKER* me, bool v) {
me->isAlive = v;
}
void ALIVE_CHECKER_ISALIVE(ALIVE_CHECKER* me) {
ALIVE_CHECKER_setIsAlive(me, true);
}