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"
|
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;
|
|
|
|
instance->reload_count = config->reload_count;
|
|
|
|
instance->callback = config->callback;
|
2022-12-08 17:36:12 +08:00
|
|
|
|
2023-01-01 12:45:07 +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)
|
|
|
|
{
|
2023-01-01 12:45:07 +08:00
|
|
|
return instance->temp_count > 0;
|
2022-12-08 17:36:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonTask()
|
|
|
|
{
|
2023-01-01 12:45:07 +08:00
|
|
|
static 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-01-01 12:45:07 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|