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): 修复唤醒后模式恢复功能 - 在唤醒事件处理中添加了最后模式的恢复逻辑 - 确保设备唤醒后能够重新提交之前的模式切换事件
This commit is contained in:
@@ -52,7 +52,72 @@ static inline bool module_lifecycle_is_initialized(
|
||||
static inline bool module_lifecycle_reports_stopped_state(
|
||||
const struct module_lifecycle_ctx *ctx)
|
||||
{
|
||||
return ctx->cfg->mode != ML_MODE_NONE;
|
||||
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(
|
||||
@@ -65,14 +130,18 @@ static inline int module_lifecycle_report_state(
|
||||
return 0;
|
||||
|
||||
case LC_STOPPED:
|
||||
if (!module_lifecycle_reports_stopped_state(ctx)) {
|
||||
return 0;
|
||||
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;
|
||||
|
||||
@@ -105,39 +174,56 @@ static inline int module_lifecycle_do_init(struct module_lifecycle_ctx *ctx)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int module_lifecycle_do_start(struct module_lifecycle_ctx *ctx,
|
||||
enum module_lifecycle target)
|
||||
static inline int module_lifecycle_finish_transition(
|
||||
struct module_lifecycle_ctx *ctx,
|
||||
enum module_lifecycle target)
|
||||
{
|
||||
int err = ctx->ops->do_start();
|
||||
|
||||
if (err) {
|
||||
return module_lifecycle_fail(ctx, err);
|
||||
}
|
||||
|
||||
ctx->state = target;
|
||||
return module_lifecycle_report_state(ctx, target);
|
||||
}
|
||||
|
||||
static inline int module_lifecycle_do_stop(struct module_lifecycle_ctx *ctx,
|
||||
enum module_lifecycle target)
|
||||
static inline int module_lifecycle_start_running(struct module_lifecycle_ctx *ctx)
|
||||
{
|
||||
int err = ctx->ops->do_stop();
|
||||
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);
|
||||
}
|
||||
|
||||
ctx->state = target;
|
||||
return module_lifecycle_report_state(ctx, target);
|
||||
return module_lifecycle_finish_transition(ctx, target);
|
||||
}
|
||||
|
||||
static inline int module_set_lifecycle(struct module_lifecycle_ctx *ctx,
|
||||
enum module_lifecycle target)
|
||||
{
|
||||
if ((ctx == NULL) || (ctx->cfg == NULL) || (ctx->ops == NULL) ||
|
||||
(ctx->ops->do_init == NULL) || (ctx->ops->do_start == NULL) ||
|
||||
(ctx->ops->do_stop == NULL)) {
|
||||
return -EINVAL;
|
||||
int err;
|
||||
|
||||
err = module_lifecycle_validate_ops(ctx);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (ctx->state == LC_ERROR) {
|
||||
@@ -152,53 +238,35 @@ static inline int module_set_lifecycle(struct module_lifecycle_ctx *ctx,
|
||||
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) {
|
||||
int err = module_lifecycle_do_init(ctx);
|
||||
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
ctx->state = LC_STOPPED;
|
||||
return module_lifecycle_do_start(ctx, LC_RUNNING);
|
||||
return module_lifecycle_start_running(ctx);
|
||||
}
|
||||
|
||||
if (target == LC_STOPPED) {
|
||||
int err = module_lifecycle_do_init(ctx);
|
||||
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
ctx->state = LC_STOPPED;
|
||||
return module_lifecycle_report_state(ctx, LC_STOPPED);
|
||||
}
|
||||
|
||||
break;
|
||||
return module_lifecycle_finish_transition(ctx, LC_STOPPED);
|
||||
|
||||
case LC_RUNNING:
|
||||
if ((target == LC_STOPPED) || (target == LC_SUSPENDED)) {
|
||||
return module_lifecycle_do_stop(ctx, target);
|
||||
}
|
||||
|
||||
break;
|
||||
return module_lifecycle_stop_running(ctx, target);
|
||||
|
||||
case LC_STOPPED:
|
||||
case LC_SUSPENDED:
|
||||
if (target == LC_RUNNING) {
|
||||
return module_lifecycle_do_start(ctx, LC_RUNNING);
|
||||
}
|
||||
|
||||
break;
|
||||
return module_lifecycle_start_running(ctx);
|
||||
|
||||
case LC_ERROR:
|
||||
default:
|
||||
break;
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
#endif /* MODULE_LIFECYCLE_H_ */
|
||||
|
||||
Reference in New Issue
Block a user