91 lines
1.9 KiB
C
91 lines
1.9 KiB
C
|
/**
|
|||
|
* @author R<EFBFBD>mi Heredero
|
|||
|
* @version 1.0.0
|
|||
|
* @date August 2023
|
|||
|
* @file watchdog.c
|
|||
|
*/
|
|||
|
|
|||
|
#include "watchdog.h"
|
|||
|
|
|||
|
void WATCHDOG_init(WATCHDOG* me){
|
|||
|
me->state = STWD_INIT;
|
|||
|
me->time = 10;
|
|||
|
me->alive.f = NULL;
|
|||
|
}
|
|||
|
|
|||
|
void WATCHDOG_startBehaviour(WATCHDOG* me){
|
|||
|
POST(me, &WATCHDOG_processEvent, evWDinit, 0, 0);
|
|||
|
}
|
|||
|
|
|||
|
bool WATCHDOG_processEvent(Event* ev) {
|
|||
|
bool processed = false;
|
|||
|
WATCHDOG* me = (WATCHDOG*)Event_getTarget(ev);
|
|||
|
WATCHDOG_STATES oldState = me->state;
|
|||
|
evIDT evid = Event_getId(ev);
|
|||
|
uint64_t data = Event_getData(ev);
|
|||
|
|
|||
|
switch (me->state) { // onState
|
|||
|
case STWD_INIT:
|
|||
|
if (ev->id == evWDinit) {
|
|||
|
me->state = STWD_ALIVE;
|
|||
|
WATCHDOG_emitPoll(me, 10*me->time, 0);
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
case STWD_ALIVE:
|
|||
|
if (ev->id == evWDpoll) {
|
|||
|
WATCHDOG_emitPoll(me, 10*me->time, 0);
|
|||
|
if (me->alive.f != NULL) {
|
|||
|
me->alive.f(me->alive.p);
|
|||
|
}
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if(oldState != me->state){
|
|||
|
switch (oldState) { // onExit
|
|||
|
case STWD_INIT:
|
|||
|
break;
|
|||
|
|
|||
|
case STWD_ALIVE:
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
switch (me->state) { // onEntry
|
|||
|
case STWD_INIT:
|
|||
|
break;
|
|||
|
|
|||
|
case STWD_ALIVE:
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
processed = true;
|
|||
|
}
|
|||
|
return processed;
|
|||
|
}
|
|||
|
|
|||
|
/*************
|
|||
|
* Callbacks *
|
|||
|
*************/
|
|||
|
|
|||
|
void WATCHDOG_onAlive(WATCHDOG* me, WATCHDOG_CALLBACK_FUNCTION f, void* p) {
|
|||
|
me->alive.f = f;
|
|||
|
me->alive.p = p;
|
|||
|
}
|
|||
|
|
|||
|
/************
|
|||
|
* EMITTERS *
|
|||
|
************/
|
|||
|
|
|||
|
void WATCHDOG_emitPoll(WATCHDOG* me, uint16_t t, int64_t data) {
|
|||
|
POST(me, &WATCHDOG_processEvent, evWDpoll, t, data);
|
|||
|
}
|
|||
|
|
|||
|
/***********
|
|||
|
* SETTERS *
|
|||
|
***********/
|
|||
|
|
|||
|
void WATCHDOG_setTime(WATCHDOG* me, uint8_t v) {
|
|||
|
me->time = v;
|
|||
|
}
|