add alive_checker

This commit is contained in:
Rémi Heredero 2023-08-24 15:25:23 +02:00
parent 6b8e797744
commit 6b9d3953bc
3 changed files with 488 additions and 0 deletions

View File

@ -0,0 +1,176 @@
/**
* @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;
}
}
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:
me->isAlive = false;
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->aliveTime = v;
}
void ALIVE_CHECKER_ISALIVE(ALIVE_CHECKER* me) {
ALIVE_CHECKER_setIsAlive(me, true);
}

View File

@ -0,0 +1,134 @@
/**
* @author Rémi Heredero
* @version 1.0.0
* @date August 2023
* @file alive_checker.h
*/
#ifndef ALIVE_CHECKER_H
#define ALIVE_CHECKER_H
#include "../xf/xf.h"
typedef enum {
STAC_INIT,
STAC_SETUP,
STAC_BORN,
STAC_WAIT,
STAC_DEAD
} ALIVE_CHECKER_STATES;
typedef enum {
evACinit = 100, // TODO change this number (< 256)
evACborn,
evACready,
evACpoll
} ALIVE_CHECKER_EVENTS;
typedef void (*ALIVE_CHECKER_CALLBACK_FUNCTION)(void*);
typedef struct {
ALIVE_CHECKER_CALLBACK_FUNCTION f; // function
void* p; // param(s)
} ALIVE_CHECKER_CALLBACK;
typedef struct {
ALIVE_CHECKER_STATES state;
bool isAlive;
uint8_t aliveTime;
ALIVE_CHECKER_CALLBACK setup;
ALIVE_CHECKER_CALLBACK born;
ALIVE_CHECKER_CALLBACK wait;
ALIVE_CHECKER_CALLBACK dead;
} ALIVE_CHECKER;
/**
* Initialize the ALIVE_CHECKER
* @param me the ALIVE_CHECKER itself
*/
void ALIVE_CHECKER_init(ALIVE_CHECKER* me);
/**
* Start the ALIVE_CHECKER state machine
* @param me the ALIVE_CHECKER itself
*/
void ALIVE_CHECKER_startBehaviour(ALIVE_CHECKER* me);
/**
* Process the event
* @param ev the event to process
* @return true if the event is processed
*/
bool ALIVE_CHECKER_processEvent(Event* ev);
/*************
* Callbacks *
*************/
/**
* Set the callback function to call when the ALIVE_CHECKER is entering state setup
* @param me the ALIVE_CHECKER itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void ALIVE_CHECKER_onSetup(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p);
/**
* Set the callback function to call when the ALIVE_CHECKER is entering state born
* @param me the ALIVE_CHECKER itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void ALIVE_CHECKER_onBorn(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p);
/**
* Set the callback function to call when the ALIVE_CHECKER is entering state wait
* @param me the ALIVE_CHECKER itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void ALIVE_CHECKER_onWait(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p);
/**
* Set the callback function to call when the ALIVE_CHECKER is entering state dead
* @param me the ALIVE_CHECKER itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void ALIVE_CHECKER_onDead(ALIVE_CHECKER* me, ALIVE_CHECKER_CALLBACK_FUNCTION f, void* p);
/************
* EMITTERS *
************/
/**
* Emit the born event
* @param me the ALIVE_CHECKER itself
* @param t time to wait in ms before triggering event
* @param data data to put on the event for XF
*/
void ALIVE_CHECKER_emitBorn(ALIVE_CHECKER* me, uint16_t t, int64_t data);
/**
* Emit the ready event
* @param me the ALIVE_CHECKER itself
* @param t time to wait in ms before triggering event
* @param data data to put on the event for XF
*/
void ALIVE_CHECKER_emitReady(ALIVE_CHECKER* me, uint16_t t, int64_t data);
/**
* Emit the poll event
* @param me the ALIVE_CHECKER itself
* @param t time to wait in ms before triggering event
* @param data data to put on the event for XF
*/
void ALIVE_CHECKER_emitPoll(ALIVE_CHECKER* me, uint16_t t, int64_t data);
/***********
* SETTERS *
***********/
void ALIVE_CHECKER_setAliveTime(ALIVE_CHECKER* me, uint8_t v);
void ALIVE_CHECKER_setIsAlive(ALIVE_CHECKER* me, bool v);
void ALIVE_CHECKER_ISALIVE(ALIVE_CHECKER* me); // Use this one when you receive CAN message
#endif

178
UML/alive.uxf Normal file
View File

@ -0,0 +1,178 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<diagram program="umlet" version="15.0.0">
<zoom_level>14</zoom_level>
<element>
<id>UMLSpecialState</id>
<coordinates>
<x>266</x>
<y>98</y>
<w>28</w>
<h>28</h>
</coordinates>
<panel_attributes>type=initial</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>266</x>
<y>112</y>
<w>112</w>
<h>98</h>
</coordinates>
<panel_attributes>lt=-&gt;
evACinit</panel_attributes>
<additional_attributes>10.0;10.0;10.0;50.0</additional_attributes>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>210</x>
<y>182</y>
<w>140</w>
<h>56</h>
</coordinates>
<panel_attributes>STAC_SETUP</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLNote</id>
<coordinates>
<x>434</x>
<y>182</y>
<w>140</w>
<h>42</h>
</coordinates>
<panel_attributes>Send params</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>266</x>
<y>224</y>
<w>126</w>
<h>98</h>
</coordinates>
<panel_attributes>lt=-&gt;
evACborn</panel_attributes>
<additional_attributes>10.0;10.0;10.0;50.0</additional_attributes>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>210</x>
<y>294</y>
<w>140</w>
<h>56</h>
</coordinates>
<panel_attributes>STAC_BORN</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>168</x>
<y>406</y>
<w>224</w>
<h>84</h>
</coordinates>
<panel_attributes>STAC_WAIT
--
/entry: isAlive = false</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>266</x>
<y>336</y>
<w>140</w>
<h>98</h>
</coordinates>
<panel_attributes>lt=-&gt;
evACready</panel_attributes>
<additional_attributes>10.0;10.0;10.0;50.0</additional_attributes>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>210</x>
<y>658</y>
<w>140</w>
<h>56</h>
</coordinates>
<panel_attributes>STAC_DEAD</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>266</x>
<y>476</y>
<w>112</w>
<h>98</h>
</coordinates>
<panel_attributes>lt=-&gt;
evACpoll</panel_attributes>
<additional_attributes>10.0;10.0;10.0;50.0</additional_attributes>
</element>
<element>
<id>UMLSpecialState</id>
<coordinates>
<x>252</x>
<y>546</y>
<w>56</w>
<h>56</h>
</coordinates>
<panel_attributes>type=decision</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>266</x>
<y>588</y>
<w>84</w>
<h>98</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[else]</panel_attributes>
<additional_attributes>10.0;10.0;10.0;50.0</additional_attributes>
</element>
<element>
<id>UMLNote</id>
<coordinates>
<x>434</x>
<y>294</y>
<w>140</w>
<h>42</h>
</coordinates>
<panel_attributes>Reset / Init</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>294</x>
<y>420</y>
<w>182</w>
<h>196</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[alive]</panel_attributes>
<additional_attributes>10.0;110.0;110.0;110.0;110.0;10.0;70.0;10.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>140</x>
<y>308</y>
<w>168</w>
<h>490</h>
</coordinates>
<panel_attributes>lt=-&gt;
evACborn</panel_attributes>
<additional_attributes>100.0;290.0;100.0;330.0;10.0;330.0;10.0;10.0;50.0;10.0</additional_attributes>
</element>
</diagram>