implement watchdog

This commit is contained in:
2023-08-25 14:13:51 +02:00
parent d6d667a3c4
commit e3f971bb18
7 changed files with 243 additions and 2 deletions

View File

@ -0,0 +1,90 @@
/**
* @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;
}

View File

@ -0,0 +1,83 @@
/**
* @author R<>mi Heredero
* @version 1.0.0
* @date August 2023
* @file watchdog.h
*/
#ifndef WATCHDOG_H
#define WATCHDOG_H
#include "../xf/xf.h"
typedef enum {
STWD_INIT,
STWD_ALIVE
} WATCHDOG_STATES;
typedef enum {
evWDinit = 20,
evWDpoll
} WATCHDOG_EVENTS;
typedef void (*WATCHDOG_CALLBACK_FUNCTION)(void*);
typedef struct {
WATCHDOG_CALLBACK_FUNCTION f; // function
void* p; // param(s)
} WATCHDOG_CALLBACK;
typedef struct {
WATCHDOG_STATES state;
uint8_t time;
WATCHDOG_CALLBACK alive;
} WATCHDOG;
/**
* Initialize the WATCHDOG
* @param me the WATCHDOG itself
*/
void WATCHDOG_init(WATCHDOG* me);
/**
* Start the WATCHDOG state machine
* @param me the WATCHDOG itself
*/
void WATCHDOG_startBehaviour(WATCHDOG* me);
/**
* Process the event
* @param ev the event to process
* @return true if the event is processed
*/
bool WATCHDOG_processEvent(Event* ev);
/*************
* Callbacks *
*************/
/**
* Set the callback function to call when the WATCHDOG is entering state alive
* @param me the WATCHDOG itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void WATCHDOG_onAlive(WATCHDOG* me, WATCHDOG_CALLBACK_FUNCTION f, void* p);
/************
* EMITTERS *
************/
/**
* Emit the poll event
* @param me the WATCHDOG itself
* @param t time to wait in ms before triggering event
* @param data data to put on the event for XF
*/
void WATCHDOG_emitPoll(WATCHDOG* me, uint16_t t, int64_t data);
/***********
* SETTERS *
***********/
void WATCHDOG_setTime(WATCHDOG* me, uint8_t v);
#endif