2022-12-08 17:36:12 +08:00
|
|
|
#include "daemon.h"
|
|
|
|
#include "bsp_dwt.h" // 后续通过定时器来计时?
|
2022-12-09 18:25:35 +08:00
|
|
|
#include "stdlib.h"
|
2023-01-01 12:45:07 +08:00
|
|
|
#include "memory.h"
|
2023-06-23 17:05:55 +08:00
|
|
|
#include "buzzer.h"
|
2022-12-08 17:36:12 +08:00
|
|
|
|
2023-01-01 12:45:07 +08:00
|
|
|
// 用于保存所有的daemon instance
|
2022-12-08 17:36:12 +08:00
|
|
|
static DaemonInstance *daemon_instances[DAEMON_MX_CNT] = {NULL};
|
2023-01-01 12:45:07 +08:00
|
|
|
static uint8_t idx; // 用于记录当前的daemon instance数量,配合回调使用
|
2022-12-08 17:36:12 +08:00
|
|
|
|
|
|
|
DaemonInstance *DaemonRegister(Daemon_Init_Config_s *config)
|
|
|
|
{
|
2023-01-01 12:45:07 +08:00
|
|
|
DaemonInstance *instance = (DaemonInstance *)malloc(sizeof(DaemonInstance));
|
|
|
|
memset(instance, 0, sizeof(DaemonInstance));
|
2022-12-08 17:36:12 +08:00
|
|
|
|
2023-01-01 12:45:07 +08:00
|
|
|
instance->owner_id = config->owner_id;
|
2023-06-23 15:56:20 +08:00
|
|
|
instance->reload_count = config->reload_count == 0 ? 100 : config->reload_count; // 默认值为100
|
2023-01-01 12:45:07 +08:00
|
|
|
instance->callback = config->callback;
|
2023-06-23 15:56:20 +08:00
|
|
|
instance->temp_count = config->init_count == 0 ? 100 : config->init_count; // 默认值为100,初始计数
|
2022-12-08 17:36:12 +08:00
|
|
|
|
2023-06-23 17:05:55 +08:00
|
|
|
instance->temp_count = config->reload_count;
|
2023-01-01 12:45:07 +08:00
|
|
|
daemon_instances[idx++] = instance;
|
|
|
|
return instance;
|
2022-12-08 17:36:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-01 17:32:22 +08:00
|
|
|
/* "喂狗"函数 */
|
2022-12-08 17:36:12 +08:00
|
|
|
void DaemonReload(DaemonInstance *instance)
|
|
|
|
{
|
|
|
|
instance->temp_count = instance->reload_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t DaemonIsOnline(DaemonInstance *instance)
|
|
|
|
{
|
2023-01-01 12:45:07 +08:00
|
|
|
return instance->temp_count > 0;
|
2022-12-08 17:36:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonTask()
|
|
|
|
{
|
2023-03-23 18:22:24 +08:00
|
|
|
DaemonInstance *dins; // 提高可读性同时降低访存开销
|
2022-12-13 19:40:03 +08:00
|
|
|
for (size_t i = 0; i < idx; ++i)
|
2022-12-08 17:36:12 +08:00
|
|
|
{
|
2023-06-23 17:05:55 +08:00
|
|
|
|
2023-01-01 12:45:07 +08:00
|
|
|
dins = daemon_instances[i];
|
2023-01-01 17:32:22 +08:00
|
|
|
if (dins->temp_count > 0) // 如果计数器还有值,说明上一次喂狗后还没有超时,则计数器减一
|
2023-01-01 12:45:07 +08:00
|
|
|
dins->temp_count--;
|
2023-01-01 17:32:22 +08:00
|
|
|
else if (dins->callback) // 等于零说明超时了,调用回调函数(如果有的话)
|
2023-01-01 12:45:07 +08:00
|
|
|
{
|
2023-01-01 17:32:22 +08:00
|
|
|
dins->callback(dins->owner_id); // module内可以将owner_id强制类型转换成自身类型从而调用特定module的offline callback
|
2023-06-03 21:58:21 +08:00
|
|
|
// @todo 为蜂鸣器/led等增加离线报警的功能,非常关键!
|
2022-12-08 17:36:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-01 17:32:22 +08:00
|
|
|
// (需要id的原因是什么?) 下面是copilot的回答!
|
|
|
|
// 需要id的原因是因为有些module可能有多个实例,而我们需要知道具体是哪个实例offline
|
|
|
|
// 如果只有一个实例,则可以不用id,直接调用callback即可
|
|
|
|
// 比如: 有一个module叫做"电机",它有两个实例,分别是"电机1"和"电机2",那么我们调用电机的离线处理函数时就需要知道是哪个电机offline
|