RTU_ALL_AI/src/system/app_modules.c

134 lines
3.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file app_modules.c
* @brief 模块注册框架实现
* @details 管理已注册线程模块的生命周期: register → init1 → init2 → start → stop。
* 此文件编译进 libsystem.a供 RTU 主程序使用。
*/
#include "mySystem.h"
#include "myLog.h"
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
/** 最大可注册模块数 */
#define APP_MODULES_MAX 16
/** 已注册模块表 */
static stru_app_module g_modules[APP_MODULES_MAX];
static int g_module_count = 0;
/** 线程句柄表 */
static pthread_t g_threads[APP_MODULES_MAX];
void app_module_register(const char *name,
app_init1_fn init1,
app_init2_fn init2,
app_run_fn run,
stru_app *p_app)
{
if (g_module_count >= APP_MODULES_MAX)
{
LOG_E("app_module_register: max modules reached");
return;
}
if (!name || !run)
{
LOG_E("app_module_register: invalid args");
return;
}
stru_app_module *m = &g_modules[g_module_count];
m->name = name;
m->init1 = init1;
m->init2 = init2;
m->run = run;
m->p_app = p_app;
g_module_count++;
LOG_I("app_module_register: [%s] registered", name);
}
int app_modules_init1(void)
{
for (int i = 0; i < g_module_count; i++)
{
if (g_modules[i].init1)
{
LOG_I("app_modules_init1: [%s]...", g_modules[i].name);
if (g_modules[i].init1(g_modules[i].p_app) != 0)
{
LOG_E("app_modules_init1: [%s] failed", g_modules[i].name);
return -1;
}
}
}
return 0;
}
int app_modules_init2(void)
{
for (int i = 0; i < g_module_count; i++)
{
if (g_modules[i].init2)
{
LOG_I("app_modules_init2: [%s]...", g_modules[i].name);
if (g_modules[i].init2(g_modules[i].p_app) != 0)
{
LOG_E("app_modules_init2: [%s] failed", g_modules[i].name);
return -1;
}
}
}
return 0;
}
void app_modules_start(void)
{
for (int i = 0; i < g_module_count; i++)
{
LOG_I("app_modules_start: [%s]...", g_modules[i].name);
if (pthread_create(&g_threads[i], NULL, g_modules[i].run,
g_modules[i].p_app) != 0)
{
LOG_E("app_modules_start: [%s] thread create failed",
g_modules[i].name);
}
}
}
void app_modules_stop(void)
{
for (int i = 0; i < g_module_count; i++)
{
if (g_modules[i].p_app && g_modules[i].p_app->p_event)
{
task_event_send(g_modules[i].p_app->p_event, 0x80); /* 停止信号 */
}
}
}
void app_modules_join(void)
{
for (int i = 0; i < g_module_count; i++)
{
if (g_threads[i])
{
pthread_join(g_threads[i], NULL);
LOG_I("app_modules_join: [%s] exited", g_modules[i].name);
}
}
}