Files
blinky/inc/module_lifecycle.h
skiinder e0817a7b44 feat(module_lifecycle): 完善模块生命周期管理功能
- 添加了模块生命周期状态转换验证函数,包括目标状态允许性检查和路径允许性检查
- 实现了模块操作验证功能,确保必要的回调函数存在
- 更新了状态报告逻辑,增加了模式检查以防止无效状态转换
- 修改了生命周期转换流程,区分运行启动和停止操作
- 优化了错误处理机制,返回适当的错误码

refactor(ble_modules): 简化BLE模块生命周期配置

- 将多个BLE模块(lifecycle_cfg)的模式从ML_MODE_POWER改为ML_MODE_NONE
- 移除了power_event相关依赖和事件订阅
- 更新了BLE BAS模块的状态转换逻辑
- 简化了BLE HID/NUS/Protocol模块的电源管理相关代码

fix(mode_switch): 修复唤醒后模式恢复功能

- 在唤醒事件处理中添加了最后模式的恢复逻辑
- 确保设备唤醒后能够重新提交之前的模式切换事件
2026-04-18 14:17:23 +08:00

273 lines
5.1 KiB
C

#ifndef MODULE_LIFECYCLE_H_
#define MODULE_LIFECYCLE_H_
#include <errno.h>
#include <stdbool.h>
#include <caf/events/module_state_event.h>
enum module_lifecycle {
LC_UNINIT,
LC_RUNNING,
LC_STOPPED,
LC_SUSPENDED,
LC_ERROR,
};
enum module_lifecycle_mode {
ML_MODE_NONE,
ML_MODE_POWER,
ML_MODE_SUSPEND,
};
struct module_lifecycle_ops {
int (*do_init)(void);
int (*do_start)(void);
int (*do_stop)(void);
};
struct module_lifecycle_cfg {
enum module_lifecycle_mode mode;
enum module_state stopped_state;
};
struct module_lifecycle_ctx {
enum module_lifecycle state;
const struct module_lifecycle_cfg *cfg;
const struct module_lifecycle_ops *ops;
};
static inline bool module_lifecycle_is_running(
const struct module_lifecycle_ctx *ctx)
{
return ctx->state == LC_RUNNING;
}
static inline bool module_lifecycle_is_initialized(
const struct module_lifecycle_ctx *ctx)
{
return (ctx->state != LC_UNINIT) && (ctx->state != LC_ERROR);
}
static inline bool module_lifecycle_reports_stopped_state(
const struct module_lifecycle_ctx *ctx)
{
return ctx->cfg->mode == ML_MODE_POWER;
}
static inline bool module_lifecycle_target_allowed(
const struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
switch (target) {
case LC_RUNNING:
case LC_ERROR:
return true;
case LC_STOPPED:
return ctx->cfg->mode == ML_MODE_POWER;
case LC_SUSPENDED:
return ctx->cfg->mode == ML_MODE_SUSPEND;
case LC_UNINIT:
default:
return false;
}
}
static inline bool module_lifecycle_path_allowed(
enum module_lifecycle current,
enum module_lifecycle target)
{
switch (current) {
case LC_UNINIT:
return (target == LC_RUNNING) || (target == LC_STOPPED);
case LC_RUNNING:
return (target == LC_STOPPED) || (target == LC_SUSPENDED);
case LC_STOPPED:
case LC_SUSPENDED:
return target == LC_RUNNING;
case LC_ERROR:
default:
return false;
}
}
static inline int module_lifecycle_validate_ops(
const struct module_lifecycle_ctx *ctx)
{
if ((ctx == NULL) || (ctx->cfg == NULL) || (ctx->ops == NULL) ||
(ctx->ops->do_init == NULL)) {
return -EINVAL;
}
switch (ctx->cfg->mode) {
case ML_MODE_NONE:
return 0;
case ML_MODE_POWER:
case ML_MODE_SUSPEND:
return (ctx->ops->do_start != NULL) && (ctx->ops->do_stop != NULL) ?
0 :
-EINVAL;
default:
return -EINVAL;
}
}
static inline int module_lifecycle_report_state(
struct module_lifecycle_ctx *ctx,
enum module_lifecycle state)
{
switch (state) {
case LC_RUNNING:
module_set_state(MODULE_STATE_READY);
return 0;
case LC_STOPPED:
if (ctx->cfg->mode != ML_MODE_POWER) {
return -EINVAL;
}
module_set_state(ctx->cfg->stopped_state);
return 0;
case LC_SUSPENDED:
if (ctx->cfg->mode != ML_MODE_SUSPEND) {
return -EINVAL;
}
module_set_state(MODULE_STATE_SUSPENDED);
return 0;
case LC_ERROR:
module_set_state(MODULE_STATE_ERROR);
return 0;
case LC_UNINIT:
default:
return -EINVAL;
}
}
static inline int module_lifecycle_fail(struct module_lifecycle_ctx *ctx, int err)
{
ctx->state = LC_ERROR;
(void)module_lifecycle_report_state(ctx, LC_ERROR);
return err ? err : -EIO;
}
static inline int module_lifecycle_do_init(struct module_lifecycle_ctx *ctx)
{
int err = ctx->ops->do_init();
if (err) {
return module_lifecycle_fail(ctx, err);
}
return 0;
}
static inline int module_lifecycle_finish_transition(
struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
ctx->state = target;
return module_lifecycle_report_state(ctx, target);
}
static inline int module_lifecycle_start_running(struct module_lifecycle_ctx *ctx)
{
int err = 0;
if (ctx->ops->do_start != NULL) {
err = ctx->ops->do_start();
if (err) {
return module_lifecycle_fail(ctx, err);
}
}
return module_lifecycle_finish_transition(ctx, LC_RUNNING);
}
static inline int module_lifecycle_stop_running(
struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
int err;
if (ctx->ops->do_stop == NULL) {
return module_lifecycle_fail(ctx, -EINVAL);
}
err = ctx->ops->do_stop();
if (err) {
return module_lifecycle_fail(ctx, err);
}
return module_lifecycle_finish_transition(ctx, target);
}
static inline int module_set_lifecycle(struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
int err;
err = module_lifecycle_validate_ops(ctx);
if (err) {
return err;
}
if (ctx->state == LC_ERROR) {
return -EFAULT;
}
if (ctx->state == target) {
return 0;
}
if (target == LC_ERROR) {
return module_lifecycle_fail(ctx, -EIO);
}
if (!module_lifecycle_target_allowed(ctx, target) ||
!module_lifecycle_path_allowed(ctx->state, target)) {
return -EPERM;
}
switch (ctx->state) {
case LC_UNINIT:
err = module_lifecycle_do_init(ctx);
if (err) {
return err;
}
if (target == LC_RUNNING) {
return module_lifecycle_start_running(ctx);
}
return module_lifecycle_finish_transition(ctx, LC_STOPPED);
case LC_RUNNING:
return module_lifecycle_stop_running(ctx, target);
case LC_STOPPED:
case LC_SUSPENDED:
return module_lifecycle_start_running(ctx);
case LC_ERROR:
default:
return -EPERM;
}
}
#endif /* MODULE_LIFECYCLE_H_ */