sentry_chassis_hzz/modules/daemon/daemon.c

47 lines
1.3 KiB
C
Raw Normal View History

2022-12-08 17:36:12 +08:00
#include "daemon.h"
#include "bsp_dwt.h" // 后续通过定时器来计时?
#include "stdlib.h"
#include "memory.h"
2022-12-08 17:36:12 +08:00
// 用于保存所有的daemon instance
2022-12-08 17:36:12 +08:00
static DaemonInstance *daemon_instances[DAEMON_MX_CNT] = {NULL};
static uint8_t idx; // 用于记录当前的daemon instance数量,配合回调使用
2022-12-08 17:36:12 +08:00
DaemonInstance *DaemonRegister(Daemon_Init_Config_s *config)
{
DaemonInstance *instance = (DaemonInstance *)malloc(sizeof(DaemonInstance));
memset(instance, 0, sizeof(DaemonInstance));
2022-12-08 17:36:12 +08:00
instance->owner_id = config->owner_id;
instance->reload_count = config->reload_count;
instance->callback = config->callback;
2022-12-08 17:36:12 +08:00
daemon_instances[idx++] = instance;
return instance;
2022-12-08 17:36:12 +08:00
}
void DaemonReload(DaemonInstance *instance)
{
instance->temp_count = instance->reload_count;
}
uint8_t DaemonIsOnline(DaemonInstance *instance)
{
return instance->temp_count > 0;
2022-12-08 17:36:12 +08:00
}
void DaemonTask()
{
static DaemonInstance *dins; // 提高可读性同时降低访存开销
for (size_t i = 0; i < idx; ++i)
2022-12-08 17:36:12 +08:00
{
dins = daemon_instances[i];
if (dins->temp_count > 0)
dins->temp_count--;
else if (dins->callback) // 如果有callback
{
dins->callback(dins->owner_id); // module内可以将owner_id强制类型转换成自身类型从而调用自身的offline callback
2022-12-08 17:36:12 +08:00
}
}
}