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/watchdog.c
2023-08-25 14:13:51 +02:00

91 lines
1.9 KiB
C

/**
* @author Ré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;
}